tizen 2.3.1 release
[platform/kernel/u-boot.git] / onenand_ipl / board / samsung / universal_c100 / checksum.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <err.h>
8
9 #ifndef SZ_8K
10 #define SZ_8K           0x2000
11 #endif
12 #ifndef SZ_14K
13 #define SZ_14K          0x3800
14 #endif
15
16 #define PAGE_SIZE       0x800
17 /*
18  * For 2KiB page OneNAND
19  * +------+------+------+------+
20  * | 2KiB | 2KiB | 2KiB | 2KiB |
21  * +------+------+------+------+
22  */
23 #define CHECKSUM_8K     (SZ_8K - 0x4)
24 /*
25  * For 4KiB page OneNAND
26  * +----------------+----------------+----------------+----------------+
27  * | 2KiB, reserved | 2KiB, reserved | 2KiB, reserved | 2KiB, reserved |
28  * +----------------+----------------+----------------+----------------+
29  */
30 #define CHECKSUM_16K    (SZ_14K - 0x4)
31
32 int main(int argc, char *argv[])
33 {
34         int ret;
35         int i, j;
36         char buf;
37         int fd;
38         int fd0;
39         unsigned int sum = 0;
40         struct stat stat;
41         off_t size;
42         unsigned int header[4];
43         int evt;
44
45         if (argc != 3)
46                 evt = 0;
47         else
48                 evt = atoi(argv[2]);
49
50         fd = open(argv[1], O_RDWR);
51
52         if (fd < 0) {
53                 printf("open err: %s\n", argv[1]);
54                 return 1;
55         }
56
57         ret = fstat(fd, &stat);
58         if (ret < 0) {
59                 perror("fstat");
60                 return 1;
61         }
62
63         if (stat.st_size > SZ_8K)
64                 size = CHECKSUM_16K;
65         else
66                 size = CHECKSUM_8K;
67
68         for (i = 0; i < size; i++) {
69                 /* evt1 doesn't have reserved area */
70                 if (evt == 0) {
71                         /* if reserved area, skip the checksum */
72                         if (stat.st_size > SZ_8K) {
73                                 if (i % PAGE_SIZE == 0) {
74                                         int res;
75                                         char tbuf[PAGE_SIZE];
76
77                                         res = i / PAGE_SIZE;
78                                         if (res % 2) {
79                                                         i += PAGE_SIZE;
80                                                 /*
81                                                  * if 1st page's reverved area,
82                                                  * copy the data to 2nd page
83                                                  */
84                                                 if (res == 1) {
85                                                         ret = read(fd, &tbuf,
86                                                                 PAGE_SIZE);
87                                                         ret = write(fd, tbuf,
88                                                                 PAGE_SIZE);
89                                                 }
90                                                 lseek(fd, i, SEEK_SET);
91                                         }
92                                 }
93                         }
94                 }
95
96                 ret = read(fd, &buf, 1);
97
98                 if (ret < 0) {
99                         perror("read");
100                         return 1;
101                 }
102
103                 sum += (buf & 0xFF);
104
105 #if 0
106                 if (i % 16 == 0)
107                         printf("%04x: ", i);
108                 printf("%02x ", buf & 0xFF);
109                 if (i % 16 == 15)
110                         printf("\n");
111 #endif
112         }
113
114         if (evt == 0)
115                 ret = write(fd, &sum, 4);
116
117         if (ret < 0)
118                 printf("read err: %s\n", ret);
119
120         close(fd);
121
122         printf("checksum = %x\n", sum);
123
124         if (evt == 1) {
125                 fd0 = open("header.bin", O_RDWR | O_CREAT | O_TRUNC,
126                                         S_IRUSR | S_IWUSR);
127
128                 if (fd0 < 0) {
129                         printf("open err: header.bin\n");
130                         return 1;
131                 }
132
133                 if (size == CHECKSUM_16K)
134                         header[0] = 0x4000;
135                 else
136                         header[0] = 0x2000;
137                 header[1] = 0;
138                 header[2] = sum;
139                 header[3] = 0;
140
141                 ret = write(fd0, header, 16);
142                 close(fd0);
143         }
144
145         return 0;
146 }