Tizen 2.0 Release
[platform/kernel/u-boot.git] / onenand_ipl / board / samsung / universal_c210 / checksum.c
1 /*
2  * Copryright (C) 2010 Samsung Electronics
3  *
4  * Generate the checksum at S5PC210 EVT0
5  *
6  * Kyungmin Park <kyungmin.park@samsung.com>
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <err.h>
15
16 #ifndef SZ_8K
17 #define SZ_8K           0x2000
18 #endif
19 #ifndef SZ_14K
20 #define SZ_14K          0x3800
21 #endif
22
23 #define CHECKSUM_8K     (SZ_8K - 0x4)
24
25 /*
26  * For 2KiB page OneNAND
27  * +------+------+------+------+------+------+------+------+
28  * | 2KiB | 2KiB | 2KiB | 2KiB | 2KiB | 2KiB | 2KiB | 2KiB |
29  * +------+------+------+------+------+------+-----C+------+
30  */
31 #define CHECKSUM_16K    (SZ_14K - 0x4)
32
33 int main(int argc, char *argv[])
34 {
35         int ret;
36         int i, j;
37         char buf;
38         int fd;
39         int fd0;
40         unsigned int sum = 0;
41         struct stat stat;
42         off_t size;
43         unsigned int header[4];
44         int evt;
45
46         if (argc != 3)
47                 evt = 0;
48         else
49                 evt = atoi(argv[2]);
50
51         fd = open(argv[1], O_RDWR);
52
53         if (fd < 0) {
54                 printf("open err: %s\n", argv[1]);
55                 return 1;
56         }
57
58         ret = fstat(fd, &stat);
59         if (ret < 0) {
60                 perror("fstat");
61                 return 1;
62         }
63
64         if (stat.st_size > SZ_8K)
65                 size = CHECKSUM_16K;
66         else
67                 size = CHECKSUM_8K;
68
69         for (i = 0; i < size; i++) {
70                 ret = read(fd, &buf, 1);
71
72                 if (ret < 0) {
73                         perror("read");
74                         return 1;
75                 }
76
77                 sum += (buf & 0xFF);
78
79 #if 0
80                 if (i % 16 == 0)
81                         printf("%04x: ", i);
82                 printf("%02x ", buf & 0xFF);
83                 if (i % 16 == 15)
84                         printf("\n");
85 #endif
86         }
87
88         if (evt == 0)
89                 ret = write(fd, &sum, 4);
90
91         if (ret < 0)
92                 printf("read err: %s\n", ret);
93
94         close(fd);
95
96         printf("checksum = %x\n", sum);
97
98         if (evt == 1) {
99                 fd0 = open("header.bin", O_RDWR | O_CREAT | O_TRUNC,
100                                         S_IRUSR | S_IWUSR);
101
102                 if (fd0 < 0) {
103                         printf("open err: header.bin\n");
104                         return 1;
105                 }
106
107                 if (size == CHECKSUM_16K)
108                         header[0] = 0x4000;
109                 else
110                         header[0] = 0x2000;
111                 header[1] = 0;
112                 header[2] = sum;
113                 header[3] = 0;
114
115                 ret = write(fd0, header, 16);
116                 close(fd0);
117         }
118
119         return 0;
120 }