bellard | 5fe141f | 2006-04-23 17:12:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Executable loader |
| 3 | * |
| 4 | * Copyright (c) 2006 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | #include "vl.h" |
| 25 | #include "disas.h" |
pbrook | 1c7b375 | 2007-03-06 23:52:01 +0000 | [diff] [blame] | 26 | #include "uboot_image.h" |
bellard | 5fe141f | 2006-04-23 17:12:42 +0000 | [diff] [blame] | 27 | |
| 28 | /* return the size or -1 if error */ |
| 29 | int get_image_size(const char *filename) |
| 30 | { |
| 31 | int fd, size; |
| 32 | fd = open(filename, O_RDONLY | O_BINARY); |
| 33 | if (fd < 0) |
| 34 | return -1; |
| 35 | size = lseek(fd, 0, SEEK_END); |
| 36 | close(fd); |
| 37 | return size; |
| 38 | } |
| 39 | |
| 40 | /* return the size or -1 if error */ |
| 41 | int load_image(const char *filename, uint8_t *addr) |
| 42 | { |
| 43 | int fd, size; |
| 44 | fd = open(filename, O_RDONLY | O_BINARY); |
| 45 | if (fd < 0) |
| 46 | return -1; |
| 47 | size = lseek(fd, 0, SEEK_END); |
| 48 | lseek(fd, 0, SEEK_SET); |
| 49 | if (read(fd, addr, size) != size) { |
| 50 | close(fd); |
| 51 | return -1; |
| 52 | } |
| 53 | close(fd); |
| 54 | return size; |
| 55 | } |
| 56 | |
| 57 | /* A.OUT loader */ |
| 58 | |
| 59 | struct exec |
| 60 | { |
| 61 | uint32_t a_info; /* Use macros N_MAGIC, etc for access */ |
| 62 | uint32_t a_text; /* length of text, in bytes */ |
| 63 | uint32_t a_data; /* length of data, in bytes */ |
| 64 | uint32_t a_bss; /* length of uninitialized data area, in bytes */ |
| 65 | uint32_t a_syms; /* length of symbol table data in file, in bytes */ |
| 66 | uint32_t a_entry; /* start address */ |
| 67 | uint32_t a_trsize; /* length of relocation info for text, in bytes */ |
| 68 | uint32_t a_drsize; /* length of relocation info for data, in bytes */ |
| 69 | }; |
| 70 | |
| 71 | #ifdef BSWAP_NEEDED |
| 72 | static void bswap_ahdr(struct exec *e) |
| 73 | { |
| 74 | bswap32s(&e->a_info); |
| 75 | bswap32s(&e->a_text); |
| 76 | bswap32s(&e->a_data); |
| 77 | bswap32s(&e->a_bss); |
| 78 | bswap32s(&e->a_syms); |
| 79 | bswap32s(&e->a_entry); |
| 80 | bswap32s(&e->a_trsize); |
| 81 | bswap32s(&e->a_drsize); |
| 82 | } |
| 83 | #else |
| 84 | #define bswap_ahdr(x) do { } while (0) |
| 85 | #endif |
| 86 | |
| 87 | #define N_MAGIC(exec) ((exec).a_info & 0xffff) |
| 88 | #define OMAGIC 0407 |
| 89 | #define NMAGIC 0410 |
| 90 | #define ZMAGIC 0413 |
| 91 | #define QMAGIC 0314 |
| 92 | #define _N_HDROFF(x) (1024 - sizeof (struct exec)) |
| 93 | #define N_TXTOFF(x) \ |
| 94 | (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \ |
| 95 | (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec))) |
| 96 | #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0) |
| 97 | #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text) |
| 98 | #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1)) |
| 99 | |
| 100 | #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text) |
| 101 | |
| 102 | #define N_DATADDR(x) \ |
| 103 | (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \ |
| 104 | : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x)))) |
| 105 | |
| 106 | |
| 107 | int load_aout(const char *filename, uint8_t *addr) |
| 108 | { |
| 109 | int fd, size, ret; |
| 110 | struct exec e; |
| 111 | uint32_t magic; |
| 112 | |
| 113 | fd = open(filename, O_RDONLY | O_BINARY); |
| 114 | if (fd < 0) |
| 115 | return -1; |
| 116 | |
| 117 | size = read(fd, &e, sizeof(e)); |
| 118 | if (size < 0) |
| 119 | goto fail; |
| 120 | |
| 121 | bswap_ahdr(&e); |
| 122 | |
| 123 | magic = N_MAGIC(e); |
| 124 | switch (magic) { |
| 125 | case ZMAGIC: |
| 126 | case QMAGIC: |
| 127 | case OMAGIC: |
| 128 | lseek(fd, N_TXTOFF(e), SEEK_SET); |
| 129 | size = read(fd, addr, e.a_text + e.a_data); |
| 130 | if (size < 0) |
| 131 | goto fail; |
| 132 | break; |
| 133 | case NMAGIC: |
| 134 | lseek(fd, N_TXTOFF(e), SEEK_SET); |
| 135 | size = read(fd, addr, e.a_text); |
| 136 | if (size < 0) |
| 137 | goto fail; |
| 138 | ret = read(fd, addr + N_DATADDR(e), e.a_data); |
| 139 | if (ret < 0) |
| 140 | goto fail; |
| 141 | size += ret; |
| 142 | break; |
| 143 | default: |
| 144 | goto fail; |
| 145 | } |
| 146 | close(fd); |
| 147 | return size; |
| 148 | fail: |
| 149 | close(fd); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | /* ELF loader */ |
| 154 | |
| 155 | static void *load_at(int fd, int offset, int size) |
| 156 | { |
| 157 | void *ptr; |
| 158 | if (lseek(fd, offset, SEEK_SET) < 0) |
| 159 | return NULL; |
| 160 | ptr = qemu_malloc(size); |
| 161 | if (!ptr) |
| 162 | return NULL; |
| 163 | if (read(fd, ptr, size) != size) { |
| 164 | qemu_free(ptr); |
| 165 | return NULL; |
| 166 | } |
| 167 | return ptr; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | #define ELF_CLASS ELFCLASS32 |
| 172 | #include "elf.h" |
| 173 | |
| 174 | #define SZ 32 |
| 175 | #define elf_word uint32_t |
| 176 | #define bswapSZs bswap32s |
| 177 | #include "elf_ops.h" |
| 178 | |
| 179 | #undef elfhdr |
| 180 | #undef elf_phdr |
| 181 | #undef elf_shdr |
| 182 | #undef elf_sym |
| 183 | #undef elf_note |
| 184 | #undef elf_word |
| 185 | #undef bswapSZs |
| 186 | #undef SZ |
| 187 | #define elfhdr elf64_hdr |
| 188 | #define elf_phdr elf64_phdr |
| 189 | #define elf_note elf64_note |
| 190 | #define elf_shdr elf64_shdr |
| 191 | #define elf_sym elf64_sym |
| 192 | #define elf_word uint64_t |
| 193 | #define bswapSZs bswap64s |
| 194 | #define SZ 64 |
| 195 | #include "elf_ops.h" |
| 196 | |
| 197 | /* return < 0 if error, otherwise the number of bytes loaded in memory */ |
bellard | 9ee3c02 | 2006-04-26 22:05:26 +0000 | [diff] [blame] | 198 | int load_elf(const char *filename, int64_t virt_to_phys_addend, |
| 199 | uint64_t *pentry) |
bellard | 5fe141f | 2006-04-23 17:12:42 +0000 | [diff] [blame] | 200 | { |
ths | 9042c0e | 2006-12-23 14:18:40 +0000 | [diff] [blame] | 201 | int fd, data_order, host_data_order, must_swab, ret; |
bellard | 5fe141f | 2006-04-23 17:12:42 +0000 | [diff] [blame] | 202 | uint8_t e_ident[EI_NIDENT]; |
| 203 | |
bellard | 699e464 | 2006-05-07 18:06:27 +0000 | [diff] [blame] | 204 | fd = open(filename, O_RDONLY | O_BINARY); |
bellard | 5fe141f | 2006-04-23 17:12:42 +0000 | [diff] [blame] | 205 | if (fd < 0) { |
| 206 | perror(filename); |
| 207 | return -1; |
| 208 | } |
| 209 | if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) |
| 210 | goto fail; |
| 211 | if (e_ident[0] != ELFMAG0 || |
| 212 | e_ident[1] != ELFMAG1 || |
| 213 | e_ident[2] != ELFMAG2 || |
| 214 | e_ident[3] != ELFMAG3) |
| 215 | goto fail; |
| 216 | #ifdef WORDS_BIGENDIAN |
| 217 | data_order = ELFDATA2MSB; |
| 218 | #else |
| 219 | data_order = ELFDATA2LSB; |
| 220 | #endif |
| 221 | must_swab = data_order != e_ident[EI_DATA]; |
ths | 9042c0e | 2006-12-23 14:18:40 +0000 | [diff] [blame] | 222 | |
| 223 | #ifdef TARGET_WORDS_BIGENDIAN |
| 224 | host_data_order = ELFDATA2MSB; |
| 225 | #else |
| 226 | host_data_order = ELFDATA2LSB; |
| 227 | #endif |
| 228 | if (host_data_order != e_ident[EI_DATA]) |
| 229 | return -1; |
| 230 | |
bellard | 5fe141f | 2006-04-23 17:12:42 +0000 | [diff] [blame] | 231 | lseek(fd, 0, SEEK_SET); |
| 232 | if (e_ident[EI_CLASS] == ELFCLASS64) { |
bellard | 9ee3c02 | 2006-04-26 22:05:26 +0000 | [diff] [blame] | 233 | ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry); |
bellard | 5fe141f | 2006-04-23 17:12:42 +0000 | [diff] [blame] | 234 | } else { |
bellard | 9ee3c02 | 2006-04-26 22:05:26 +0000 | [diff] [blame] | 235 | ret = load_elf32(fd, virt_to_phys_addend, must_swab, pentry); |
bellard | 5fe141f | 2006-04-23 17:12:42 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | close(fd); |
| 239 | return ret; |
| 240 | |
| 241 | fail: |
| 242 | close(fd); |
| 243 | return -1; |
| 244 | } |
pbrook | 1c7b375 | 2007-03-06 23:52:01 +0000 | [diff] [blame] | 245 | |
| 246 | static void bswap_uboot_header(uboot_image_header_t *hdr) |
| 247 | { |
| 248 | #ifndef WORDS_BIGENDIAN |
| 249 | bswap32s(&hdr->ih_magic); |
| 250 | bswap32s(&hdr->ih_hcrc); |
| 251 | bswap32s(&hdr->ih_time); |
| 252 | bswap32s(&hdr->ih_size); |
| 253 | bswap32s(&hdr->ih_load); |
| 254 | bswap32s(&hdr->ih_ep); |
| 255 | bswap32s(&hdr->ih_dcrc); |
| 256 | #endif |
| 257 | } |
| 258 | |
| 259 | /* Load a U-Boot image. */ |
| 260 | int load_uboot(const char *filename, target_ulong *ep, int *is_linux) |
| 261 | { |
| 262 | |
| 263 | int fd; |
| 264 | int size; |
| 265 | uboot_image_header_t h; |
| 266 | uboot_image_header_t *hdr = &h; |
| 267 | uint8_t *data = NULL; |
| 268 | |
| 269 | fd = open(filename, O_RDONLY | O_BINARY); |
| 270 | if (fd < 0) |
| 271 | return -1; |
| 272 | |
| 273 | size = read(fd, hdr, sizeof(uboot_image_header_t)); |
| 274 | if (size < 0) |
| 275 | goto fail; |
| 276 | |
| 277 | bswap_uboot_header(hdr); |
| 278 | |
| 279 | if (hdr->ih_magic != IH_MAGIC) |
| 280 | goto fail; |
| 281 | |
| 282 | /* TODO: Implement Multi-File images. */ |
| 283 | if (hdr->ih_type == IH_TYPE_MULTI) { |
| 284 | fprintf(stderr, "Unable to load multi-file u-boot images\n"); |
| 285 | goto fail; |
| 286 | } |
| 287 | |
| 288 | /* TODO: Implement compressed images. */ |
| 289 | if (hdr->ih_comp != IH_COMP_NONE) { |
| 290 | fprintf(stderr, "Unable to load compressed u-boot images\n"); |
| 291 | goto fail; |
| 292 | } |
| 293 | |
| 294 | /* TODO: Check CPU type. */ |
| 295 | if (is_linux) { |
| 296 | if (hdr->ih_type == IH_TYPE_KERNEL && hdr->ih_os == IH_OS_LINUX) |
| 297 | *is_linux = 1; |
| 298 | else |
| 299 | *is_linux = 0; |
| 300 | } |
| 301 | |
| 302 | *ep = hdr->ih_ep; |
| 303 | data = qemu_malloc(hdr->ih_size); |
| 304 | if (!data) |
| 305 | goto fail; |
| 306 | |
| 307 | if (read(fd, data, hdr->ih_size) != hdr->ih_size) { |
| 308 | fprintf(stderr, "Error reading file\n"); |
| 309 | goto fail; |
| 310 | } |
| 311 | |
| 312 | cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size); |
| 313 | |
| 314 | return hdr->ih_size; |
| 315 | |
| 316 | fail: |
| 317 | if (data) |
| 318 | qemu_free(data); |
| 319 | close(fd); |
| 320 | return -1; |
| 321 | } |