1 /* common.c - Common data structures and functions. */
3 Copyright 1992-1998 Werner Almesberger.
4 Copyright 1999-2005 John Coffman.
7 Licensed under the terms contained in the file 'COPYING' in the
25 char *identify = NULL;
26 int boot_dev_nr, raid_index, do_md_install;
27 int verbose = 0, test = 0, compact = 0, linear = 0, raid_flags = 0, zflag = 0,
28 ireloc = 0, force_fs = 0, force_raid = 0, extended_pt = 0, query = 0,
29 nowarn = 0, lba32 = 0, autoauto = 0, passw = 0, geometric = 0, eflag = 0;
30 int colormax = 15, warnings = 0;
31 DL_BIOS bios_passes_dl = DL_NOT_SET;
35 int bios_boot, bios_map;
36 unsigned short drv_map[DRVMAP_SIZE+1]; /* fixup maps ... */
38 unsigned int prt_map[PRTMAP_SIZE+1];
40 #endif /* !__MSDOS__ */
42 /*volatile*/ void pdie(char *msg)
48 fprintf(errstd, "%s\n", msg);
49 #endif /* !__MSDOS__ */
54 /*volatile*/ void die(char *fmt,...)
59 fprintf(errstd,"Fatal: "); /* JRC */
61 vfprintf(errstd,fmt,ap);
68 /*volatile*/ void warn(char *fmt,...)
73 if (nowarn > 0) return;
76 fprintf(errstd,"Warning: ");
78 vfprintf(errstd,fmt,ap);
90 if ((this = malloc(size)) == NULL) pdie("Out of memory");
91 memset(this, 0, size); /* insure consistency */
97 void *ralloc(void *old,int size)
101 if ((this = realloc(old,size)) == NULL) pdie("Out of memory");
106 char *stralloc(const char *str)
110 if ((this = strdup(str)) == NULL) pdie("Out of memory");
115 int to_number(char *num)
120 number = strtol(num,&end,0);
121 if (end && *end) die("Not a number: \"%s\"",num);
126 int timer_number(char *num)
131 number = strtol(num,&end,0);
142 number *= 10; /* convert seconds to tenths */
150 if (number < 0 || number > 36000) die("Not a valid timer value: \"%s\"",num);
155 static char *name(int stage)
159 return "First boot sector";
161 return "Second boot sector";
163 return "Chain loader";
165 die("Internal error: Unknown stage code %d",stage);
167 return NULL; /* for GCC */
171 void check_version(BOOT_SECTOR *sect,int stage)
173 int bs_major, bs_minor;
175 if (!strncmp(sect->par_1.signature-4,"LILO",4))
176 die("%s has a pre-21 LILO signature",name(stage));
177 if (strncmp(sect->par_1.signature,"LILO",4))
178 die("%s doesn't have a valid LILO signature",name(stage));
179 if ((sect->par_1.stage&0xFF) != stage)
180 die("%s has an invalid stage code (%d)",name(stage),sect->par_1.stage);
182 bs_major = sect->par_1.version & 255;
183 bs_minor = sect->par_1.version >> 8;
184 if (sect->par_1.version != VERSION)
185 die("%s is version %d.%d. Expecting version %d.%d.",name(stage),
186 bs_major,bs_minor, VERSION_MAJOR,VERSION_MINOR);
191 int stat_equal(struct stat *a,struct stat *b)
193 return a->st_dev == b->st_dev && a->st_ino == b->st_ino;
195 #endif /* !__MSDOS__ */
197 #endif /* !SHS_MAIN */
201 /* accumulate a partial CRC-32 */
203 unsigned int crc32partial(unsigned char *cp, int nsize,
204 unsigned int polynomial, unsigned int *accum)
206 unsigned int poly, crc;
213 for (i=0; i<8; i++) {
214 if ( ( (crc>>31) ^ (ch>>(7-i)) ) & 1) poly = polynomial;
216 crc = (crc<<1) ^ poly;
219 return (*accum = ~crc);
223 /* calculate a CRC-32 polynomial */
225 unsigned int crc32 (unsigned char *cp, int nsize, unsigned int polynomial)
227 unsigned int crc = 0;
228 return crc32partial(cp, nsize, polynomial, &crc);
232 /* show what a link resolves to */
234 void show_link(char *name)
239 count = readlink(name, lname, sizeof(lname)-1);
242 printf(" -> %s", lname);
245 #else /* __MSDOS__ */
246 char * strerror(int err)
250 #endif /* !__MSDOS__ */
256 int main(int argc, char *argv[])
258 unsigned char buf[4096];
262 fd = open(argv[1],O_RDONLY);
264 n = read(fd,buf,sizeof(buf));
266 crc32partial(buf, n, CRC_POLY1, &crc);
267 n = read(fd,buf,sizeof(buf));
271 printf("0x%08x\n", (int)crc);
272 if (sizeof(short)!=2) {
273 fprintf(stderr,"***Fatal: SHORT != 2\n");
276 if (sizeof(int)!=4) {
277 fprintf(stderr, "*****Fatal: INT != 4\n");
280 if (sizeof(long)>sizeof(int))
281 fprintf(stderr, "**Note: LONG is bigger than INT\n");
285 #endif /* SHS_MAIN */