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 void (*theKernel) (int, char **, char **, int *); 73 74 /* find kernel entry point */ 75 theKernel = (void (*)(int, char **, char **, int *))images->ep; 76 77 debug("## Transferring control to Linux (at address %08lx) ...\n", 78 (ulong) theKernel); 79 80 bootstage_mark(BOOTSTAGE_ID_RUN_OS); 81 82 /* we assume that the kernel is in place */ 83 printf("\nStarting kernel ...\n\n"); 84 85 theKernel(linux_argc, linux_argv, linux_env, 0); 86 } 87 88 int do_bootm_linux(int flag, int argc, char * const argv[], 89 bootm_headers_t *images) 90 { 91 /* No need for those on MIPS */ 92 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE) 93 return -1; 94 95 if (flag & BOOTM_STATE_OS_PREP) { 96 boot_prep_linux(images); 97 return 0; 98 } 99 100 if (flag & BOOTM_STATE_OS_GO) { 101 boot_jump_linux(images); 102 return 0; 103 } 104 105 boot_prep_linux(images); 106 boot_jump_linux(images); 107 108 /* does not return */ 109 return 1; 110 } 111 112 static void linux_params_init(ulong start, char *line) 113 { 114 char *next, *quote, *argp; 115 116 linux_argc = 1; 117 linux_argv = (char **)start; 118 linux_argv[0] = 0; 119 argp = (char *)(linux_argv + LINUX_MAX_ARGS); 120 121 next = line; 122 123 while (line && *line && linux_argc < LINUX_MAX_ARGS) { 124 quote = strchr(line, '"'); 125 next = strchr(line, ' '); 126 127 while (next && quote && quote < next) { 128 /* 129 * we found a left quote before the next blank 130 * now we have to find the matching right quote 131 */ 132 next = strchr(quote + 1, '"'); 133 if (next) { 134 quote = strchr(next + 1, '"'); 135 next = strchr(next + 1, ' '); 136 } 137 } 138 139 if (!next) 140 next = line + strlen(line); 141 142 linux_argv[linux_argc] = argp; 143 memcpy(argp, line, next - line); 144 argp[next - line] = 0; 145 146 argp += next - line + 1; 147 linux_argc++; 148 149 if (*next) 150 next++; 151 152 line = next; 153 } 154 155 linux_env = (char **)(((ulong) argp + 15) & ~15); 156 linux_env[0] = 0; 157 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS); 158 linux_env_idx = 0; 159 } 160 161 static void linux_env_set(char *env_name, char *env_val) 162 { 163 if (linux_env_idx < LINUX_MAX_ENVS - 1) { 164 linux_env[linux_env_idx] = linux_env_p; 165 166 strcpy(linux_env_p, env_name); 167 linux_env_p += strlen(env_name); 168 169 strcpy(linux_env_p, "="); 170 linux_env_p += 1; 171 172 strcpy(linux_env_p, env_val); 173 linux_env_p += strlen(env_val); 174 175 linux_env_p++; 176 linux_env[++linux_env_idx] = 0; 177 } 178 } 179