ReactOS  0.4.13-dev-455-g28ed234
boot.c File Reference
#include "vfatlib.h"
#include <debug.h>
Include dependency graph for boot.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define ROUND_TO_MULTIPLE(n, m)   ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
 
#define FAT12_THRESHOLD   4085
 
#define FAT16_THRESHOLD   65525
 
#define GET_UNALIGNED_W(f)   ( (uint16_t)f[0] | ((uint16_t)f[1]<<8) )
 

Functions

static const charget_media_descr (unsigned char media)
 
static void dump_boot (DOS_FS *fs, struct boot_sector *b, unsigned lss)
 
static void check_backup_boot (DOS_FS *fs, struct boot_sector *b, unsigned int lss)
 
static void init_fsinfo (struct info_sector *i)
 
static void read_fsinfo (DOS_FS *fs, struct boot_sector *b, unsigned int lss)
 
static char print_fat_dirty_state (void)
 
static void check_fat_state_bit (DOS_FS *fs, void *b)
 
void read_boot (DOS_FS *fs)
 
static void write_boot_label (DOS_FS *fs, char *label)
 
off_t find_volume_de (DOS_FS *fs, DIR_ENT *de)
 
static void write_volume_label (DOS_FS *fs, char *label)
 
void write_label (DOS_FS *fs, char *label)
 

Variables

struct {
   uint8_t   media
 
   const char *   descr
 
mediabytes []
 

Macro Definition Documentation

◆ FAT12_THRESHOLD

#define FAT12_THRESHOLD   4085

Definition at line 37 of file boot.c.

◆ FAT16_THRESHOLD

#define FAT16_THRESHOLD   65525

Definition at line 38 of file boot.c.

◆ GET_UNALIGNED_W

#define GET_UNALIGNED_W (   f)    ( (uint16_t)f[0] | ((uint16_t)f[1]<<8) )

Definition at line 57 of file boot.c.

◆ NDEBUG

#define NDEBUG

Definition at line 30 of file boot.c.

◆ ROUND_TO_MULTIPLE

#define ROUND_TO_MULTIPLE (   n,
  m 
)    ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)

Definition at line 33 of file boot.c.

Function Documentation

◆ check_backup_boot()

static void check_backup_boot ( DOS_FS fs,
struct boot_sector b,
unsigned int  lss 
)
static

Definition at line 124 of file boot.c.

125 {
126  struct boot_sector b2;
127 
128  if (!fs->backupboot_start) {
129  printf("There is no backup boot sector.\n");
130  if (le16toh(b->reserved) < 3) {
131  printf("And there is no space for creating one!\n");
132  return;
133  }
134  if (interactive)
135  printf("1) Create one\n2) Do without a backup\n");
136  else
137  printf(" Auto-creating backup boot block.\n");
138  if (!interactive || get_key("12", "?") == '1') {
139  unsigned int bbs;
140  /* The usual place for the backup boot sector is sector 6. Choose
141  * that or the last reserved sector. */
142  if (le16toh(b->reserved) >= 7 && le16toh(b->info_sector) != 6)
143  bbs = 6;
144  else {
145  bbs = le16toh(b->reserved) - 1;
146  if (bbs == le16toh(b->info_sector))
147  --bbs; /* this is never 0, as we checked reserved >= 3! */
148  }
149  fs->backupboot_start = bbs * lss;
150  b->backup_boot = htole16(bbs);
151  fs_write(fs->backupboot_start, sizeof(*b), b);
153  sizeof(b->backup_boot), &b->backup_boot);
154  printf("Created backup of boot sector in sector %d\n", bbs);
155  return;
156  } else
157  return;
158  }
159 
160  fs_read(fs->backupboot_start, sizeof(b2), &b2);
161  if (memcmp(b, &b2, sizeof(b2)) != 0) {
162  /* there are any differences */
163  uint8_t *p, *q;
164  int i, pos, first = 1;
165  char buf[20];
166 
167  printf("There are differences between boot sector and its backup.\n");
168  printf("This is mostly harmless. Differences: (offset:original/backup)\n ");
169  pos = 2;
170  for (p = (uint8_t *) b, q = (uint8_t *) & b2, i = 0; i < sizeof(b2);
171  ++p, ++q, ++i) {
172  if (*p != *q) {
173  sprintf(buf, "%s%u:%02x/%02x", first ? "" : ", ",
174  (unsigned)(p - (uint8_t *) b), *p, *q);
175  if (pos + strlen(buf) > 78)
176  printf("\n "), pos = 2;
177  printf("%s", buf);
178  pos += strlen(buf);
179  first = 0;
180  }
181  }
182  printf("\n");
183 
184  if (interactive)
185  printf("1) Copy original to backup\n"
186  "2) Copy backup to original\n" "3) No action\n");
187  else
188  printf(" Not automatically fixing this.\n");
189  switch (interactive ? get_key("123", "?") : '3') {
190  case '1':
191  fs_write(fs->backupboot_start, sizeof(*b), b);
192  break;
193  case '2':
194  fs_write(0, sizeof(b2), &b2);
195  break;
196  default:
197  break;
198  }
199  }
200 }
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
const GLint * first
Definition: glext.h:5794
#define htole16(x)
Definition: storage32.h:546
#define interactive
Definition: rosglue.h:34
Definition: fs.h:235
void fs_write(off_t pos, int size, void *data)
Definition: io.c:344
#define sprintf(buf, format,...)
Definition: sprintf.c:55
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define offsetof(TYPE, MEMBER)
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
char get_key(const char *valid, const char *prompt)
Definition: common.c:184
void fs_read(off_t pos, int size, void *data)
Definition: io.c:282
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
BYTE uint8_t
Definition: msvideo1.c:66
static CRYPT_DATA_BLOB b2[]
Definition: msg.c:582
GLfloat GLfloat p
Definition: glext.h:8902
uint16_t backup_boot
Definition: fsck.fat.h:57
#define printf
Definition: config.h:203

Referenced by read_boot().

◆ check_fat_state_bit()

static void check_fat_state_bit ( DOS_FS fs,
void b 
)
static

Definition at line 302 of file boot.c.

303 {
304  if (fs->fat_bits == 32) {
305  struct boot_sector *b32 = b;
306 
307  if (b32->reserved3 & FAT_STATE_DIRTY) {
308  printf("0x41: ");
309  if (print_fat_dirty_state() == '1') {
310  b32->reserved3 &= ~FAT_STATE_DIRTY;
311  fs_write(0, sizeof(*b32), b32);
312  }
313  }
314  } else {
315  struct boot_sector_16 *b16 = b;
316 
317  if (b16->reserved2 & FAT_STATE_DIRTY) {
318  printf("0x25: ");
319  if (print_fat_dirty_state() == '1') {
320  b16->reserved2 &= ~FAT_STATE_DIRTY;
321  fs_write(0, sizeof(*b16), b16);
322  }
323  }
324  }
325 }
Definition: fs.h:235
void fs_write(off_t pos, int size, void *data)
Definition: io.c:344
#define FAT_STATE_DIRTY
Definition: fsck.fat.h:80
uint8_t reserved3
Definition: fsck.fat.h:115
#define b
Definition: ke_i.h:79
uint8_t reserved2
Definition: fsck.fat.h:144
static char print_fat_dirty_state(void)
Definition: boot.c:281
#define printf
Definition: config.h:203

Referenced by read_boot().

◆ dump_boot()

static void dump_boot ( DOS_FS fs,
struct boot_sector b,
unsigned  lss 
)
static

Definition at line 71 of file boot.c.

72 {
73  unsigned short sectors;
74 
75  printf("Boot sector contents:\n");
76  if (!atari_format) {
77  char id[9];
78  strncpy(id, (const char *)b->system_id, 8);
79  id[8] = 0;
80  printf("System ID \"%s\"\n", id);
81  } else {
82  /* On Atari, a 24 bit serial number is stored at offset 8 of the boot
83  * sector */
84  printf("Serial number 0x%x\n",
85  b->system_id[5] | (b->system_id[6] << 8) | (b->
86  system_id[7] << 16));
87  }
88  printf("Media byte 0x%02x (%s)\n", b->media, get_media_descr(b->media));
89  printf("%10d bytes per logical sector\n", GET_UNALIGNED_W(b->sector_size));
90  printf("%10d bytes per cluster\n", fs->cluster_size);
91  printf("%10d reserved sector%s\n", le16toh(b->reserved),
92  le16toh(b->reserved) == 1 ? "" : "s");
93  printf("First FAT starts at byte %llu (sector %llu)\n",
94  (unsigned long long)fs->fat_start,
95  (unsigned long long)fs->fat_start / lss);
96  printf("%10d FATs, %d bit entries\n", b->fats, fs->fat_bits);
97  printf("%10lld bytes per FAT (= %llu sectors)\n", (long long)fs->fat_size,
98  (long long)fs->fat_size / lss);
99  if (!fs->root_cluster) {
100  printf("Root directory starts at byte %llu (sector %llu)\n",
101  (unsigned long long)fs->root_start,
102  (unsigned long long)fs->root_start / lss);
103  printf("%10d root directory entries\n", fs->root_entries);
104  } else {
105  printf("Root directory start at cluster %lu (arbitrary size)\n",
106  (unsigned long)fs->root_cluster);
107  }
108  printf("Data area starts at byte %llu (sector %llu)\n",
109  (unsigned long long)fs->data_start,
110  (unsigned long long)fs->data_start / lss);
111  printf("%10lu data clusters (%llu bytes)\n",
112  (unsigned long)fs->data_clusters,
113  (unsigned long long)fs->data_clusters * fs->cluster_size);
114  printf("%u sectors/track, %u heads\n", le16toh(b->secs_track),
115  le16toh(b->heads));
116  printf("%10u hidden sectors\n", atari_format ?
117  /* On Atari, the hidden field is only 16 bit wide and unused */
118  (((unsigned char *)&b->hidden)[0] |
119  ((unsigned char *)&b->hidden)[1] << 8) : le32toh(b->hidden));
120  sectors = GET_UNALIGNED_W(b->sectors);
121  printf("%10u sectors total\n", sectors ? sectors : le32toh(b->total_sect));
122 }
#define GET_UNALIGNED_W(f)
Definition: boot.c:57
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
Definition: fs.h:235
int atari_format
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
__u8 sectors[2]
Definition: mkdosfs.c:366
static const char * get_media_descr(unsigned char media)
Definition: boot.c:60
__u8 system_id[8]
Definition: mkdosfs.c:359
#define printf
Definition: config.h:203

Referenced by read_boot().

◆ find_volume_de()

off_t find_volume_de ( DOS_FS fs,
DIR_ENT *  de 
)

Definition at line 517 of file boot.c.

518 {
519  uint32_t cluster;
520  off_t offset;
521  int i;
522 
523  if (fs->root_cluster) {
524  for (cluster = fs->root_cluster;
525  cluster != 0 && cluster != -1;
526  cluster = next_cluster(fs, cluster)) {
527  offset = cluster_start(fs, cluster);
528  for (i = 0; i * sizeof(DIR_ENT) < fs->cluster_size; i++) {
529  fs_read(offset, sizeof(DIR_ENT), de);
530  if (de->attr != VFAT_LN_ATTR && de->attr & ATTR_VOLUME)
531  return offset;
532  offset += sizeof(DIR_ENT);
533  }
534  }
535  } else {
536  for (i = 0; i < fs->root_entries; i++) {
537  offset = fs->root_start + i * sizeof(DIR_ENT);
538  fs_read(offset, sizeof(DIR_ENT), de);
539  if (de->attr != VFAT_LN_ATTR && de->attr & ATTR_VOLUME)
540  return offset;
541  }
542  }
543 
544  return 0;
545 }
#define ATTR_VOLUME
Definition: mkdosfs.c:369
__kernel_off_t off_t
Definition: linux.h:201
GLintptr offset
Definition: glext.h:5920
Definition: fs.h:235
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
uint32_t next_cluster(DOS_FS *fs, uint32_t cluster)
Definition: fat.c:276
void fs_read(off_t pos, int size, void *data)
Definition: io.c:282
UINT32 uint32_t
Definition: types.h:75
off_t cluster_start(DOS_FS *fs, uint32_t cluster)
Definition: fat.c:289
#define VFAT_LN_ATTR
Definition: fsck.fat.h:78

Referenced by write_volume_label().

◆ get_media_descr()

static const char* get_media_descr ( unsigned char  media)
static

Definition at line 60 of file boot.c.

61 {
62  int i;
63 
64  for (i = 0; i < sizeof(mediabytes) / sizeof(*mediabytes); ++i) {
65  if (mediabytes[i].media == media)
66  return (mediabytes[i].descr);
67  }
68  return ("undefined");
69 }
uint8_t media
Definition: boot.c:41
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
const char * descr
Definition: boot.c:42
static struct @3958 mediabytes[]

Referenced by dump_boot().

◆ init_fsinfo()

static void init_fsinfo ( struct info_sector i)
static

Definition at line 202 of file boot.c.

203 {
204  memset(i, 0, sizeof (struct info_sector));
205  i->magic = htole32(0x41615252);
206  i->signature = htole32(0x61417272);
207  i->free_clusters = htole32(-1);
208  i->next_cluster = htole32(2);
209  i->boot_sign = htole32(0xaa550000);
210 }
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define htole32(x)
Definition: storage32.h:543
#define memset(x, y, z)
Definition: compat.h:39

Referenced by read_fsinfo().

◆ print_fat_dirty_state()

static char print_fat_dirty_state ( void  )
static

Definition at line 281 of file boot.c.

282 {
283  printf("Dirty bit is set. Fs was not properly unmounted and"
284  " some data may be corrupt.\n");
285 
286  if (interactive) {
287  printf("1) Remove dirty bit\n" "2) No action\n");
288  return get_key("12", "?");
289 #ifndef __REACTOS__
290  } else
291 #else
292  } else if (rw) {
293 #endif
294  printf(" Automatically removing dirty bit.\n");
295  return '1';
296 #ifdef __REACTOS__
297  }
298  return '2';
299 #endif
300 }
#define interactive
Definition: rosglue.h:34
char get_key(const char *valid, const char *prompt)
Definition: common.c:184
int rw
#define printf
Definition: config.h:203

Referenced by check_fat_state_bit().

◆ read_boot()

void read_boot ( DOS_FS fs)

Definition at line 327 of file boot.c.

328 {
329  struct boot_sector b;
330  unsigned total_sectors;
331  unsigned int logical_sector_size, sectors;
333  unsigned total_fat_entries;
334  off_t data_size;
335 
336  fs_read(0, sizeof(b), &b);
337  logical_sector_size = GET_UNALIGNED_W(b.sector_size);
338  if (!logical_sector_size)
339  die("Logical sector size is zero.");
340 
341  /* This was moved up because it's the first thing that will fail */
342  /* if the platform needs special handling of unaligned multibyte accesses */
343  /* but such handling isn't being provided. See GET_UNALIGNED_W() above. */
344  if (logical_sector_size & (SECTOR_SIZE - 1))
345  die("Logical sector size (%d bytes) is not a multiple of the physical "
346  "sector size.", logical_sector_size);
347 
348  fs->cluster_size = b.cluster_size * logical_sector_size;
349  if (!fs->cluster_size)
350  die("Cluster size is zero.");
351  if (b.fats != 2 && b.fats != 1)
352  die("Currently, only 1 or 2 FATs are supported, not %d.\n", b.fats);
353  fs->nfats = b.fats;
354  sectors = GET_UNALIGNED_W(b.sectors);
355  total_sectors = sectors ? sectors : le32toh(b.total_sect);
356  if (verbose)
357  printf("Checking we can access the last sector of the filesystem\n");
358  /* Can't access last odd sector anyway, so round down */
359  fs_test((off_t)((total_sectors & ~1) - 1) * logical_sector_size,
360  logical_sector_size);
361 
362  fat_length = le16toh(b.fat_length) ?
363  le16toh(b.fat_length) : le32toh(b.fat32_length);
364  if (!fat_length)
365  die("FAT size is zero.");
366 
367  fs->fat_start = (off_t)le16toh(b.reserved) * logical_sector_size;
368  fs->root_start = ((off_t)le16toh(b.reserved) + b.fats * fat_length) *
369  logical_sector_size;
370  fs->root_entries = GET_UNALIGNED_W(b.dir_entries);
371  fs->data_start = fs->root_start + ROUND_TO_MULTIPLE(fs->root_entries <<
373  logical_sector_size);
374 
375  data_size = (off_t)total_sectors * logical_sector_size - fs->data_start;
376  if (data_size < fs->cluster_size)
377  die("Filesystem has no space for any data clusters");
378 
379  fs->data_clusters = data_size / fs->cluster_size;
380  fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */
381  fs->fsinfo_start = 0; /* no FSINFO structure */
382  fs->free_clusters = -1; /* unknown */
383  if (!b.fat_length && b.fat32_length) {
384  fs->fat_bits = 32;
385  fs->root_cluster = le32toh(b.root_cluster);
386  if (!fs->root_cluster && fs->root_entries)
387  /* M$ hasn't specified this, but it looks reasonable: If
388  * root_cluster is 0 but there is a separate root dir
389  * (root_entries != 0), we handle the root dir the old way. Give a
390  * warning, but convertig to a root dir in a cluster chain seems
391  * to complex for now... */
392  printf("Warning: FAT32 root dir not in cluster chain! "
393  "Compatibility mode...\n");
394  else if (!fs->root_cluster && !fs->root_entries)
395  die("No root directory!");
396  else if (fs->root_cluster && fs->root_entries)
397  printf("Warning: FAT32 root dir is in a cluster chain, but "
398  "a separate root dir\n"
399  " area is defined. Cannot fix this easily.\n");
400  if (fs->data_clusters < FAT16_THRESHOLD)
401  printf("Warning: Filesystem is FAT32 according to fat_length "
402  "and fat32_length fields,\n"
403  " but has only %lu clusters, less than the required "
404  "minimum of %d.\n"
405  " This may lead to problems on some systems.\n",
406  (unsigned long)fs->data_clusters, FAT16_THRESHOLD);
407 
409  fs->backupboot_start = le16toh(b.backup_boot) * logical_sector_size;
410  check_backup_boot(fs, &b, logical_sector_size);
411 
412  read_fsinfo(fs, &b, logical_sector_size);
413  } else if (!atari_format) {
414  /* On real MS-DOS, a 16 bit FAT is used whenever there would be too
415  * much clusers otherwise. */
416  fs->fat_bits = (fs->data_clusters >= FAT12_THRESHOLD) ? 16 : 12;
417  if (fs->data_clusters >= FAT16_THRESHOLD)
418  die("Too many clusters (%lu) for FAT16 filesystem.",
419  (unsigned long)fs->data_clusters);
421  } else {
422  /* On Atari, things are more difficult: GEMDOS always uses 12bit FATs
423  * on floppies, and always 16 bit on harddisks. */
424  fs->fat_bits = 16; /* assume 16 bit FAT for now */
425  /* If more clusters than fat entries in 16-bit fat, we assume
426  * it's a real MSDOS FS with 12-bit fat. */
427  if (fs->data_clusters + 2 > fat_length * logical_sector_size * 8 / 16 ||
428  /* if it has one of the usual floppy sizes -> 12bit FAT */
429  (total_sectors == 720 || total_sectors == 1440 ||
430  total_sectors == 2880))
431  fs->fat_bits = 12;
432  }
433  /* On FAT32, the high 4 bits of a FAT entry are reserved */
434  fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits;
435  fs->fat_size = fat_length * logical_sector_size;
436 
437  fs->label = calloc(12, sizeof(uint8_t));
438  if (fs->fat_bits == 12 || fs->fat_bits == 16) {
439  struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
440  if (b16->extended_sig == 0x29)
441  memmove(fs->label, b16->label, 11);
442  else
443 #ifdef __REACTOS__
444  {
445  free(fs->label);
446 #endif
447  fs->label = NULL;
448 #ifdef __REACTOS__
449  }
450 #endif
451  } else if (fs->fat_bits == 32) {
452  if (b.extended_sig == 0x29)
453  memmove(fs->label, &b.label, 11);
454  else
455 #ifdef __REACTOS__
456  {
457  free(fs->label);
458 #endif
459  fs->label = NULL;
460 #ifdef __REACTOS__
461  }
462 #endif
463  }
464 
465  total_fat_entries = (uint64_t)fs->fat_size * 8 / fs->fat_bits;
466  if (fs->data_clusters > total_fat_entries - 2)
467  die("Filesystem has %u clusters but only space for %u FAT entries.",
468  fs->data_clusters, total_fat_entries - 2);
469  if (!fs->root_entries && !fs->root_cluster)
470  die("Root directory has zero size.");
471  if (fs->root_entries & (MSDOS_DPS - 1))
472  die("Root directory (%d entries) doesn't span an integral number of "
473  "sectors.", fs->root_entries);
474  if (logical_sector_size & (SECTOR_SIZE - 1))
475  die("Logical sector size (%u bytes) is not a multiple of the physical "
476  "sector size.", logical_sector_size);
477 #if 0 /* linux kernel doesn't check that either */
478  /* ++roman: On Atari, these two fields are often left uninitialized */
479  if (!atari_format && (!b.secs_track || !b.heads))
480  die("Invalid disk format in boot sector.");
481 #endif
482  if (verbose)
483  dump_boot(fs, &b, logical_sector_size);
484 }
uint8_t label[11]
Definition: fsck.fat.h:148
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define FAT12_THRESHOLD
Definition: boot.c:37
#define GET_UNALIGNED_W(f)
Definition: boot.c:57
#define free
Definition: debug_ros.c:5
__kernel_off_t off_t
Definition: linux.h:201
uint32_t fat32_length
Definition: fsck.fat.h:52
#define ROUND_TO_MULTIPLE(n, m)
Definition: boot.c:33
DWORD total_sectors
Definition: cdmake.c:201
__u16 fat_length
Definition: mkdosfs.c:368
Definition: fs.h:235
static void dump_boot(DOS_FS *fs, struct boot_sector *b, unsigned lss)
Definition: boot.c:71
#define verbose
Definition: rosglue.h:36
int fs_test(off_t pos, int size)
Definition: io.c:322
#define MSDOS_DPS
Definition: msdos_fs.h:28
#define SECTOR_SIZE
Definition: winldr.h:34
__u8 cluster_size
Definition: mkdosfs.c:362
smooth NULL
Definition: ftsmooth.c:416
int atari_format
#define off_t
Definition: dosfsck.h:5
#define FAT16_THRESHOLD
Definition: boot.c:38
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
if(!(yy_init))
Definition: macro.lex.yy.c:714
void fs_read(off_t pos, int size, void *data)
Definition: io.c:282
BYTE uint8_t
Definition: msvideo1.c:66
static void check_backup_boot(DOS_FS *fs, struct boot_sector *b, unsigned int lss)
Definition: boot.c:124
#define uint64_t
Definition: nsiface.idl:62
static void read_fsinfo(DOS_FS *fs, struct boot_sector *b, unsigned int lss)
Definition: boot.c:212
static void check_fat_state_bit(DOS_FS *fs, void *b)
Definition: boot.c:302
#define MSDOS_DIR_BITS
Definition: msdos_fs.h:30
__u8 sectors[2]
Definition: mkdosfs.c:366
#define calloc
Definition: rosglue.h:14
uint8_t extended_sig
Definition: fsck.fat.h:146
#define die(str)
Definition: mkdosfs.c:347
#define printf
Definition: config.h:203

Referenced by VfatChkdsk().

◆ read_fsinfo()

static void read_fsinfo ( DOS_FS fs,
struct boot_sector b,
unsigned int  lss 
)
static

Definition at line 212 of file boot.c.

213 {
214  struct info_sector i;
215 
216  if (!b->info_sector) {
217  printf("No FSINFO sector\n");
218  if (interactive)
219  printf("1) Create one\n2) Do without FSINFO\n");
220  else
221  printf(" Not automatically creating it.\n");
222  if (interactive && get_key("12", "?") == '1') {
223  /* search for a free reserved sector (not boot sector and not
224  * backup boot sector) */
225  uint32_t s;
226  for (s = 1; s < le16toh(b->reserved); ++s)
227  if (s != le16toh(b->backup_boot))
228  break;
229  if (s > 0 && s < le16toh(b->reserved)) {
230  init_fsinfo(&i);
231  fs_write((off_t)s * lss, sizeof(i), &i);
232  b->info_sector = htole16(s);
234  sizeof(b->info_sector), &b->info_sector);
235  if (fs->backupboot_start)
236  fs_write(fs->backupboot_start +
238  sizeof(b->info_sector), &b->info_sector);
239  } else {
240  printf("No free reserved sector found -- "
241  "no space for FSINFO sector!\n");
242  return;
243  }
244  } else
245  return;
246  }
247 
248  fs->fsinfo_start = le16toh(b->info_sector) * lss;
249  fs_read(fs->fsinfo_start, sizeof(i), &i);
250 
251  if (i.magic != htole32(0x41615252) ||
252  i.signature != htole32(0x61417272) || i.boot_sign != htole32(0xaa550000)) {
253  printf("FSINFO sector has bad magic number(s):\n");
254  if (i.magic != htole32(0x41615252))
255  printf(" Offset %llu: 0x%08x != expected 0x%08x\n",
256  (unsigned long long)offsetof(struct info_sector, magic),
257  le32toh(i.magic), 0x41615252);
258  if (i.signature != htole32(0x61417272))
259  printf(" Offset %llu: 0x%08x != expected 0x%08x\n",
260  (unsigned long long)offsetof(struct info_sector, signature),
261  le32toh(i.signature), 0x61417272);
262  if (i.boot_sign != htole32(0xaa550000))
263  printf(" Offset %llu: 0x%08x != expected 0x%08x\n",
264  (unsigned long long)offsetof(struct info_sector, boot_sign),
265  le32toh(i.boot_sign), 0xaa550000);
266  if (interactive)
267  printf("1) Correct\n2) Don't correct (FSINFO invalid then)\n");
268  else
269  printf(" Auto-correcting it.\n");
270  if (!interactive || get_key("12", "?") == '1') {
271  init_fsinfo(&i);
272  fs_write(fs->fsinfo_start, sizeof(i), &i);
273  } else
274  fs->fsinfo_start = 0;
275  }
276 
277  if (fs->fsinfo_start)
278  fs->free_clusters = le32toh(i.free_clusters);
279 }
__kernel_off_t off_t
Definition: linux.h:201
#define htole16(x)
Definition: storage32.h:546
u32_t magic(void)
#define interactive
Definition: rosglue.h:34
Definition: fs.h:235
void fs_write(off_t pos, int size, void *data)
Definition: io.c:344
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define offsetof(TYPE, MEMBER)
#define htole32(x)
Definition: storage32.h:543
uint32_t boot_sign
Definition: fsck.fat.h:162
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
char get_key(const char *valid, const char *prompt)
Definition: common.c:184
void fs_read(off_t pos, int size, void *data)
Definition: io.c:282
static void init_fsinfo(struct info_sector *i)
Definition: boot.c:202
GLdouble s
Definition: gl.h:2039
UINT32 uint32_t
Definition: types.h:75
#define printf
Definition: config.h:203
uint32_t signature
Definition: fsck.fat.h:158

Referenced by read_boot().

◆ write_boot_label()

static void write_boot_label ( DOS_FS fs,
char label 
)
static

Definition at line 487 of file boot.c.

488 {
489  if (fs->fat_bits == 12 || fs->fat_bits == 16) {
490  struct boot_sector_16 b16;
491 
492  fs_read(0, sizeof(b16), &b16);
493  if (b16.extended_sig != 0x29) {
494  b16.extended_sig = 0x29;
495  b16.serial = 0;
496  memmove(b16.fs_type, fs->fat_bits == 12 ? "FAT12 " : "FAT16 ",
497  8);
498  }
499  memmove(b16.label, label, 11);
500  fs_write(0, sizeof(b16), &b16);
501  } else if (fs->fat_bits == 32) {
502  struct boot_sector b;
503 
504  fs_read(0, sizeof(b), &b);
505  if (b.extended_sig != 0x29) {
506  b.extended_sig = 0x29;
507  b.serial = 0;
508  memmove(b.fs_type, "FAT32 ", 8);
509  }
510  memmove(b.label, label, 11);
511  fs_write(0, sizeof(b), &b);
512  if (fs->backupboot_start)
513  fs_write(fs->backupboot_start, sizeof(b), &b);
514  }
515 }
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
Definition: fs.h:235
void fs_write(off_t pos, int size, void *data)
Definition: io.c:344
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
void fs_read(off_t pos, int size, void *data)
Definition: io.c:282
uint8_t label[11]
Definition: fsck.fat.h:65

Referenced by write_label().

◆ write_label()

void write_label ( DOS_FS fs,
char label 
)

Definition at line 582 of file boot.c.

583 {
584  int l = strlen(label);
585 
586  while (l < 11)
587  label[l++] = ' ';
588 
591 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
Definition: fs.h:235
r l[0]
Definition: byte_order.h:167
static void write_boot_label(DOS_FS *fs, char *label)
Definition: boot.c:487
uint8_t label[11]
Definition: fsck.fat.h:65
static void write_volume_label(DOS_FS *fs, char *label)
Definition: boot.c:547

◆ write_volume_label()

static void write_volume_label ( DOS_FS fs,
char label 
)
static

Definition at line 547 of file boot.c.

548 {
549  time_t now = time(NULL);
550  struct tm *mtime = localtime(&now);
551  off_t offset;
552  int created;
553  DIR_ENT de;
554 
555  created = 0;
556  offset = find_volume_de(fs, &de);
557  if (offset == 0) {
558  created = 1;
559  offset = alloc_rootdir_entry(fs, &de, label, 0);
560  }
561  memcpy(de.name, label, 11);
562  de.time = htole16((unsigned short)((mtime->tm_sec >> 1) +
563  (mtime->tm_min << 5) +
564  (mtime->tm_hour << 11)));
565  de.date = htole16((unsigned short)(mtime->tm_mday +
566  ((mtime->tm_mon + 1) << 5) +
567  ((mtime->tm_year - 80) << 9)));
568  if (created) {
569  de.attr = ATTR_VOLUME;
570  de.ctime_ms = 0;
571  de.ctime = de.time;
572  de.cdate = de.date;
573  de.adate = de.date;
574  de.starthi = 0;
575  de.start = 0;
576  de.size = 0;
577  }
578 
579  fs_write(offset, sizeof(DIR_ENT), &de);
580 }
off_t alloc_rootdir_entry(DOS_FS *fs, DIR_ENT *de, const char *pattern, int gen_name)
Definition: check.c:74
int tm_min
Definition: time.h:78
off_t find_volume_de(DOS_FS *fs, DIR_ENT *de)
Definition: boot.c:517
#define ATTR_VOLUME
Definition: mkdosfs.c:369
__kernel_off_t off_t
Definition: linux.h:201
int tm_mday
Definition: time.h:80
GLintptr offset
Definition: glext.h:5920
#define htole16(x)
Definition: storage32.h:546
int tm_year
Definition: time.h:82
__u16 time
Definition: mkdosfs.c:366
Definition: fs.h:235
void fs_write(off_t pos, int size, void *data)
Definition: io.c:344
time_t now
Definition: finger.c:65
smooth NULL
Definition: ftsmooth.c:416
int tm_mon
Definition: time.h:81
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
Definition: time.h:76
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:426
uint8_t label[11]
Definition: fsck.fat.h:65
__kernel_time_t time_t
Definition: linux.h:252
int tm_sec
Definition: time.h:77
int tm_hour
Definition: time.h:79

Referenced by write_label().

Variable Documentation

◆ descr

const char* descr

Definition at line 42 of file boot.c.

Referenced by AddItem(), CRYPT_AsnDecodeAccessDescription(), CRYPT_AsnEncodeAccessDescription(), CxxHandleV8Frame(), dwarf2_parse_line_numbers(), get_dc_data_ex(), get_media_descr(), LISTBOX_Create(), LISTBOX_DeleteItem(), LISTBOX_Destroy(), LISTBOX_Directory(), LISTBOX_DrawFocusRect(), LISTBOX_FindFileStrPos(), LISTBOX_FindString(), LISTBOX_FindStringPos(), LISTBOX_GetCurrentPageSize(), LISTBOX_GetItemFromPoint(), LISTBOX_GetItemHeight(), LISTBOX_GetItemRect(), LISTBOX_GetMaxTopIndex(), LISTBOX_GetSelCount(), LISTBOX_GetSelItems(), LISTBOX_GetText(), LISTBOX_HandleChar(), LISTBOX_HandleHScroll(), LISTBOX_HandleKeyDown(), LISTBOX_HandleLButtonDown(), LISTBOX_HandleLButtonDownCombo(), LISTBOX_HandleLButtonUp(), LISTBOX_HandleMouseMove(), LISTBOX_HandleMouseWheel(), LISTBOX_HandleSystemTimer(), LISTBOX_HandleTimer(), LISTBOX_HandleVScroll(), LISTBOX_InitStorage(), LISTBOX_InsertItem(), LISTBOX_InsertString(), LISTBOX_InvalidateItemRect(), LISTBOX_InvalidateItems(), LISTBOX_MakeItemVisible(), LISTBOX_MoveCaret(), LISTBOX_NCPaint(), LISTBOX_Paint(), LISTBOX_PaintItem(), LISTBOX_RemoveItem(), LISTBOX_RepaintItem(), LISTBOX_ResetContent(), LISTBOX_SelectItemRange(), LISTBOX_SetCaretIndex(), LISTBOX_SetColumnWidth(), LISTBOX_SetCount(), LISTBOX_SetFont(), LISTBOX_SetHorizontalExtent(), LISTBOX_SetHorizontalPos(), LISTBOX_SetItemHeight(), LISTBOX_SetRedraw(), LISTBOX_SetSelection(), LISTBOX_SetTabStops(), LISTBOX_SetTopItem(), LISTBOX_update_uistate(), LISTBOX_UpdatePage(), LISTBOX_UpdateScroll(), LISTBOX_UpdateSize(), LISTBOX_WindowProc(), ListBoxWndProc_common(), NtUserGetListBoxInfo(), output_typelib_regscript(), process_comment(), process_extra(), sw_DescribePixelFormat(), test_DC_bitmap(), test_device_caps(), test_import_resolution(), test_multiselect(), test_stdout_handle(), wglDescribePixelFormat(), write_coclass(), and write_progid().

◆ media

uint8_t media

Definition at line 41 of file boot.c.

Referenced by get_media_descr().

◆ mediabytes

struct { ... } mediabytes[]
Initial value:
= {
{
0xf0, "5.25\" or 3.5\" HD floppy"}, {
0xf8, "hard disk"}, {
0xf9, "3,5\" 720k floppy 2s/80tr/9sec or "
"5.25\" 1.2M floppy 2s/80tr/15sec"}, {
0xfa, "5.25\" 320k floppy 1s/80tr/8sec"}, {
0xfb, "3.5\" 640k floppy 2s/80tr/8sec"}, {
0xfc, "5.25\" 180k floppy 1s/40tr/9sec"}, {
0xfd, "5.25\" 360k floppy 2s/40tr/9sec"}, {
0xfe, "5.25\" 160k floppy 1s/40tr/8sec"}, {
0xff, "5.25\" 320k floppy 2s/40tr/8sec"},}

Referenced by get_media_descr().