wdenk | c021880 | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 10 | #include <image.h> |
Jean-Christophe PLAGNIOL-VILLARD | a31e091 | 2009-04-04 12:49:11 +0200 | [diff] [blame] | 11 | #include <u-boot/zlib.h> |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 12 | #include <asm/byteorder.h> |
| 13 | #include <asm/addrspace.h> |
| 14 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 17 | #define LINUX_MAX_ENVS 256 |
| 18 | #define LINUX_MAX_ARGS 256 |
| 19 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 20 | static int linux_argc; |
| 21 | static char **linux_argv; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 22 | |
Daniel Schwierzeck | e51a6b7 | 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 | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 26 | |
Daniel Schwierzeck | e51a6b7 | 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 | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 29 | |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 30 | static void boot_prep_linux(bootm_headers_t *images) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 31 | { |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 32 | char *commandline = getenv("bootargs"); |
| 33 | char env_buf[12]; |
| 34 | char *cp; |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 35 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 36 | linux_params_init(UNCACHED_SDRAM(gd->bd->bi_boot_params), commandline); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 37 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 38 | #ifdef CONFIG_MEMSIZE_IN_BYTES |
Daniel Schwierzeck | e51a6b7 | 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 | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 41 | #else |
Daniel Schwierzeck | e51a6b7 | 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 | 45bde48 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 44 | (ulong)(gd->ram_size >> 20)); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 45 | #endif /* CONFIG_MEMSIZE_IN_BYTES */ |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 46 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 47 | linux_env_set("memsize", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 48 | |
Daniel Schwierzeck | e51a6b7 | 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 | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 51 | |
Daniel Schwierzeck | e51a6b7 | 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 | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 54 | |
Daniel Schwierzeck | e51a6b7 | 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 | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 57 | |
Daniel Schwierzeck | e51a6b7 | 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 | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 60 | |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 61 | cp = getenv("ethaddr"); |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 62 | if (cp) |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 63 | linux_env_set("ethaddr", cp); |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 64 | |
| 65 | cp = getenv("eth1addr"); |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 66 | if (cp) |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 67 | linux_env_set("eth1addr", cp); |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 68 | } |
Jason McMullan | e7c3745 | 2008-06-08 23:56:00 -0400 | [diff] [blame] | 69 | |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 70 | static void boot_jump_linux(bootm_headers_t *images) |
| 71 | { |
Daniel Schwierzeck | c4b3784 | 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 | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 74 | |
Daniel Schwierzeck | c4b3784 | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 75 | debug("## Transferring control to Linux (at address %p) ...\n", kernel); |
Gabor Juhos | 0ea7213 | 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 | c4b3784 | 2013-05-09 17:10:06 +0200 | [diff] [blame^] | 82 | kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0); |
Gabor Juhos | 0ea7213 | 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 | 9c170e2 | 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 | } |
Gabor Juhos | 0ea7213 | 2013-01-07 02:53:41 +0000 | [diff] [blame] | 101 | |
| 102 | boot_prep_linux(images); |
Gabor Juhos | e08634c | 2013-01-07 02:53:40 +0000 | [diff] [blame] | 103 | boot_jump_linux(images); |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 104 | |
Marian Balakowicz | cd7c596 | 2008-03-12 10:33:00 +0100 | [diff] [blame] | 105 | /* does not return */ |
Kumar Gala | 40d7e99 | 2008-08-15 08:24:45 -0500 | [diff] [blame] | 106 | return 1; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 109 | static void linux_params_init(ulong start, char *line) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 110 | { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 111 | char *next, *quote, *argp; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 112 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 113 | linux_argc = 1; |
Daniel Schwierzeck | 45bde48 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 114 | linux_argv = (char **)start; |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 115 | linux_argv[0] = 0; |
Daniel Schwierzeck | 45bde48 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 116 | argp = (char *)(linux_argv + LINUX_MAX_ARGS); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 117 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 118 | next = line; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 119 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 120 | while (line && *line && linux_argc < LINUX_MAX_ARGS) { |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 121 | quote = strchr(line, '"'); |
| 122 | next = strchr(line, ' '); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 123 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 124 | while (next && quote && quote < next) { |
Daniel Schwierzeck | 45bde48 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 125 | /* |
| 126 | * we found a left quote before the next blank |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 127 | * now we have to find the matching right quote |
| 128 | */ |
Daniel Schwierzeck | e51a6b7 | 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 | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 136 | if (!next) |
| 137 | next = line + strlen(line); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 138 | |
| 139 | linux_argv[linux_argc] = argp; |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 140 | memcpy(argp, line, next - line); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 141 | argp[next - line] = 0; |
| 142 | |
| 143 | argp += next - line + 1; |
| 144 | linux_argc++; |
| 145 | |
| 146 | if (*next) |
| 147 | next++; |
| 148 | |
| 149 | line = next; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Daniel Schwierzeck | 45bde48 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 152 | linux_env = (char **)(((ulong) argp + 15) & ~15); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 153 | linux_env[0] = 0; |
Daniel Schwierzeck | 45bde48 | 2013-05-09 17:10:06 +0200 | [diff] [blame] | 154 | linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 155 | linux_env_idx = 0; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 158 | static void linux_env_set(char *env_name, char *env_val) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 159 | { |
wdenk | 5da627a | 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 | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 162 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 163 | strcpy(linux_env_p, env_name); |
| 164 | linux_env_p += strlen(env_name); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 165 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 166 | strcpy(linux_env_p, "="); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 167 | linux_env_p += 1; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 168 | |
Daniel Schwierzeck | e51a6b7 | 2012-06-03 23:46:04 +0200 | [diff] [blame] | 169 | strcpy(linux_env_p, env_val); |
| 170 | linux_env_p += strlen(env_val); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 172 | linux_env_p++; |
| 173 | linux_env[++linux_env_idx] = 0; |
| 174 | } |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 175 | } |