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 23 static char **linux_env; 24 static char *linux_env_p; 25 static int linux_env_idx; 26 27 static void linux_params_init(ulong start, char *commandline); 28 static void linux_env_set(char *env_name, char *env_val); 29 30 static ulong arch_get_sp(void) 31 { 32 ulong ret; 33 34 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : ); 35 36 return ret; 37 } 38 39 void arch_lmb_reserve(struct lmb *lmb) 40 { 41 ulong sp; 42 43 sp = arch_get_sp(); 44 debug("## Current stack ends at 0x%08lx\n", sp); 45 46 /* adjust sp by 4K to be safe */ 47 sp -= 4096; 48 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp); 49 } 50 51 static void boot_prep_linux(bootm_headers_t *images) 52 { 53 char *commandline = getenv("bootargs"); 54 char env_buf[12]; 55 char *cp; 56 57 linux_params_init(UNCACHED_SDRAM(gd->bd->bi_boot_params), commandline); 58 59 #ifdef CONFIG_MEMSIZE_IN_BYTES 60 sprintf(env_buf, "%lu", (ulong)gd->ram_size); 61 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size); 62 #else 63 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20)); 64 debug("## Giving linux memsize in MB, %lu\n", 65 (ulong)(gd->ram_size >> 20)); 66 #endif /* CONFIG_MEMSIZE_IN_BYTES */ 67 68 linux_env_set("memsize", env_buf); 69 70 sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start)); 71 linux_env_set("initrd_start", env_buf); 72 73 sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start)); 74 linux_env_set("initrd_size", env_buf); 75 76 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart)); 77 linux_env_set("flash_start", env_buf); 78 79 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize)); 80 linux_env_set("flash_size", env_buf); 81 82 cp = getenv("ethaddr"); 83 if (cp) 84 linux_env_set("ethaddr", cp); 85 86 cp = getenv("eth1addr"); 87 if (cp) 88 linux_env_set("eth1addr", cp); 89 } 90 91 static void boot_jump_linux(bootm_headers_t *images) 92 { 93 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong); 94 kernel_entry_t kernel = (kernel_entry_t) images->ep; 95 96 debug("## Transferring control to Linux (at address %p) ...\n", kernel); 97 98 bootstage_mark(BOOTSTAGE_ID_RUN_OS); 99 100 /* we assume that the kernel is in place */ 101 printf("\nStarting kernel ...\n\n"); 102 103 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0); 104 } 105 106 int do_bootm_linux(int flag, int argc, char * const argv[], 107 bootm_headers_t *images) 108 { 109 /* No need for those on MIPS */ 110 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE) 111 return -1; 112 113 if (flag & BOOTM_STATE_OS_PREP) { 114 boot_prep_linux(images); 115 return 0; 116 } 117 118 if (flag & BOOTM_STATE_OS_GO) { 119 boot_jump_linux(images); 120 return 0; 121 } 122 123 boot_prep_linux(images); 124 boot_jump_linux(images); 125 126 /* does not return */ 127 return 1; 128 } 129 130 static void linux_params_init(ulong start, char *line) 131 { 132 char *next, *quote, *argp; 133 134 linux_argc = 1; 135 linux_argv = (char **)start; 136 linux_argv[0] = 0; 137 argp = (char *)(linux_argv + LINUX_MAX_ARGS); 138 139 next = line; 140 141 while (line && *line && linux_argc < LINUX_MAX_ARGS) { 142 quote = strchr(line, '"'); 143 next = strchr(line, ' '); 144 145 while (next && quote && quote < next) { 146 /* 147 * we found a left quote before the next blank 148 * now we have to find the matching right quote 149 */ 150 next = strchr(quote + 1, '"'); 151 if (next) { 152 quote = strchr(next + 1, '"'); 153 next = strchr(next + 1, ' '); 154 } 155 } 156 157 if (!next) 158 next = line + strlen(line); 159 160 linux_argv[linux_argc] = argp; 161 memcpy(argp, line, next - line); 162 argp[next - line] = 0; 163 164 argp += next - line + 1; 165 linux_argc++; 166 167 if (*next) 168 next++; 169 170 line = next; 171 } 172 173 linux_env = (char **)(((ulong) argp + 15) & ~15); 174 linux_env[0] = 0; 175 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS); 176 linux_env_idx = 0; 177 } 178 179 static void linux_env_set(char *env_name, char *env_val) 180 { 181 if (linux_env_idx < LINUX_MAX_ENVS - 1) { 182 linux_env[linux_env_idx] = linux_env_p; 183 184 strcpy(linux_env_p, env_name); 185 linux_env_p += strlen(env_name); 186 187 strcpy(linux_env_p, "="); 188 linux_env_p += 1; 189 190 strcpy(linux_env_p, env_val); 191 linux_env_p += strlen(env_val); 192 193 linux_env_p++; 194 linux_env[++linux_env_idx] = 0; 195 } 196 } 197