1 /* 2 * Copyright (C) 2011 Samsung Electronics 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 #include <fcntl.h> 11 #include <errno.h> 12 #include <string.h> 13 #include <sys/stat.h> 14 15 #define BUFSIZE (16*1024) 16 #define IMG_SIZE (16*1024) 17 #define SPL_HEADER_SIZE 16 18 #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \ 19 | S_IWGRP | S_IROTH | S_IWOTH) 20 #define SPL_HEADER "S5PC210 HEADER " 21 /* 22 * Requirement: 23 * IROM code reads first 14K bytes from boot device. 24 * It then calculates the checksum of 14K-4 bytes and compare with data at 25 * 14K-4 offset. 26 * 27 * This function takes two filenames: 28 * IN "u-boot-spl.bin" and 29 * OUT "$(BOARD)-spl.bin as filenames. 30 * It reads the "u-boot-spl.bin" in 16K buffer. 31 * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer. 32 * It writes the buffer to "$(BOARD)-spl.bin" file. 33 */ 34 35 int main(int argc, char **argv) 36 { 37 int i, len; 38 unsigned char buffer[BUFSIZE] = {0}; 39 int ifd, ofd; 40 unsigned int checksum = 0, count; 41 42 if (argc != 3) { 43 printf(" %d Wrong number of arguments\n", argc); 44 exit(EXIT_FAILURE); 45 } 46 47 ifd = open(argv[1], O_RDONLY); 48 if (ifd < 0) { 49 fprintf(stderr, "%s: Can't open %s: %s\n", 50 argv[0], argv[1], strerror(errno)); 51 exit(EXIT_FAILURE); 52 } 53 54 ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM); 55 if (ifd < 0) { 56 fprintf(stderr, "%s: Can't open %s: %s\n", 57 argv[0], argv[2], strerror(errno)); 58 if (ifd) 59 close(ifd); 60 exit(EXIT_FAILURE); 61 } 62 63 len = lseek(ifd, 0, SEEK_END); 64 lseek(ifd, 0, SEEK_SET); 65 66 memcpy(&buffer[0], SPL_HEADER, SPL_HEADER_SIZE); 67 68 count = (len < (IMG_SIZE - SPL_HEADER_SIZE)) 69 ? len : (IMG_SIZE - SPL_HEADER_SIZE); 70 71 if (read(ifd, buffer + SPL_HEADER_SIZE, count) != count) { 72 fprintf(stderr, "%s: Can't read %s: %s\n", 73 argv[0], argv[1], strerror(errno)); 74 75 if (ifd) 76 close(ifd); 77 if (ofd) 78 close(ofd); 79 80 exit(EXIT_FAILURE); 81 } 82 83 for (i = 0; i < IMG_SIZE - SPL_HEADER_SIZE; i++) 84 checksum += buffer[i+16]; 85 86 *(ulong *)buffer ^= 0x1f; 87 *(ulong *)(buffer+4) ^= checksum; 88 89 for (i = 1; i < SPL_HEADER_SIZE; i++) 90 buffer[i] ^= buffer[i-1]; 91 92 if (write(ofd, buffer, BUFSIZE) != BUFSIZE) { 93 fprintf(stderr, "%s: Can't write %s: %s\n", 94 argv[0], argv[2], strerror(errno)); 95 96 if (ifd) 97 close(ifd); 98 if (ofd) 99 close(ofd); 100 101 exit(EXIT_FAILURE); 102 } 103 104 if (ifd) 105 close(ifd); 106 if (ofd) 107 close(ofd); 108 109 return EXIT_SUCCESS; 110 } 111