1 /* common.c - Common data structures and functions
3 * Copyright 1992-1998 Werner Almesberger
4 * Copyright 1999-2005 John Coffman
5 * Copyright 2009-2015 Joachim Wiedorn
8 * Licensed under the terms contained in the file 'COPYING'
9 * in the source directory.
24 char *identify = NULL;
25 int boot_dev_nr, raid_index, do_md_install;
26 int verbose = 0, test = 0, compact = 0, linear = 0, raid_flags = 0, zflag = 0,
27 ireloc = 0, force_fs = 0, force_raid = 0, extended_pt = 0, query = 0,
28 nowarn = 0, lba32 = 0, autoauto = 0, passw = 0, geometric = 0, eflag = 0;
29 int colormax = 15, warnings = 0;
30 DL_BIOS bios_passes_dl = DL_NOT_SET;
34 int bios_boot, bios_map;
35 unsigned short drv_map[DRVMAP_SIZE+1]; /* fixup maps ... */
37 unsigned int prt_map[PRTMAP_SIZE+1];
39 #endif /* !__MSDOS__ */
41 /*volatile*/ void pdie(const char *msg)
47 fprintf(errstd, "%s\n", msg);
48 #endif /* !__MSDOS__ */
53 /*volatile*/ void __attribute__ ((format (printf, 1, 2))) die(const char *fmt,...)
58 fprintf(errstd,"Fatal: "); /* JRC */
60 vfprintf(errstd,fmt,ap);
67 /*volatile*/ void warn(char *fmt,...)
72 if (nowarn > 0) return;
75 fprintf(errstd,"Warning: ");
77 vfprintf(errstd,fmt,ap);
89 if ((this = malloc(size)) == NULL) pdie("Out of memory");
90 memset(this, 0, size); /* insure consistency */
96 void *ralloc(void *old,int size)
100 if ((this = realloc(old,size)) == NULL) pdie("Out of memory");
105 char *stralloc(const char *str)
109 if ((this = strdup(str)) == NULL) pdie("Out of memory");
114 int to_number(char *num)
119 number = strtol(num,&end,0);
120 if (end && *end) die("Not a number: \"%s\"",num);
125 int timer_number(char *num)
130 number = strtol(num,&end,0);
141 number *= 10; /* convert seconds to tenths */
149 if (number < 0 || number > 36000) die("Not a valid timer value: \"%s\"",num);
154 static char *name(int stage)
158 return "First boot sector";
160 return "Second boot sector";
162 return "Chain loader";
164 die("Internal error: Unknown stage code %d",stage);
166 return NULL; /* for GCC */
170 void check_version(BOOT_SECTOR *sect,int stage)
172 int bs_major, bs_minor;
174 if (!strncmp(sect->par_1.signature-4,"LILO",4))
175 die("%s has a pre-21 LILO signature",name(stage));
176 if (strncmp(sect->par_1.signature,"LILO",4))
177 die("%s doesn't have a valid LILO signature",name(stage));
178 if ((sect->par_1.stage&0xFF) != stage)
179 die("%s has an invalid stage code (%d)",name(stage),sect->par_1.stage);
181 bs_major = sect->par_1.version & 255;
182 bs_minor = sect->par_1.version >> 8;
183 if (sect->par_1.version != VERSION)
184 die("%s is version %d.%d. Expecting version %d.%d.",name(stage),
185 bs_major,bs_minor, VERSION_MAJOR,VERSION_MINOR);
190 int stat_equal(struct stat *a,struct stat *b)
192 return a->st_dev == b->st_dev && a->st_ino == b->st_ino;
194 #endif /* !__MSDOS__ */
196 #endif /* !SHS_MAIN */
200 /* accumulate a partial CRC-32 */
202 unsigned int crc32partial(unsigned char *cp, int nsize,
203 unsigned int polynomial, unsigned int *accum)
205 unsigned int poly, crc;
212 for (i=0; i<8; i++) {
213 if ( ( (crc>>31) ^ (ch>>(7-i)) ) & 1) poly = polynomial;
215 crc = (crc<<1) ^ poly;
218 return (*accum = ~crc);
222 /* calculate a CRC-32 polynomial */
224 unsigned int crc32 (unsigned char *cp, int nsize, unsigned int polynomial)
226 unsigned int crc = 0;
227 return crc32partial(cp, nsize, polynomial, &crc);
231 /* show what a link resolves to */
233 void show_link(char *name)
238 count = readlink(name, lname, sizeof(lname)-1);
241 printf(" -> %s", lname);
244 #else /* __MSDOS__ */
245 char * strerror(int err)
249 #endif /* !__MSDOS__ */
255 int main(int argc, char *argv[])
257 unsigned char buf[4096];
261 fd = open(argv[1],O_RDONLY);
263 n = read(fd,buf,sizeof(buf));
265 crc32partial(buf, n, CRC_POLY1, &crc);
266 n = read(fd,buf,sizeof(buf));
270 printf("0x%08x\n", (int)crc);
271 if (sizeof(short)!=2) {
272 fprintf(stderr,"***Fatal: SHORT != 2\n");
275 if (sizeof(int)!=4) {
276 fprintf(stderr, "*****Fatal: INT != 4\n");
279 if (sizeof(int32_t)>sizeof(int))
280 fprintf(stderr, "**Note: LONG is bigger than INT\n");
284 #endif /* SHS_MAIN */