4 static unsigned flags
= 0;
7 static void vgripe_syserr(int st
, int err
, const char *fmt
, va_list ap
)
8 { vmoan_syserr(err
, fmt
, ap
); if (st
> status
) status
= st
; }
9 static void vgripe(int st
, const char *fmt
, va_list ap
)
10 { vgripe_syserr(st
, 0, fmt
, ap
); }
12 static void gripe_syserr(int st
, int err
, const char *fmt
, ...)
15 va_start(ap
, fmt
); vgripe_syserr(st
, err
, fmt
, ap
); va_end(ap
);
17 PRINTF_LIKE(2, 3) static void gripe(int st
, const char *fmt
, ...)
18 { va_list ap
; va_start(ap
, fmt
); vgripe(st
, fmt
, ap
); va_end(ap
); }
20 static int carefully_read(int fd
, off_t off
,
21 void *p
, size_t sz
, const char *file
)
26 if (lseek(fd
, off
, SEEK_SET
) < 0)
27 { gripe_syserr(2, errno
, "failed to seek in `%s'", file
); return (-1); }
32 { gripe_syserr(2, errno
, "failed to read `%s'", file
); return (-1); }
34 { gripe(2, "unexpected end-of-file reading `%s'", file
); return (-1); }
40 typedef uint_least32_t u32
;
41 #define Pu32 PRIuLEAST32
43 static u32
load32(const unsigned char *p
)
45 return (((u32
)p
[0] << 0) | ((u32
)p
[1] << 8) |
46 ((u32
)p
[2] << 16) | ((u32
)p
[3] << 24));
51 static int check_anchor_header(const unsigned char *b
, secaddr addr
,
52 unsigned f
, const char *file
)
59 gripe(1, "anchor descriptor not found at `%s' sector %"PRIuSEC
"",
64 for (i
= 0, t
= 0; i
< 16; i
++) if (i
!= 4) t
+= b
[i
];
68 gripe(1, "bad anchor descriptor header checksum (%u /= %u) "
69 "at `%s' sector %"PRIuSEC
"", (unsigned)b
[4], t
, file
, addr
);
77 gripe(1, "bad sector number in anchor descriptor "
78 "(%"PRIuSEC
" /= %"PRIuSEC
") in `%s'",
87 static int all_zero_p(const unsigned char *p
, size_t sz
)
96 static void check_img(const char *file
)
99 unsigned char b
[SECTORSZ
], bb
[SECTORSZ
];
105 fd
= open(file
, (flags
&F_FIX
) ? O_RDWR
: O_RDONLY
);
107 { gripe_syserr(2, errno
, "failed to open `%s'", file
); goto end
; }
108 blksz
= SECTORSZ
; volsz
= device_size(fd
, file
, &blksz
);
109 if (SECTORSZ
!= 2048)
110 { gripe(2, "device sector size %d /= 2048", blksz
); goto end
; }
111 if (volsz
%SECTORSZ
) {
112 gripe(2, "bad length for `%s' -- not whole number of sectors", file
);
115 end
= volsz
/SECTORSZ
;
117 if (carefully_read(fd
, 256*SECTORSZ
, b
, SECTORSZ
, file
) ||
118 check_anchor_header(b
, 256, CAHF_GRIPE
| CAHF_FULL
, file
))
121 for (i
= 1; i
< 32768; i
++) {
122 if (carefully_read(fd
, (off_t
)(end
- i
)*SECTORSZ
, bb
, SECTORSZ
, file
))
124 if (bb
[0] || !all_zero_p(bb
, SECTORSZ
)) goto nonzero
;
126 gripe(1, "too many trailing zero sectors: "
127 "couldn't find backup anchor descriptor in `%s'", file
);
132 if (bb
[0] == 2 && !check_anchor_header(bb
, end
- j
, 0, file
))
136 gripe(1, "failed to find backup anchor descriptor in `%s'", file
);
139 if (carefully_read(fd
, (off_t
)(end
- j
)*SECTORSZ
, bb
, SECTORSZ
, file
))
143 gripe(1, "found backup anchor descriptor at sector %"PRIuSEC
" "
144 " = %"PRIuSEC
" from end, = %"PRIuSEC
" from trailing zeros",
145 end
- j
, j
, j
- i
+ 1);
149 if (check_anchor_header(bb
, end
- i
, CAHF_GRIPE
| CAHF_FULL
, file
))
152 if (MEMCMP(b
+ 16, != , bb
+ 16, SECTORSZ
- 16)) {
153 gripe(1, "backup anchor descriptor in sector %"PRIuSEC
" "
154 "doesn't match primary in sector 256 of `%s'",
161 gripe(1, "%u trailing zero sectors in `%s'", i
- 1, file
);
163 if (ftruncate(fd
, (off_t
)(end
- i
+ 1)*SECTORSZ
))
164 gripe_syserr(2, errno
,
165 "failed to truncate `%s' to %"PRIuSEC
" sectors",
168 moan("removed %u trailing zero sectors from `%s'", i
- 1, file
);
173 if (fd
!= -1) close(fd
);
176 static void usage(FILE *fp
)
177 { fprintf(fp
, "usage: %s [-x] IMG ...\n", prog
); }
179 int main(int argc
, char *argv
[])
187 opt
= getopt(argc
, argv
, "hx"); if (opt
< 0) break;
189 case 'h': usage(stdout
); exit(0);
190 case 'x': flags
|= F_FIX
; break;
191 default: f
|= f_bogus
; break;
194 if (optind
>= argc
) f
|= f_bogus
;
195 if (f
&f_bogus
) { usage(stderr
); exit(2); }
197 for (i
= optind
; i
< argc
; i
++) check_img(argv
[i
]);