1 /* 2 * (C) Copyright 2003 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <command.h> 10 #include <image.h> 11 #include <u-boot/zlib.h> 12 #include <asm/byteorder.h> 13 #include <asm/addrspace.h> 14 15 DECLARE_GLOBAL_DATA_PTR; 16 17 #define LINUX_MAX_ENVS 256 18 #define LINUX_MAX_ARGS 256 19 20 #if defined(CONFIG_MALTA) 21 #define mips_boot_malta 1 22 #else 23 #define mips_boot_malta 0 24 #endif 25 26 static int linux_argc; 27 static char **linux_argv; 28 static char *linux_argp; 29 30 static char **linux_env; 31 static char *linux_env_p; 32 static int linux_env_idx; 33 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 55 static int boot_setup_linux(bootm_headers_t *images) 56 { 57 int ret; 58 ulong rd_len; 59 60 rd_len = images->rd_end - images->rd_start; 61 ret = boot_ramdisk_high(&images->lmb, images->rd_start, 62 rd_len, &images->initrd_start, &images->initrd_end); 63 if (ret) 64 return ret; 65 66 return 0; 67 } 68 69 static void linux_cmdline_init(void) 70 { 71 linux_argc = 1; 72 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params); 73 linux_argv[0] = 0; 74 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS); 75 } 76 77 static void linux_cmdline_set(const char *value, size_t len) 78 { 79 linux_argv[linux_argc] = linux_argp; 80 memcpy(linux_argp, value, len); 81 linux_argp[len] = 0; 82 83 linux_argp += len + 1; 84 linux_argc++; 85 } 86 87 static void linux_cmdline_dump(void) 88 { 89 int i; 90 91 debug("## cmdline argv at 0x%p, argp at 0x%p\n", 92 linux_argv, linux_argp); 93 94 for (i = 1; i < linux_argc; i++) 95 debug(" arg %03d: %s\n", i, linux_argv[i]); 96 } 97 98 static void boot_cmdline_linux(bootm_headers_t *images) 99 { 100 const char *bootargs, *next, *quote; 101 102 linux_cmdline_init(); 103 104 bootargs = getenv("bootargs"); 105 if (!bootargs) 106 return; 107 108 next = bootargs; 109 110 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) { 111 quote = strchr(bootargs, '"'); 112 next = strchr(bootargs, ' '); 113 114 while (next && quote && quote < next) { 115 /* 116 * we found a left quote before the next blank 117 * now we have to find the matching right quote 118 */ 119 next = strchr(quote + 1, '"'); 120 if (next) { 121 quote = strchr(next + 1, '"'); 122 next = strchr(next + 1, ' '); 123 } 124 } 125 126 if (!next) 127 next = bootargs + strlen(bootargs); 128 129 linux_cmdline_set(bootargs, next - bootargs); 130 131 if (*next) 132 next++; 133 134 bootargs = next; 135 } 136 137 linux_cmdline_dump(); 138 } 139 140 static void linux_env_init(void) 141 { 142 linux_env = (char **)(((ulong) linux_argp + 15) & ~15); 143 linux_env[0] = 0; 144 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS); 145 linux_env_idx = 0; 146 } 147 148 static void linux_env_set(const char *env_name, const char *env_val) 149 { 150 if (linux_env_idx < LINUX_MAX_ENVS - 1) { 151 linux_env[linux_env_idx] = linux_env_p; 152 153 strcpy(linux_env_p, env_name); 154 linux_env_p += strlen(env_name); 155 156 if (mips_boot_malta) { 157 linux_env_p++; 158 linux_env[++linux_env_idx] = linux_env_p; 159 } else { 160 *linux_env_p++ = '='; 161 } 162 163 strcpy(linux_env_p, env_val); 164 linux_env_p += strlen(env_val); 165 166 linux_env_p++; 167 linux_env[++linux_env_idx] = 0; 168 } 169 } 170 171 static void boot_prep_linux(bootm_headers_t *images) 172 { 173 char env_buf[12]; 174 const char *cp; 175 ulong rd_start, rd_size; 176 177 #ifdef CONFIG_MEMSIZE_IN_BYTES 178 sprintf(env_buf, "%lu", (ulong)gd->ram_size); 179 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size); 180 #else 181 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20)); 182 debug("## Giving linux memsize in MB, %lu\n", 183 (ulong)(gd->ram_size >> 20)); 184 #endif /* CONFIG_MEMSIZE_IN_BYTES */ 185 186 rd_start = UNCACHED_SDRAM(images->initrd_start); 187 rd_size = images->initrd_end - images->initrd_start; 188 189 linux_env_init(); 190 191 linux_env_set("memsize", env_buf); 192 193 sprintf(env_buf, "0x%08lX", rd_start); 194 linux_env_set("initrd_start", env_buf); 195 196 sprintf(env_buf, "0x%lX", rd_size); 197 linux_env_set("initrd_size", env_buf); 198 199 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart)); 200 linux_env_set("flash_start", env_buf); 201 202 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize)); 203 linux_env_set("flash_size", env_buf); 204 205 cp = getenv("ethaddr"); 206 if (cp) 207 linux_env_set("ethaddr", cp); 208 209 cp = getenv("eth1addr"); 210 if (cp) 211 linux_env_set("eth1addr", cp); 212 213 if (mips_boot_malta) { 214 sprintf(env_buf, "%un8r", gd->baudrate); 215 linux_env_set("modetty0", env_buf); 216 } 217 } 218 219 static void boot_jump_linux(bootm_headers_t *images) 220 { 221 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong); 222 kernel_entry_t kernel = (kernel_entry_t) images->ep; 223 ulong linux_extra = 0; 224 225 debug("## Transferring control to Linux (at address %p) ...\n", kernel); 226 227 bootstage_mark(BOOTSTAGE_ID_RUN_OS); 228 229 if (mips_boot_malta) 230 linux_extra = gd->ram_size; 231 232 /* we assume that the kernel is in place */ 233 printf("\nStarting kernel ...\n\n"); 234 235 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra); 236 } 237 238 int do_bootm_linux(int flag, int argc, char * const argv[], 239 bootm_headers_t *images) 240 { 241 int ret; 242 243 /* No need for those on MIPS */ 244 if (flag & BOOTM_STATE_OS_BD_T) 245 return -1; 246 247 if (flag & BOOTM_STATE_OS_CMDLINE) { 248 boot_cmdline_linux(images); 249 return 0; 250 } 251 252 if (flag & BOOTM_STATE_OS_PREP) { 253 boot_prep_linux(images); 254 return 0; 255 } 256 257 if (flag & BOOTM_STATE_OS_GO) { 258 boot_jump_linux(images); 259 return 0; 260 } 261 262 ret = boot_setup_linux(images); 263 if (ret) 264 return ret; 265 266 boot_cmdline_linux(images); 267 boot_prep_linux(images); 268 boot_jump_linux(images); 269 270 /* does not return */ 271 return 1; 272 } 273