wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 10 | #include <image.h> |
Jean-Christophe PLAGNIOL-VILLARD | a31e091 | 2009-04-04 12:49:11 +0200 | [diff] [blame] | 11 | #include <u-boot/zlib.h> |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 12 | #include <asm/byteorder.h> |
| 13 | #include <asm/addrspace.h> |
| 14 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 17 | #define LINUX_MAX_ENVS 256 |
| 18 | #define LINUX_MAX_ARGS 256 |
| 19 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 20 | static int linux_argc; |
| 21 | static char **linux_argv; |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 22 | static char *linux_argp; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 23 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 24 | static char **linux_env; |
| 25 | static char *linux_env_p; |
| 26 | static int linux_env_idx; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 27 | |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 28 | static void linux_params_init(void); |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 29 | static void linux_env_set(char *env_name, char *env_val); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 30 | |
Daniel Schwierzeck | f66cc1e | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 31 | static ulong arch_get_sp(void) |
| 32 | { |
| 33 | ulong ret; |
| 34 | |
| 35 | __asm__ __volatile__("move %0, $sp" : "=r"(ret) : ); |
| 36 | |
| 37 | return ret; |
| 38 | } |
| 39 | |
| 40 | void arch_lmb_reserve(struct lmb *lmb) |
| 41 | { |
| 42 | ulong sp; |
| 43 | |
| 44 | sp = arch_get_sp(); |
| 45 | debug("## Current stack ends at 0x%08lx\n", sp); |
| 46 | |
| 47 | /* adjust sp by 4K to be safe */ |
| 48 | sp -= 4096; |
| 49 | lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp); |
| 50 | } |
| 51 | |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 52 | static void linux_cmdline_init(void) |
| 53 | { |
| 54 | linux_argc = 1; |
| 55 | linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params); |
| 56 | linux_argv[0] = 0; |
| 57 | linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS); |
| 58 | } |
| 59 | |
| 60 | static void linux_cmdline_set(const char *value, size_t len) |
| 61 | { |
| 62 | linux_argv[linux_argc] = linux_argp; |
| 63 | memcpy(linux_argp, value, len); |
| 64 | linux_argp[len] = 0; |
| 65 | |
| 66 | linux_argp += len + 1; |
| 67 | linux_argc++; |
| 68 | } |
| 69 | |
| 70 | static void linux_cmdline_dump(void) |
| 71 | { |
| 72 | int i; |
| 73 | |
| 74 | debug("## cmdline argv at 0x%p, argp at 0x%p\n", |
| 75 | linux_argv, linux_argp); |
| 76 | |
| 77 | for (i = 1; i < linux_argc; i++) |
| 78 | debug(" arg %03d: %s\n", i, linux_argv[i]); |
| 79 | } |
| 80 | |
| 81 | static void boot_cmdline_linux(bootm_headers_t *images) |
| 82 | { |
| 83 | const char *bootargs, *next, *quote; |
| 84 | |
| 85 | linux_cmdline_init(); |
| 86 | |
| 87 | bootargs = getenv("bootargs"); |
| 88 | if (!bootargs) |
| 89 | return; |
| 90 | |
| 91 | next = bootargs; |
| 92 | |
| 93 | while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) { |
| 94 | quote = strchr(bootargs, '"'); |
| 95 | next = strchr(bootargs, ' '); |
| 96 | |
| 97 | while (next && quote && quote < next) { |
| 98 | /* |
| 99 | * we found a left quote before the next blank |
| 100 | * now we have to find the matching right quote |
| 101 | */ |
| 102 | next = strchr(quote + 1, '"'); |
| 103 | if (next) { |
| 104 | quote = strchr(next + 1, '"'); |
| 105 | next = strchr(next + 1, ' '); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (!next) |
| 110 | next = bootargs + strlen(bootargs); |
| 111 | |
| 112 | linux_cmdline_set(bootargs, next - bootargs); |
| 113 | |
| 114 | if (*next) |
| 115 | next++; |
| 116 | |
| 117 | bootargs = next; |
| 118 | } |
| 119 | |
| 120 | linux_cmdline_dump(); |
| 121 | } |
| 122 | |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 123 | static void boot_prep_linux(bootm_headers_t *images) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 124 | { |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 125 | char env_buf[12]; |
| 126 | char *cp; |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 127 | |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 128 | linux_params_init(); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 129 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 130 | #ifdef CONFIG_MEMSIZE_IN_BYTES |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 131 | sprintf(env_buf, "%lu", (ulong)gd->ram_size); |
| 132 | debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 133 | #else |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 134 | sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20)); |
| 135 | debug("## Giving linux memsize in MB, %lu\n", |
Daniel Schwierzeck | 45bde48 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 136 | (ulong)(gd->ram_size >> 20)); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 137 | #endif /* CONFIG_MEMSIZE_IN_BYTES */ |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 138 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 139 | linux_env_set("memsize", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 140 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 141 | sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start)); |
| 142 | linux_env_set("initrd_start", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 143 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 144 | sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start)); |
| 145 | linux_env_set("initrd_size", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 146 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 147 | sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart)); |
| 148 | linux_env_set("flash_start", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 149 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 150 | sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize)); |
| 151 | linux_env_set("flash_size", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 152 | |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 153 | cp = getenv("ethaddr"); |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 154 | if (cp) |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 155 | linux_env_set("ethaddr", cp); |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 156 | |
| 157 | cp = getenv("eth1addr"); |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 158 | if (cp) |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 159 | linux_env_set("eth1addr", cp); |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 160 | } |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 161 | |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 162 | static void boot_jump_linux(bootm_headers_t *images) |
| 163 | { |
Daniel Schwierzeck | c4b3784 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 164 | typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong); |
| 165 | kernel_entry_t kernel = (kernel_entry_t) images->ep; |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 166 | |
Daniel Schwierzeck | c4b3784 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 167 | debug("## Transferring control to Linux (at address %p) ...\n", kernel); |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 168 | |
| 169 | bootstage_mark(BOOTSTAGE_ID_RUN_OS); |
| 170 | |
| 171 | /* we assume that the kernel is in place */ |
| 172 | printf("\nStarting kernel ...\n\n"); |
| 173 | |
Daniel Schwierzeck | c4b3784 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 174 | kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0); |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | int do_bootm_linux(int flag, int argc, char * const argv[], |
| 178 | bootm_headers_t *images) |
| 179 | { |
Gabor Juhos | 9c170e2 | 2013-01-07 02:53:42 +0000 | [diff] [blame] | 180 | /* No need for those on MIPS */ |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 181 | if (flag & BOOTM_STATE_OS_BD_T) |
Gabor Juhos | 9c170e2 | 2013-01-07 02:53:42 +0000 | [diff] [blame] | 182 | return -1; |
| 183 | |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 184 | if (flag & BOOTM_STATE_OS_CMDLINE) { |
| 185 | boot_cmdline_linux(images); |
| 186 | return 0; |
| 187 | } |
| 188 | |
Gabor Juhos | 9c170e2 | 2013-01-07 02:53:42 +0000 | [diff] [blame] | 189 | if (flag & BOOTM_STATE_OS_PREP) { |
| 190 | boot_prep_linux(images); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | if (flag & BOOTM_STATE_OS_GO) { |
| 195 | boot_jump_linux(images); |
| 196 | return 0; |
| 197 | } |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 198 | |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 199 | boot_cmdline_linux(images); |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 200 | boot_prep_linux(images); |
Gabor Juhos | e08634c | 2013-01-07 02:53:40 +0000 | [diff] [blame] | 201 | boot_jump_linux(images); |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 202 | |
Marian Balakowicz | cd7c596 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 203 | /* does not return */ |
Kumar Gala | 40d7e99 | 2008-08-15 08:24:45 -0500 | [diff] [blame] | 204 | return 1; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 207 | static void linux_params_init(void) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 208 | { |
Daniel Schwierzeck | 59e8cbd | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 209 | linux_env = (char **)(((ulong) linux_argp + 15) & ~15); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 210 | linux_env[0] = 0; |
Daniel Schwierzeck | 45bde48 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 211 | linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 212 | linux_env_idx = 0; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 215 | static void linux_env_set(char *env_name, char *env_val) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 216 | { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 217 | if (linux_env_idx < LINUX_MAX_ENVS - 1) { |
| 218 | linux_env[linux_env_idx] = linux_env_p; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 219 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 220 | strcpy(linux_env_p, env_name); |
| 221 | linux_env_p += strlen(env_name); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 222 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 223 | strcpy(linux_env_p, "="); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 224 | linux_env_p += 1; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 225 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 226 | strcpy(linux_env_p, env_val); |
| 227 | linux_env_p += strlen(env_val); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 228 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 229 | linux_env_p++; |
| 230 | linux_env[++linux_env_idx] = 0; |
| 231 | } |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 232 | } |