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