1 /* $NetBSD: iplsum.c,v 1.3 2009/03/14 21:04:09 dsl Exp $ */
4 * Calculate 32bit checksum of IPL and store in a certain location
6 * Written in 2003 by ITOH Yasufumi (itohy@NetBSD.org).
10 #include <sys/types.h>
13 #include <netinet/in.h>
15 #ifndef __BIT_TYPES_DEFINED__
16 typedef unsigned int u_int32_t
;
20 #define IPLOFF (4*1024) /* 4KB */
21 #define IPL1SIZE (4*1024) /* 4KB */
22 #define IPL2SIZE (1*1024) /* 1KB */
23 #define IPL2ONDISK 0x0400
24 #define IPL3SIZE (3*512) /* 1.5KB */
25 #define IPL3ONDISK 0x0A00
26 #define IPLSIZE (IPL1SIZE + IPL2SIZE + IPL3SIZE)
27 #define BOOTSIZE (IPLOFF + IPLSIZE)
28 #define BOOTBLOCKSIZE 8192
30 u_int32_t bootblk
[BOOTSIZE
/ sizeof(u_int32_t
) + 1];
32 #define SUMOFF ((IPLOFF + 4) / sizeof(u_int32_t))
35 int main(int, char *[]);
39 main(int argc
, char *argv
[])
44 int iploff
, iplsumsize
;
47 fprintf(stderr
, "usage: %s <input> <output>\n", argv
[0]);
52 if ((fp
= fopen(argv
[1], "rb")) == NULL
) {
56 if ((len
= fread(bootblk
, 1, sizeof bootblk
, fp
)) <= IPLOFF
) {
57 fprintf(stderr
, "%s: too short\n", argv
[1]);
59 } else if (len
> BOOTSIZE
) {
60 fprintf(stderr
, "%s: too long\n", argv
[1]);
66 if ((ntohl(bootblk
[0]) & 0xffff0000) != 0x80000000) {
67 fprintf(stderr
, "%s: bad LIF magic\n", argv
[1]);
70 iploff
= ntohl(bootblk
[0xf0 / sizeof(u_int32_t
)]);
71 iplsumsize
= ntohl(bootblk
[0xf4 / sizeof(u_int32_t
)]);
72 printf("%d bytes free, ipl offset = %d, ipl sum size = %d\n",
73 BOOTSIZE
- len
, iploff
, iplsumsize
);
74 if (iploff
!= IPLOFF
|| iplsumsize
<= 0 || iplsumsize
% 2048 ||
75 iploff
+ iplsumsize
> BOOTBLOCKSIZE
) {
76 fprintf(stderr
, "%s: bad ipl offset / size\n", argv
[1]);
82 for (p
= bootblk
+ IPLOFF
/ sizeof(u_int32_t
);
83 p
< bootblk
+ (IPLOFF
+ IPL1SIZE
) / sizeof(u_int32_t
); p
++)
86 bootblk
[SUMOFF
] = htonl(ntohl(bootblk
[SUMOFF
]) - sum
);
88 /* transfer ipl part 2 */
89 memcpy(bootblk
+ IPL2ONDISK
/ sizeof(u_int32_t
),
90 bootblk
+ (IPLOFF
+ IPL1SIZE
) / sizeof(u_int32_t
),
93 /* transfer ipl part 3 */
94 memcpy(bootblk
+ IPL3ONDISK
/ sizeof(u_int32_t
),
95 bootblk
+ (IPLOFF
+ IPL1SIZE
+ IPL2SIZE
) / sizeof(u_int32_t
),
99 if ((fp
= fopen(argv
[2], "wb")) == NULL
) {
103 if ((len
= fwrite(bootblk
, 1, BOOTBLOCKSIZE
, fp
)) != BOOTBLOCKSIZE
) {
107 fprintf(stderr
, "%s: short write\n", argv
[2]);
109 (void) remove(argv
[2]);
114 (void) remove(argv
[2]);