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