wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 4 | * Marius Groeger <mgroeger@sysgo.de> |
| 5 | * |
| 6 | * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <command.h> |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 26 | #include <image.h> |
| 27 | #include <zlib.h> |
| 28 | #include <asm/byteorder.h> |
| 29 | #include <asm/zimage.h> |
| 30 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 31 | /*cmd_boot.c*/ |
| 32 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 33 | |
| 34 | extern image_header_t header; /* from cmd_bootm.c */ |
| 35 | |
| 36 | |
| 37 | image_header_t *fake_header(image_header_t *hdr, void *ptr, int size) |
| 38 | { |
| 39 | /* try each supported image type in order */ |
| 40 | if (NULL != fake_zimage_header(hdr, ptr, size)) { |
| 41 | return hdr; |
| 42 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 43 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
| 49 | ulong addr, ulong *len_ptr, int verify) |
| 50 | { |
wdenk | ea909b7 | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 51 | void *base_ptr; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 52 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 53 | ulong len = 0, checksum; |
| 54 | ulong initrd_start, initrd_end; |
| 55 | ulong data; |
| 56 | image_header_t *hdr = &header; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 57 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 58 | /* |
| 59 | * Check if there is an initrd image |
| 60 | */ |
| 61 | if (argc >= 3) { |
| 62 | addr = simple_strtoul(argv[2], NULL, 16); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 63 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 64 | printf ("## Loading Ramdisk Image at %08lx ...\n", addr); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 65 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 66 | /* Copy header so we can blank CRC field for re-calculation */ |
| 67 | memcpy (&header, (char *)addr, sizeof(image_header_t)); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 68 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 69 | if (ntohl(hdr->ih_magic) != IH_MAGIC) { |
| 70 | printf ("Bad Magic Number\n"); |
| 71 | do_reset (cmdtp, flag, argc, argv); |
| 72 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 73 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 74 | data = (ulong)&header; |
| 75 | len = sizeof(image_header_t); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 76 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 77 | checksum = ntohl(hdr->ih_hcrc); |
| 78 | hdr->ih_hcrc = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 79 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 80 | if (crc32 (0, (char *)data, len) != checksum) { |
| 81 | printf ("Bad Header Checksum\n"); |
| 82 | do_reset (cmdtp, flag, argc, argv); |
| 83 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 84 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 85 | print_image_hdr (hdr); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 86 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 87 | data = addr + sizeof(image_header_t); |
| 88 | len = ntohl(hdr->ih_size); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 89 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 90 | if (verify) { |
| 91 | ulong csum = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 92 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 93 | printf (" Verifying Checksum ... "); |
| 94 | csum = crc32 (0, (char *)data, len); |
| 95 | if (csum != ntohl(hdr->ih_dcrc)) { |
| 96 | printf ("Bad Data CRC\n"); |
| 97 | do_reset (cmdtp, flag, argc, argv); |
| 98 | } |
| 99 | printf ("OK\n"); |
| 100 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 101 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 102 | if ((hdr->ih_os != IH_OS_LINUX) || |
| 103 | (hdr->ih_arch != IH_CPU_I386) || |
| 104 | (hdr->ih_type != IH_TYPE_RAMDISK) ) { |
| 105 | printf ("No Linux i386 Ramdisk Image\n"); |
| 106 | do_reset (cmdtp, flag, argc, argv); |
| 107 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 108 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 109 | /* |
| 110 | * Now check if we have a multifile image |
| 111 | */ |
| 112 | } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) { |
| 113 | ulong tail = ntohl(len_ptr[0]) % 4; |
| 114 | int i; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 115 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 116 | /* skip kernel length and terminator */ |
| 117 | data = (ulong)(&len_ptr[2]); |
| 118 | /* skip any additional image length fields */ |
| 119 | for (i=1; len_ptr[i]; ++i) |
| 120 | data += 4; |
| 121 | /* add kernel length, and align */ |
| 122 | data += ntohl(len_ptr[0]); |
| 123 | if (tail) { |
| 124 | data += 4 - tail; |
| 125 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 126 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 127 | len = ntohl(len_ptr[1]); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 128 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 129 | } else { |
| 130 | /* |
| 131 | * no initrd image |
| 132 | */ |
| 133 | data = 0; |
| 134 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 135 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 136 | #ifdef DEBUG |
| 137 | if (!data) { |
| 138 | printf ("No initrd\n"); |
| 139 | } |
| 140 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 141 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 142 | if (data) { |
| 143 | initrd_start = data; |
| 144 | initrd_end = initrd_start + len; |
| 145 | printf (" Loading Ramdisk to %08lx, end %08lx ... ", |
| 146 | initrd_start, initrd_end); |
| 147 | memmove ((void *)initrd_start, (void *)data, len); |
| 148 | printf ("OK\n"); |
| 149 | } else { |
| 150 | initrd_start = 0; |
| 151 | initrd_end = 0; |
| 152 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 153 | |
Wolfgang Denk | e644670 | 2006-07-21 11:30:18 +0200 | [diff] [blame^] | 154 | /* if multi-part image, we need to advance base ptr */ |
| 155 | if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) { |
| 156 | int i; |
| 157 | for (i=0, addr+=sizeof(int); len_ptr[i++]; addr+=sizeof(int)); |
| 158 | } |
| 159 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 160 | base_ptr = load_zimage((void*)addr + sizeof(image_header_t), ntohl(hdr->ih_size), |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 161 | initrd_start, initrd_end-initrd_start, 0); |
| 162 | |
| 163 | if (NULL == base_ptr) { |
| 164 | printf ("## Kernel loading failed ...\n"); |
| 165 | do_reset(cmdtp, flag, argc, argv); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 166 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 167 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 168 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 169 | #ifdef DEBUG |
| 170 | printf ("## Transferring control to Linux (at address %08x) ...\n", |
| 171 | (u32)base_ptr); |
| 172 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 173 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 174 | /* we assume that the kernel is in place */ |
| 175 | printf("\nStarting kernel ...\n\n"); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 176 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 177 | boot_zimage(base_ptr); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 178 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 179 | } |