Subbaraman Narayanamurthy | 2a8fd54 | 2010-10-27 15:45:18 -0700 | [diff] [blame^] | 1 | /* Copyright 2007, Google Inc. */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <unistd.h> |
| 6 | #include <fcntl.h> |
| 7 | |
| 8 | #include <sys/stat.h> |
| 9 | |
| 10 | int main(int argc, char *argv[]) |
| 11 | { |
| 12 | struct stat s; |
| 13 | unsigned size, base; |
| 14 | int unified_boot = 0; |
| 15 | unsigned unified_boot_magic[20]; |
| 16 | unsigned non_unified_boot_magic[10]; |
| 17 | unsigned magic_len = 0; |
| 18 | unsigned *magic; |
| 19 | int fd; |
| 20 | |
| 21 | if(argc < 3) { |
| 22 | fprintf(stderr,"usage: mkheader <bin> <hdr>\n"); |
| 23 | return -1; |
| 24 | } |
| 25 | |
| 26 | if (argc == 4) { |
| 27 | if(!strcmp("unified-boot",argv[3])) { |
| 28 | unified_boot = 1; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if(stat(argv[1], &s)) { |
| 33 | perror("cannot stat binary"); |
| 34 | return -1; |
| 35 | } |
| 36 | |
| 37 | if(unified_boot) { |
| 38 | magic = unified_boot_magic; |
| 39 | magic_len = sizeof(unified_boot_magic); |
| 40 | } else { |
| 41 | magic = non_unified_boot_magic; |
| 42 | magic_len = sizeof(non_unified_boot_magic); |
| 43 | } |
| 44 | |
| 45 | size = s.st_size; |
| 46 | #if MEMBASE |
| 47 | base = MEMBASE; |
| 48 | #else |
| 49 | base = 0; |
| 50 | #endif |
| 51 | |
| 52 | magic[0] = 0x00000005; /* appsbl */ |
| 53 | magic[1] = 0x00000002; /* nand */ |
| 54 | magic[2] = 0x00000000; |
| 55 | magic[3] = base; |
| 56 | magic[4] = size; |
| 57 | magic[5] = size; |
| 58 | magic[6] = size + base; |
| 59 | magic[7] = 0x00000000; |
| 60 | magic[8] = size + base; |
| 61 | magic[9] = 0x00000000; |
| 62 | |
| 63 | if (unified_boot == 1) |
| 64 | { |
| 65 | magic[10] = 0x33836685; /* cookie magic number */ |
| 66 | magic[11] = 0x00000001; /* cookie version */ |
| 67 | magic[12] = 0x00000002; /* file formats */ |
| 68 | magic[13] = 0x00000000; |
| 69 | magic[14] = 0x00500000; /* 5M for boot.img */ |
| 70 | magic[15] = 0x00000000; |
| 71 | magic[16] = 0x00000000; |
| 72 | magic[17] = 0x00000000; |
| 73 | magic[18] = 0x00000000; |
| 74 | magic[19] = 0x00000000; |
| 75 | } |
| 76 | |
| 77 | fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 78 | if(fd < 0) { |
| 79 | perror("cannot open header for writing"); |
| 80 | return -1; |
| 81 | } |
| 82 | if(write(fd, magic, magic_len) != magic_len) { |
| 83 | perror("cannot write header"); |
| 84 | close(fd); |
| 85 | unlink(argv[2]); |
| 86 | return -1; |
| 87 | } |
| 88 | close(fd); |
| 89 | |
| 90 | return 0; |
| 91 | } |