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