Add externs to avoid multiple definitions, and then add missing definitions.
[rrq/maintain_lilo.git] / src / common.h
1 /* common.h  -  Common data structures and functions
2  * 
3  * Copyright 1992-1998 Werner Almesberger
4  * Copyright 1999-2005 John Coffman
5  * Copyright 2009-2015 Joachim Wiedorn
6  * All rights reserved.
7  * 
8  * Licensed under the terms contained in the file 'COPYING'
9  * in the source directory.
10  */
11
12 #ifndef COMMON_H
13 #define COMMON_H
14
15 #ifndef LILO_ASM
16 #include <fcntl.h>
17 #include <asm/types.h>
18 #if !__MSDOS__
19 #include <sys/stat.h>
20 #endif
21
22 #include <inttypes.h>
23 #define PTR_WIDTH ((int) (sizeof(intptr_t) * 2))
24
25 #ifndef PAGE_SIZE
26 # define PAGE_SIZE 4096U
27 #endif
28
29 #ifdef O_ACCMODE
30 # define O_NOACCESS O_ACCMODE
31 #else
32 /* open a file for "no access" */
33 # define O_NOACCESS 3
34 #endif
35
36 /* special for LILO, bypass the actual open in  dev_open( , ,-1)   */
37 #define O_BYPASS -1
38
39 #endif
40
41 #if __MSDOS__
42 #define const
43 #endif
44
45
46 /*
47 ;*/typedef struct {             /*
48                                                         block   0
49 ;*/    unsigned char sector,track; /* CX 
50                                                 sa_sector:      .blkb   1
51                                                 sa_track:       .blkb   1
52 ;*/    unsigned char device,head; /* DX
53                                                 sa_device:      .blkb   1
54                                                 sa_head:        .blkb   1
55 ;*/    unsigned char num_sect; /* AL
56                                                 sa_num_sect:    .blkb   1
57 ;*/} SECTOR_ADDR; /*
58                                                 sa_size:
59                                                         endb
60
61
62
63
64 ;*/typedef struct {                     /*
65                                                         block   0
66 ;*/    char name[MAX_IMAGE_NAME+1];     /* image name, NUL terminated 
67                                                 id_name:        .blkb   MAX_IMAGE_NAME_asm+1
68 ;*/    unsigned short password_crc[MAX_PW_CRC*(sizeof(INT4)/sizeof(short))];  /* 4 password CRC-32 values
69                                                 id_password_crc:.blkb   MAX_PW_CRC_asm*4
70 ;*/    unsigned short rd_size[2]; /* RAM disk size in sectors, 0 if none
71                                                 id_rd_size:     .blkb   4               ;don't change the order !!!
72 ;*/    SECTOR_ADDR initrd,start;  /* start of initrd & kernel
73                                                 id_initrd:      .blkb   sa_size         ;  **
74                                                 id_start:       .blkb   sa_size         ;  **
75 ;*/    unsigned short flags,vga_mode; /* image flags & video mode
76                                                 id_flags:       .blkb   2               ;  **
77                                                 id_vga_mode:    .blkb   2               ;  **
78 ;*/} IMAGE_DESCR;               /*
79                                                 id_size:
80                                                         endb
81
82
83
84 ;*/typedef struct {                     /*
85                                                         block   0
86 ;*/    unsigned char  bytes_per_sector[2];      /*
87                                                 bpb_bytes_per_sector:   .blkb   2
88 ;*/    unsigned char  sectors_per_cluster;      /*
89                                                 bpb_sectors_per_cluster:.blkb   1
90 ;*/    unsigned char  reserved_sectors[2];      /*
91                                                                 .blkb   2
92 ;*/    unsigned char  number_of_FATs;           /*
93                                                                 .blkb   1
94 ;*/    unsigned char  root_dir_entries[2];      /*
95                                                                 .blkb   2
96 ;*/    unsigned char  total_sectors[2];         /*
97                                                         .blkb   2
98 ;*/    unsigned char  media_descriptor;         /*
99                                                         .blkb   1
100 ;*/    unsigned char  sectors_per_FAT[2];       /*
101                                                         .blkb   2
102 ;*/    unsigned char  sectors_per_track[2];     /* DOS v.3
103                                                         .blkb   2
104 ;*/    unsigned char  heads_per_cylinder[2];    /* DOS v.3
105                                                         .blkb   2
106 ;*/    unsigned char  hidden_sectors[4];        /*
107                                                         .blkb   4
108 ;*/    unsigned char  total_sectors_long[4];    /* if total_sectors==0
109                                                         .blkb   4
110 ;*/    unsigned char  reserved[7];              /* pad to 32 bytes
111                                                         .blkb   7
112 ;*/} BIOS_PARAMETER_BLOCK;      /*
113                                                 bpb_size:
114                                                         endb
115                                                         
116                                                         
117                                                         
118 ;*/typedef struct {             /*
119                                                 block   0
120 ;*/    unsigned char jump[3];   /* jump to boot code
121                                                 bpdos_jump:     .blkb   3
122 ;*/    char system[8];          /* system ID
123                                                 bpdos_system:   .blkb   8
124 ;*/    BIOS_PARAMETER_BLOCK bpb;        /* BIOS parameter block
125                                                 bpdos_bpb:      .blkb   bpb_size
126 ;*/} BOOT_PARAMS_DOS;   /* DOS fat header
127                                                 bpdos_size:
128                                                         endb
129                                                         
130                                                         
131
132 ;*/typedef struct {                                     /*
133                                                         block   0
134 ;*/    unsigned char cli;     /* clear interrupt flag instruction
135                                                 par1_cli:       .blkb   1
136 ;*/    unsigned char jmp0, jmp1;        /* short jump
137                                                 par1_jump:      .blkb   2
138 ;*/    unsigned char stage;  /*
139                                                 par1_stage:     .blkb   1
140 ;*/    unsigned short code_length;  /* length of the first stage code
141                                                 par1_code_len:  .blkb   2
142 ;*/    char signature[4]; /* "LILO"
143                                                 par1_signature: .blkb   4
144 ;*/    unsigned short version;  /*
145                                                 par1_version:   .blkb   2
146 ;*/    unsigned int map_stamp; /* timestamp for this installation (map creation)
147                                                 par1_mapstamp:  .blkb   4
148 ;*/    unsigned int raid_offset; /* raid partition/partition offset
149                                                 par1_raid_offset: .blkb 4
150 ;*/    unsigned int timestamp; /* timestamp for restoration
151                                                 par1_timestamp: .blkb   4
152 ;*/    unsigned int map_serial_no; /* volume serial no. / id containing the map file
153                                                 par1_map_serial_no:     .blkb   4
154 ;*/    unsigned short prompt; /* FLAG_PROMPT=always, FLAG_RAID install
155                                                 par1_prompt:    .blkb   2
156 ;*/    SECTOR_ADDR secondary; /* sectors of the second stage loader
157                                                 par1_secondary: .blkb   sa_size+1
158 ;*/} BOOT_PARAMS_1; /* first stage boot loader 
159                                                                 .align  4
160                                                 par1_size:
161                                                         endb
162
163
164
165 ;*/typedef struct {     /* second stage parameters
166                                                         block   0
167 ;*/    char jump[6]; /* jump over the data
168                                                 par2_jump:      .blkb   6
169 ;*/    char signature[4]; /* "LILO"
170                                                 par2_signature: .blkb   4
171 ;*/    unsigned short version;  /*
172                                                 par2_version:   .blkb   2
173 ;*/    unsigned int map_stamp;          /* time of creation of the map file
174                                                 par2_mapstamp:  .blkb   4
175 ;*/    unsigned short stage;    /*
176                                                 par2_stage:     .blkb   2
177 ;*/    unsigned char port; /* COM port. 0 = none, 1 = COM1, etc. !!! keep these two serial bytes together !!!
178 ;*/    unsigned char ser_param; /* RS-232 parameters, must be 0 if unused
179                                                 par2_port:      .blkb   1       ; referenced together
180                                                 par2_ser_param: .blkb   1       ; **
181 ;*/    unsigned short timeout; /* 54 msec delay until input time-out, 0xffff: never 
182                                                 par2_timeout:   .blkb   2
183 ;*/    unsigned short delay; /* delay: wait that many 54 msec units.
184                                                 par2_delay:     .blkb   2
185 ;*/    unsigned short msg_len; /* 0 if none
186                                                 par2_msg_len:   .blkb   2
187 ;*/    SECTOR_ADDR keytab; /* keyboard translation table
188                                                 par2_keytab:    .blkb   sa_size
189 ;*/    unsigned char flag2;     /*      flags specific to the second stage loader
190                                                 par2_flag2:     .blkb   1
191 ;*/} BOOT_PARAMS_2; /* second stage boot loader
192                                                                 .align  4
193                                                 par2_size:
194                                                         endb
195
196
197
198
199 ;*/typedef struct {                     /*
200                                                         block   0
201 ;*/    char jump[6]; /* jump over the data
202                                                 parC_jump:      .blkb   6
203 ;*/    char signature[4]; /* "LILO" 
204                                                 parC_signature: .blkb   4
205 ;*/    unsigned short stage,version; /* stage is 0x10
206                                                 parC_stage:     .blkb   2
207                                                 parC_version:   .blkb   2
208 ;*/    unsigned short offset; /* partition entry offset
209                                                 parC_offset:    .blkb   2
210 ;*/    unsigned char drive; /* BIOS drive code
211                                                 parC_drive:     .blkb   1
212 ;*/    unsigned char head; /* head; always 0
213                                                 parC_head:      .blkb   1
214 ;*/    unsigned short drvmap; /* offset of drive map
215                                                 parC_drvmap:    .blkb   2
216 ;*/    unsigned char ptable[PARTITION_ENTRY*PARTITION_ENTRIES]; /* part. table
217                                                 parC_ptable:    .blkb   64
218 ;*/    unsigned short p_devmap[2];      /* pointer to device map filled in by second.S
219                                                 parC_devmap:    .blkb   4
220 ;*/} BOOT_PARAMS_C; /* chain loader
221                                                 parC_size:
222                                                         endb
223
224
225
226 ;*/typedef struct {                                     /*
227                                                         block   0
228 ;*/     char menu_sig[4];       /* "MENU" or "BMP4" signature, or NULs if not present
229                                                 mt_sig:         .blkb   4
230 ;*/     unsigned char at_text;  /* attribute for normal menu text
231                                                 mt_at_text:     .blkb   1
232 ;*/     unsigned char at_highlight;     /* attribute for highlighted text
233                                                 mt_at_hilite:   .blkb   1
234 ;*/     unsigned char at_border;        /* attribute for borders
235                                                 mt_at_border:   .blkb   1
236 ;*/     unsigned char at_title;         /* attribute for title
237                                                 mt_at_title:    .blkb   1
238 ;*/     unsigned char len_title;        /* length of the title string
239                                                 mt_len_title:   .blkb   1
240 ;*/     char title[MAX_MENU_TITLE+2];   /* MENU title to override default
241                                                 mt_title:       .blkb   MAX_MENU_TITLE_asm+2
242 ;*/     short row, col, ncol;           /* BMP row, col, and ncols
243                                                 mt_row:         .blkw   1
244                                                 mt_col:         .blkw   1
245                                                 mt_ncol:        .blkw   1
246 ;*/     short maxcol, xpitch;           /* BMP max per col, xpitch between cols
247                                                 mt_maxcol:      .blkw   1
248                                                 mt_xpitch:      .blkw   1
249 ;*/     short fg, bg, sh;               /* BMP normal text fore, backgr, shadow
250                                                 mt_fg:          .blkw   1
251                                                 mt_bg:          .blkw   1
252                                                 mt_sh:          .blkw   1
253 ;*/     short h_fg, h_bg, h_sh;         /* highlight fg, bg, & shadow
254                                                 mt_h_fg:        .blkw   1
255                                                 mt_h_bg:        .blkw   1
256                                                 mt_h_sh:        .blkw   1
257 ;*/     short t_fg, t_bg, t_sh;         /* timer fg, bg, & shadow colors
258                                                 mt_t_fg:        .blkw   1
259                                                 mt_t_bg:        .blkw   1
260                                                 mt_t_sh:        .blkw   1
261 ;*/     short t_row, t_col;             /* timer position
262                                                 mt_t_row:       .blkw   1
263                                                 mt_t_col:       .blkw   1
264 ;*/     short mincol, reserved[3];      /* BMP min per col before spill to next, reserved spacer
265                                                 mt_mincol:      .blkw   1
266                                                                 .blkw   3
267
268 ;*/     unsigned int serial_no[MAX_BIOS_DEVICES];       /* Known device serial nos. 0x80 .. 0x8F
269                                                 mt_serial_no:   .blkw   MAX_BIOS_DEVICES_asm*2
270 ;*/     unsigned int raid_offset[MAX_RAID_DEVICES];     /* RAID offsets for flagged devices
271                                                 mt_raid_offset: .blkw   MAX_RAID_DEVICES_asm*2
272 ;*/     unsigned short raid_dev_mask;                   /* 16 bit raid device mask flagging items in serial_no
273                                                 mt_raid_dev_mask: .blkw 1
274 ;*/     SECTOR_ADDR msg; /* initial greeting message
275                                                 mt_msg: .blkb   sa_size
276 ;*/     SECTOR_ADDR dflcmd; /* default command line
277                                                 mt_dflcmd:      .blkb   sa_size
278 ;*/     SECTOR_ADDR mt_descr[MAX_DESCR_SECS];   /* descriptor disk addresses
279                                                 mt_descr:       .blkb   sa_size*MAX_DESCR_SECS_asm
280 ;*/     char unused[150-MAX_BIOS_DEVICES*sizeof(int)-(MAX_RAID_DEVICES)*sizeof(int)-MAX_DESCR_SECS*sizeof(SECTOR_ADDR)];                /* spacer
281                                                 mt_unused:      .blkb   150-sa_size*MAX_DESCR_SECS_asm-4*MAX_BIOS_DEVICES_asm-4*MAX_RAID_DEVICES_asm
282 ;*/     short checksum[2];              /* checksum longword
283                                                 mt_cksum:       .blkw   2
284 ;*/     unsigned char mt_flag;          /* contains the FLAG_NOBD only
285                                                 mt_flag:        .blkb   1
286 ;*/     char unused2;                   /* spacer beyond checksum
287                                                 mt_unused2:     .blkb   1
288 ;*/} MENUTABLE;         /* MENU and BITMAP parameters at KEYTABLE+256
289                                                 mt_size:
290                                                         endb
291
292 ;*/
293
294 #ifndef LILO_ASM
295
296 typedef struct {
297     unsigned char bootcode[MAX_BOOT_SIZE];
298     unsigned short mbz;         /* must be zero */
299     int  volume_id;
300     unsigned short marker;      /* may be zero */
301     unsigned char part[PART_TABLE_SIZE];
302     unsigned short boot_ind;    /* 0xAA55 */
303 } BOOT_VOLID;
304     
305 typedef union {
306     BOOT_PARAMS_1 par_1;
307     BOOT_PARAMS_2 par_2;
308     BOOT_PARAMS_C par_c;
309     BOOT_PARAMS_DOS par_d;
310     BOOT_VOLID boot;
311     unsigned char sector[SECTOR_SIZE];
312 } BOOT_SECTOR;
313
314 typedef union {
315     struct {
316         IMAGE_DESCR descr[MAX_IMAGES]; /* boot file descriptors */
317     } d;
318     unsigned char sector[SECTOR_SIZE*MAX_DESCR_SECS];
319     struct {
320         unsigned int sector[SECTOR_SIZE/4*MAX_DESCR_SECS - 1];
321         unsigned int checksum;
322     } l;
323 } DESCR_SECTORS;
324
325 typedef struct {
326     int size;
327     unsigned char data[1];
328 } BUILTIN_FILE;
329
330
331 #ifdef LCF_FIRST6
332 #pragma pack (2)
333 typedef struct {
334     unsigned char device, flags;
335     unsigned int sector;
336 } SECTOR_ADDR6;
337 #pragma pack ()
338 #endif
339
340
341 #endif
342 /*
343 IMAGES_numerator = SECTOR_SIZE_asm*MAX_DESCR_SECS_asm - 4 - 1
344 IMAGES = IMAGES_numerator / id_size
345 ;*/
346 #ifndef LILO_ASM
347 typedef struct {
348     unsigned short jump;        /*  0: jump to startup code */
349     char signature[4];          /*  2: "HdrS" */
350     unsigned short version;     /*  6: header version */
351     unsigned short x,y,z;       /*  8: LOADLIN hacks */
352     unsigned short ver_offset;  /* 14: kernel version string */
353     unsigned char loader;       /* 16: loader type */
354     unsigned char flags;        /* 17: loader flags */
355     unsigned short a;           /* 18: more LOADLIN hacks */
356     unsigned int start; /* 20: kernel start, filled in by loader */
357     unsigned int ramdisk;       /* 24: RAM disk start address */
358     unsigned int ramdisk_size;  /* 28: RAM disk size */
359     unsigned short b,c;         /* 32: bzImage hacks */
360     unsigned short heap_end_ptr;/* 36: 2.01 end of free area after setup code */
361     unsigned char d;            /* 38: padding */
362     unsigned int cmd_line_ptr; /* 40: 2.02 address32 of command line */
363     unsigned int ramdisk_max;   /* 44: 2.03 address32 of highest mem. for ramdisk */
364 } SETUP_HDR;
365
366 #define alloc_t(t) ((t *) alloc(sizeof(t)))
367
368 typedef enum {X_NULL=0, X_NONE, X_AUTO, X_MBR_ONLY, X_MBR, X_SPEC} LILO_EXTRA;
369 typedef enum {AD_ANY=0, AD_GEOMETRIC, AD_LINEAR, AD_LBA32=4} ADDR_MODE;
370 typedef enum {DL_NOT_SET=0, DL_BAD=1, DL_UNKNOWN, DL_MAYBE, DL_GOOD} DL_BIOS;
371
372 extern LILO_EXTRA extra;
373 extern char *identify;
374 extern int verbose,test,compact,linear,nowarn,lba32,autoauto,passw,geometric;
375 extern int bios_boot, bios_map;
376 extern int ireloc, force_fs, force_raid, extended_pt, query;
377 extern int colormax, warnings;
378 extern DL_BIOS bios_passes_dl;
379 extern int boot_dev_nr,raid_index,raid_flags,do_md_install,ndisk,zflag,eflag;
380 extern unsigned short drv_map[DRVMAP_SIZE+1]; /* needed for fixup maps */
381 extern int curr_drv_map;
382 extern unsigned int prt_map[PRTMAP_SIZE+1];
383 extern int curr_prt_map, config_read;
384 extern unsigned int serial_no[MAX_BIOS_DEVICES];
385 extern char *config_file;
386 extern FILE *errstd;
387 extern FILE *pp_fd;
388 extern char *identify;  /* in identify.c */
389 extern int dm_major_list[16];
390 extern int dm_major_nr;
391
392 #define crc(a,b) (~crc32((a),(b),CRC_POLY1))
393 #define brev(x)  \
394  { register unsigned short u1,u2; register unsigned int u=(x); u1=u; u2=u>>16; \
395  u1=(u1>>8)|(u1<<8); u2=(u2>>8)|(u2<<8); x=(unsigned int)u1<<16|u2; }
396 #define cc(x) crc(x.data,x.size)
397 #define comma (cc(First)|cc(Second)|cc(Third)|cc(Bitmap)|cc(Chain)|cc(Mbr)|cc(Mbr2))
398
399
400 /*volatile*/ void pdie(const char *msg) __attribute__ ((noreturn));
401 /* Do a perror and then exit. */
402
403
404 /*volatile*/ void __attribute__ ((format (printf, 1, 2)))
405  __attribute__ ((noreturn)) die(const char *fmt,...);
406 /* fprintf an error message and then exit. */
407
408
409 /*volatile*/ void warn(char *fmt,...);
410 /* issue a warning message if !nowarn */
411
412
413 void *alloc(int size);
414 /* Allocates the specified number of bytes. Dies on error. */
415
416
417 void *ralloc(void *old,int size);
418 /* Changes the size of an allocated memory area. Dies on error. */
419
420
421 char *stralloc(const char *str);
422 /* Like strdup, but dies on error. */
423
424
425 int to_number(char *num);
426 /* Converts a string to a number. Dies if the number is invalid. */
427
428
429 int timer_number(char *num);
430 /* Converts a string to a number.  Allows suffix of 't', 's', 'm', 'h'
431 for Tenths, Seconds, Minutes, Hours. Dies if number is invalid for 
432 "timeout" or "delay" */
433
434
435 void check_version(BOOT_SECTOR *sect, int stage);
436 /* Verify that a boot sector has the correct version number. */
437
438
439 int stat_equal(struct stat *a, struct stat *b);
440 /* Compares two stat structures. Returns a non-zero integer if they describe
441    the same file, zero if they don't. */
442
443
444
445 unsigned int crc32partial(unsigned char *cp, int nsize,
446                         unsigned int polynomial, unsigned int *accum);
447 /* accumulate a partial CRC-32 */
448
449
450
451 unsigned int crc32 (unsigned char *cp, int nsize, unsigned int polynomial);
452 /* calculate a CRC-32 polynomial */
453
454
455 void show_link(char *name);
456 /* show what a link resolves to */
457
458 #if __MSDOS__
459 #undef comma
460 #define comma 0
461 #define semi ";"
462 #endif
463 #endif
464 #endif