Chander Kashyap | 488ef1a | 2011-08-18 22:37:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Samsung Electronics |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/stat.h> |
| 30 | |
| 31 | #define BUFSIZE (16*1024) |
| 32 | #define IMG_SIZE (16*1024) |
| 33 | #define SPL_HEADER_SIZE 16 |
| 34 | #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \ |
| 35 | | S_IWGRP | S_IROTH | S_IWOTH) |
| 36 | #define SPL_HEADER "S5PC210 HEADER " |
| 37 | /* |
| 38 | * Requirement: |
| 39 | * IROM code reads first 14K bytes from boot device. |
| 40 | * It then calculates the checksum of 14K-4 bytes and compare with data at |
| 41 | * 14K-4 offset. |
| 42 | * |
| 43 | * This function takes two filenames: |
| 44 | * IN "u-boot-spl.bin" and |
| 45 | * OUT "$(BOARD)-spl.bin as filenames. |
| 46 | * It reads the "u-boot-spl.bin" in 16K buffer. |
| 47 | * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer. |
| 48 | * It writes the buffer to "$(BOARD)-spl.bin" file. |
| 49 | */ |
| 50 | |
| 51 | int main(int argc, char **argv) |
| 52 | { |
| 53 | int i, len; |
| 54 | unsigned char buffer[BUFSIZE] = {0}; |
| 55 | int ifd, ofd; |
| 56 | unsigned int checksum = 0, count; |
| 57 | |
| 58 | if (argc != 3) { |
| 59 | printf(" %d Wrong number of arguments\n", argc); |
| 60 | exit(EXIT_FAILURE); |
| 61 | } |
| 62 | |
| 63 | ifd = open(argv[1], O_RDONLY); |
| 64 | if (ifd < 0) { |
| 65 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 66 | argv[0], argv[1], strerror(errno)); |
| 67 | exit(EXIT_FAILURE); |
| 68 | } |
| 69 | |
| 70 | ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM); |
| 71 | if (ifd < 0) { |
| 72 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 73 | argv[0], argv[2], strerror(errno)); |
| 74 | if (ifd) |
| 75 | close(ifd); |
| 76 | exit(EXIT_FAILURE); |
| 77 | } |
| 78 | |
| 79 | len = lseek(ifd, 0, SEEK_END); |
| 80 | lseek(ifd, 0, SEEK_SET); |
| 81 | |
| 82 | memcpy(&buffer[0], SPL_HEADER, SPL_HEADER_SIZE); |
| 83 | |
| 84 | count = (len < (IMG_SIZE - SPL_HEADER_SIZE)) |
| 85 | ? len : (IMG_SIZE - SPL_HEADER_SIZE); |
| 86 | |
| 87 | if (read(ifd, buffer + SPL_HEADER_SIZE, count) != count) { |
| 88 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 89 | argv[0], argv[1], strerror(errno)); |
| 90 | |
| 91 | if (ifd) |
| 92 | close(ifd); |
| 93 | if (ofd) |
| 94 | close(ofd); |
| 95 | |
| 96 | exit(EXIT_FAILURE); |
| 97 | } |
| 98 | |
| 99 | for (i = 0; i < IMG_SIZE - SPL_HEADER_SIZE; i++) |
| 100 | checksum += buffer[i+16]; |
| 101 | |
| 102 | *(ulong *)buffer ^= 0x1f; |
| 103 | *(ulong *)(buffer+4) ^= checksum; |
| 104 | |
| 105 | for (i = 1; i < SPL_HEADER_SIZE; i++) |
| 106 | buffer[i] ^= buffer[i-1]; |
| 107 | |
| 108 | if (write(ofd, buffer, BUFSIZE) != BUFSIZE) { |
| 109 | fprintf(stderr, "%s: Can't write %s: %s\n", |
| 110 | argv[0], argv[2], strerror(errno)); |
| 111 | |
| 112 | if (ifd) |
| 113 | close(ifd); |
| 114 | if (ofd) |
| 115 | close(ofd); |
| 116 | |
| 117 | exit(EXIT_FAILURE); |
| 118 | } |
| 119 | |
| 120 | if (ifd) |
| 121 | close(ifd); |
| 122 | if (ofd) |
| 123 | close(ofd); |
| 124 | |
| 125 | return EXIT_SUCCESS; |
| 126 | } |