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