1/* 2 * This program will take an image and make a 32K page aligned file 3 * from the given source file so that we can write the EDC syndrom 4 * bytes to the DiskOnChip device using the M-Systems BDK. 5 * 6 * $Id: docprep.c,v 1.1 2003/05/23 17:19:27 mpl Exp $ 7 * 8 */ 9#include <sys/types.h> 10#include <sys/stat.h> 11#include <fcntl.h> 12#include <stdlib.h> 13#include <stdio.h> 14#include <string.h> 15#include <stdlib.h> 16#include <stdio.h> 17 18#define KBYTE 1024 19#define BOUNDARY (32*KBYTE) 20 21 22int 23main(int argc, char *argv[]) 24{ 25 int fh; 26 unsigned char* image; 27 unsigned char* padding; 28 29 int totalsize = 0; 30 int pa, npages, padbytes; 31 32 33 if(argc != 3){ 34 35 printf("Usage: %s <infile> <outfile>\n", 36 argv[0]); 37 exit(1); 38 39 } 40 41 fh = open(argv[1],O_RDONLY); 42 if (fh < 0) { 43 perror(argv[1]); 44 } 45 46 totalsize = lseek(fh,0L,SEEK_END); 47 lseek(fh,0L,SEEK_SET); 48 49 image = (unsigned char*)malloc(totalsize); 50 if (image == NULL) { 51 perror("malloc"); 52 exit(1); 53 } 54 55 if (read(fh,image, totalsize) != totalsize) { 56 perror("read"); 57 exit(1); 58 } 59 60 close(fh); 61 62 /* 63 * Now write the output file 64 */ 65 66 fh = open(argv[2],O_RDWR |O_CREAT |O_TRUNC, 67 S_IREAD|S_IWRITE|S_IRGRP|S_IWGRP|S_IROTH); 68 if (fh < 0) { 69 perror(argv[2]); 70 exit(1); 71 } 72 73 if (write(fh,image,totalsize) != totalsize) { 74 perror(argv[2]); 75 exit(1); 76 } 77 78 /* Workaround DiskOnChip (TM) binary partition loader page 79 * write issue. In order for DOC to compute ECC/EDC, you need 80 * to always write a page (32K) of data (minimum). If the 81 * image is not page aligned, we simply write N bytes of zeros 82 * at the end so that the DOC ASIC controller will correctly 83 * compute the ECC syndrome bytes and all will be good. 84 * Note that the PPCBoot loader will never read these bytes, 85 * we just put them there to keep the DOC asic controller happy. 86 * 87 */ 88 89 pa = (totalsize % BOUNDARY) == 0; /* True if 32K aligned */ 90 npages = (totalsize / BOUNDARY) + (totalsize % BOUNDARY > 0); 91 padbytes = (npages*BOUNDARY - totalsize); 92 93 if(!pa){ 94 padding = (char*)malloc(padbytes); 95 memset(padding, 0x0, padbytes); 96 printf("Total Size: %d bytes (%d bytes aligned [%x])," 97 "%d bytes pad)\n", 98 totalsize + padbytes, 99 npages*BOUNDARY, 100 npages*BOUNDARY, 101 padbytes); 102 103 if (write(fh, padding, padbytes) != padbytes) { 104 perror(argv[2]); 105 exit(2); 106 } 107 108 free(padding); 109 totalsize += padbytes; 110 } 111 112 printf("Wrote %d bytes to %s\n",totalsize, argv[2]); 113 114 close(fh); 115 116 exit(0); 117} 118 119