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 static int linux_argc; 21 static char **linux_argv; 22 static char *linux_argp; 23 24 static char **linux_env; 25 static char *linux_env_p; 26 static int linux_env_idx; 27 28 static void linux_params_init(void); 29 static void linux_env_set(char *env_name, char *env_val); 30 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 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 123 static void boot_prep_linux(bootm_headers_t *images) 124 { 125 char env_buf[12]; 126 char *cp; 127 128 linux_params_init(); 129 130 #ifdef CONFIG_MEMSIZE_IN_BYTES 131 sprintf(env_buf, "%lu", (ulong)gd->ram_size); 132 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size); 133 #else 134 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20)); 135 debug("## Giving linux memsize in MB, %lu\n", 136 (ulong)(gd->ram_size >> 20)); 137 #endif /* CONFIG_MEMSIZE_IN_BYTES */ 138 139 linux_env_set("memsize", env_buf); 140 141 sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start)); 142 linux_env_set("initrd_start", env_buf); 143 144 sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start)); 145 linux_env_set("initrd_size", env_buf); 146 147 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart)); 148 linux_env_set("flash_start", env_buf); 149 150 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize)); 151 linux_env_set("flash_size", env_buf); 152 153 cp = getenv("ethaddr"); 154 if (cp) 155 linux_env_set("ethaddr", cp); 156 157 cp = getenv("eth1addr"); 158 if (cp) 159 linux_env_set("eth1addr", cp); 160 } 161 162 static void boot_jump_linux(bootm_headers_t *images) 163 { 164 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong); 165 kernel_entry_t kernel = (kernel_entry_t) images->ep; 166 167 debug("## Transferring control to Linux (at address %p) ...\n", kernel); 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 174 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0); 175 } 176 177 int do_bootm_linux(int flag, int argc, char * const argv[], 178 bootm_headers_t *images) 179 { 180 /* No need for those on MIPS */ 181 if (flag & BOOTM_STATE_OS_BD_T) 182 return -1; 183 184 if (flag & BOOTM_STATE_OS_CMDLINE) { 185 boot_cmdline_linux(images); 186 return 0; 187 } 188 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 } 198 199 boot_cmdline_linux(images); 200 boot_prep_linux(images); 201 boot_jump_linux(images); 202 203 /* does not return */ 204 return 1; 205 } 206 207 static void linux_params_init(void) 208 { 209 linux_env = (char **)(((ulong) linux_argp + 15) & ~15); 210 linux_env[0] = 0; 211 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS); 212 linux_env_idx = 0; 213 } 214 215 static void linux_env_set(char *env_name, char *env_val) 216 { 217 if (linux_env_idx < LINUX_MAX_ENVS - 1) { 218 linux_env[linux_env_idx] = linux_env_p; 219 220 strcpy(linux_env_p, env_name); 221 linux_env_p += strlen(env_name); 222 223 strcpy(linux_env_p, "="); 224 linux_env_p += 1; 225 226 strcpy(linux_env_p, env_val); 227 linux_env_p += strlen(env_val); 228 229 linux_env_p++; 230 linux_env[++linux_env_idx] = 0; 231 } 232 } 233