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