wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 10 | #include <image.h> |
Jean-Christophe PLAGNIOL-VILLARD | 6bb9449 | 2009-04-04 12:49:11 +0200 | [diff] [blame] | 11 | #include <u-boot/zlib.h> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 12 | #include <asm/byteorder.h> |
| 13 | #include <asm/addrspace.h> |
| 14 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 17 | #define LINUX_MAX_ENVS 256 |
| 18 | #define LINUX_MAX_ARGS 256 |
| 19 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 20 | static int linux_argc; |
| 21 | static char **linux_argv; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 22 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 23 | static char **linux_env; |
| 24 | static char *linux_env_p; |
| 25 | static int linux_env_idx; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 26 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 27 | static void linux_params_init(ulong start, char *commandline); |
| 28 | static void linux_env_set(char *env_name, char *env_val); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 29 | |
Gabor Juhos | 72cb5f0 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 30 | static void boot_prep_linux(bootm_headers_t *images) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 31 | { |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 32 | char *commandline = getenv("bootargs"); |
| 33 | char env_buf[12]; |
| 34 | char *cp; |
Marian Balakowicz | b4a12a9 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 35 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 36 | linux_params_init(UNCACHED_SDRAM(gd->bd->bi_boot_params), commandline); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 37 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 38 | #ifdef CONFIG_MEMSIZE_IN_BYTES |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 39 | sprintf(env_buf, "%lu", (ulong)gd->ram_size); |
| 40 | debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 41 | #else |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 42 | sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20)); |
| 43 | debug("## Giving linux memsize in MB, %lu\n", |
Daniel Schwierzeck | 40f30c0 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 44 | (ulong)(gd->ram_size >> 20)); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 45 | #endif /* CONFIG_MEMSIZE_IN_BYTES */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 46 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 47 | linux_env_set("memsize", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 48 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 49 | sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start)); |
| 50 | linux_env_set("initrd_start", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 51 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 52 | sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start)); |
| 53 | linux_env_set("initrd_size", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 54 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 55 | sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart)); |
| 56 | linux_env_set("flash_start", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 57 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 58 | sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize)); |
| 59 | linux_env_set("flash_size", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 60 | |
Jason McMullan | cfbe457 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 61 | cp = getenv("ethaddr"); |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 62 | if (cp) |
Jason McMullan | cfbe457 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 63 | linux_env_set("ethaddr", cp); |
Jason McMullan | cfbe457 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 64 | |
| 65 | cp = getenv("eth1addr"); |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 66 | if (cp) |
Jason McMullan | cfbe457 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 67 | linux_env_set("eth1addr", cp); |
Gabor Juhos | 72cb5f0 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void boot_jump_linux(bootm_headers_t *images) |
| 71 | { |
Daniel Schwierzeck | 0b95a7c | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 72 | typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong); |
| 73 | kernel_entry_t kernel = (kernel_entry_t) images->ep; |
Gabor Juhos | 72cb5f0 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 74 | |
Daniel Schwierzeck | 0b95a7c | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 75 | debug("## Transferring control to Linux (at address %p) ...\n", kernel); |
Gabor Juhos | 72cb5f0 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 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 | |
Daniel Schwierzeck | 0b95a7c | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 82 | kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0); |
Gabor Juhos | 72cb5f0 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int do_bootm_linux(int flag, int argc, char * const argv[], |
| 86 | bootm_headers_t *images) |
| 87 | { |
Gabor Juhos | 7f959fd | 2013-01-07 02:53:42 +0000 | [diff] [blame] | 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 | } |
Jason McMullan | cfbe457 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 101 | |
Gabor Juhos | 72cb5f0 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 102 | boot_prep_linux(images); |
Gabor Juhos | e0d783b | 2013-01-07 02:53:40 +0000 | [diff] [blame] | 103 | boot_jump_linux(images); |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 104 | |
Marian Balakowicz | df8ff33 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 105 | /* does not return */ |
Kumar Gala | 48626aa | 2008-08-15 08:24:45 -0500 | [diff] [blame] | 106 | return 1; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 109 | static void linux_params_init(ulong start, char *line) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 110 | { |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 111 | char *next, *quote, *argp; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 112 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 113 | linux_argc = 1; |
Daniel Schwierzeck | 40f30c0 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 114 | linux_argv = (char **)start; |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 115 | linux_argv[0] = 0; |
Daniel Schwierzeck | 40f30c0 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 116 | argp = (char *)(linux_argv + LINUX_MAX_ARGS); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 117 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 118 | next = line; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 119 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 120 | while (line && *line && linux_argc < LINUX_MAX_ARGS) { |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 121 | quote = strchr(line, '"'); |
| 122 | next = strchr(line, ' '); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 123 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 124 | while (next && quote && quote < next) { |
Daniel Schwierzeck | 40f30c0 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 125 | /* |
| 126 | * we found a left quote before the next blank |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 127 | * now we have to find the matching right quote |
| 128 | */ |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 129 | next = strchr(quote + 1, '"'); |
| 130 | if (next) { |
| 131 | quote = strchr(next + 1, '"'); |
| 132 | next = strchr(next + 1, ' '); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 135 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 136 | if (!next) |
| 137 | next = line + strlen(line); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 138 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 139 | linux_argv[linux_argc] = argp; |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 140 | memcpy(argp, line, next - line); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 141 | argp[next - line] = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 142 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 143 | argp += next - line + 1; |
| 144 | linux_argc++; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 145 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 146 | if (*next) |
| 147 | next++; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 148 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 149 | line = next; |
| 150 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 151 | |
Daniel Schwierzeck | 40f30c0 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 152 | linux_env = (char **)(((ulong) argp + 15) & ~15); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 153 | linux_env[0] = 0; |
Daniel Schwierzeck | 40f30c0 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 154 | linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 155 | linux_env_idx = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 158 | static void linux_env_set(char *env_name, char *env_val) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 159 | { |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 160 | if (linux_env_idx < LINUX_MAX_ENVS - 1) { |
| 161 | linux_env[linux_env_idx] = linux_env_p; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 162 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 163 | strcpy(linux_env_p, env_name); |
| 164 | linux_env_p += strlen(env_name); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 165 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 166 | strcpy(linux_env_p, "="); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 167 | linux_env_p += 1; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 168 | |
Daniel Schwierzeck | 83010eb | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 169 | strcpy(linux_env_p, env_val); |
| 170 | linux_env_p += strlen(env_val); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 172 | linux_env_p++; |
| 173 | linux_env[++linux_env_idx] = 0; |
| 174 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 175 | } |