tizen 2.4 release
[kernel/u-boot-tm1.git] / nand_fdl / fdl-2 / src / img2data.c
1 /*
2  *   gcc img2data.c -o img2data
3  *   make_ext4fs -s -l 5M -a productinfo /home/apuser/productinfo.img
4  *   ./img2data /home/apuser/productinfo.img ../inc/phasecheck.h
5  */
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <signal.h>
15 #include <sys/ioctl.h>
16 #include <string.h>
17 #include <mtd/mtd-user.h>
18
19 #define BUFFER_LEN      (512)
20 char gbuffer[BUFFER_LEN];
21
22 int main(int argc, char **argv)
23 {
24         const char *filename, *outname;
25         int fd;
26         FILE *fp;
27         struct stat stat_buf;   
28         int total, ii, recv, valdata;
29         unsigned long offset;
30
31         if (argc != 3) {
32                 printf("example : ./img2data /home/apuser/productinfo.img ../inc/myphasecheck.h\n");
33                 return 0;
34         }
35
36         filename = argv[1];
37         outname = argv[2];
38         memset(&stat_buf, 0, sizeof(struct stat));
39         if (access(filename, 0) == 0)
40                 stat(filename, &stat_buf);
41         else {
42                 stat_buf.st_size = 0;
43                 return 0;
44         }
45
46         fd = open(filename, O_RDWR);
47         if (fd < 0)
48                 printf("open input file : %s error\n", filename);
49         fp = fopen(outname, "w");
50         if (fp == NULL) {
51                 close(fd);
52                 printf("open output file : %s error\n", filename);
53                 return 0;
54         }
55
56         /* output header */
57         fprintf(fp, "\nstruct ext4_off_data {\n");
58         fprintf(fp, "      unsigned long off;\n");
59         fprintf(fp, "      unsigned char data;\n");
60         fprintf(fp, "};\n\n");
61         fprintf(fp, "struct ext4_off_data ext4_pattern[] = {\n");
62
63         total = stat_buf.st_size;
64         recv = 0;
65         valdata = 0;
66         while (total > 0) {
67                 memset(gbuffer, 0, BUFFER_LEN);
68                 read(fd, gbuffer, BUFFER_LEN);
69                 for (ii = 0; ii < BUFFER_LEN; ii ++) {
70                         if (gbuffer[ii] != 0) {
71                                 //printf("{0x%08x, 0x%02x},", (recv + ii), (gbuffer[ii] & 0xff));
72                                 fprintf(fp, "{0x%08x, 0x%02x},", (recv + ii), (gbuffer[ii] & 0xff));
73                                 valdata ++;
74                         }
75                 }
76                 total -= BUFFER_LEN;
77                 recv += BUFFER_LEN;
78         }
79         
80         offset = ftell(fp);
81         //printf("\nvaldata = %d  offset = %ld\n", valdata, offset);
82         fseek(fp, offset - 1,SEEK_SET);
83         fprintf(fp, "\n};\n\n");
84         close(fd);
85         fclose(fp);
86         
87         return 1;
88 }