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 open_dvd(file, (flags&F_FIX) ? O_RDWR : O_RDONLY, &fd, 0);
106 blksz = SECTORSZ; volsz = device_size(fd, file, &blksz);
107 if (SECTORSZ != 2048)
108 { gripe(2, "device sector size %d /= 2048", blksz); goto end; }
109 if (volsz%SECTORSZ) {
110 gripe(2, "bad length for `%s' -- not whole number of sectors", file);
113 end = volsz/SECTORSZ;
115 if (carefully_read(fd, 256*SECTORSZ, b, SECTORSZ, file) ||
116 check_anchor_header(b, 256, CAHF_GRIPE | CAHF_FULL, file))
119 for (i = 1; i < 32768; i++) {
120 if (carefully_read(fd, (off_t)(end - i)*SECTORSZ, bb, SECTORSZ, file))
122 if (bb[0] || !all_zero_p(bb, SECTORSZ)) goto nonzero;
124 gripe(1, "too many trailing zero sectors: "
125 "couldn't find backup anchor descriptor in `%s'", file);
130 if (bb[0] == 2 && !check_anchor_header(bb, end - j, 0, file))
134 gripe(1, "failed to find backup anchor descriptor in `%s'", file);
137 if (carefully_read(fd, (off_t)(end - j)*SECTORSZ, bb, SECTORSZ, file))
141 gripe(1, "found backup anchor descriptor at sector %"PRIuSEC" "
142 " = %"PRIuSEC" from end, = %"PRIuSEC" from trailing zeros",
143 end - j, j, j - i + 1);
147 if (check_anchor_header(bb, end - i, CAHF_GRIPE | CAHF_FULL, file))
150 if (MEMCMP(b + 16, != , bb + 16, SECTORSZ - 16)) {
151 gripe(1, "backup anchor descriptor in sector %"PRIuSEC" "
152 "doesn't match primary in sector 256 of `%s'",
159 gripe(1, "%u trailing zero sectors in `%s'", i - 1, file);
161 if (ftruncate(fd, (off_t)(end - i + 1)*SECTORSZ))
162 gripe_syserr(2, errno,
163 "failed to truncate `%s' to %"PRIuSEC" sectors",
166 moan("removed %u trailing zero sectors from `%s'", i - 1, file);
171 if (fd != -1) close(fd);
174 static void usage(FILE *fp)
175 { fprintf(fp, "usage: %s [-x] IMG ...\n", prog); }
177 int main(int argc, char *argv[])
185 opt = getopt(argc, argv, "hx"); if (opt < 0) break;
187 case 'h': usage(stdout); exit(0);
188 case 'x': flags |= F_FIX; break;
189 default: f |= f_bogus; break;
192 if (optind >= argc) f |= f_bogus;
193 if (f&f_bogus) { usage(stderr); exit(2); }
194 setlocale(LC_ALL, "");
195 progress_init(&progress);
196 for (i = optind; i < argc; i++) check_img(argv[i]);
197 progress_free(&progress);