OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [simtest/] [disk/] [wrtmbr/] [wrtmbr.c] - Blame information for rev 53

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 hellwig
/*
2
 * wrtmbr.c -- write the master boot record to the disk
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
 
11
 
12
#define SECTOR_SIZE             512
13
 
14
 
15
void error(char *fmt, ...) {
16
  va_list ap;
17
 
18
  va_start(ap, fmt);
19
  fprintf(stderr, "Error: ");
20
  vfprintf(stderr, fmt, ap);
21
  fprintf(stderr, "\n");
22
  va_end(ap);
23
  exit(1);
24
}
25
 
26
 
27
void usage(char *myself) {
28
  fprintf(stderr,
29
          "Usage: %s <disk file> <master boot record file>\n",
30
          myself);
31
  exit(1);
32
}
33
 
34
 
35
int main(int argc, char *argv[]) {
36
  FILE *diskFile;
37
  FILE *bootFile;
38
  unsigned char sectorBuffer[SECTOR_SIZE];
39
 
40
  if (argc != 3) {
41
    usage(argv[0]);
42
  }
43
  diskFile = fopen(argv[1], "r+b");
44
  if (diskFile == NULL) {
45
    error("cannot open disk file '%s'", argv[1]);
46
  }
47
  bootFile = fopen(argv[2], "rb");
48
  if (bootFile == NULL) {
49
    error("cannot open boot sector file '%s'", argv[2]);
50
  }
51
  if (fseek(bootFile, 0, SEEK_END) != 0) {
52
    error("cannot position to end of boot sector file");
53
  }
54
  if (ftell(bootFile) != SECTOR_SIZE) {
55
    error("'%s' is not a proper boot sector file (wrong length)", argv[2]);
56
  }
57
  if (fseek(bootFile, 0, SEEK_SET) != 0) {
58
    error("cannot position to start of boot sector file");
59
  }
60
  if (fread(sectorBuffer, SECTOR_SIZE, 1, bootFile) != 1) {
61
    error("cannot read data from boot sector file");
62
  }
63
  fclose(bootFile);
64
  if (sectorBuffer[SECTOR_SIZE - 2] != 0x55 ||
65
      sectorBuffer[SECTOR_SIZE - 1] != 0xAA) {
66
    error("'%s' is not a proper boot sector file (no signature)", argv[2]);
67
  }
68
  if (fseek(diskFile, 0, SEEK_SET) != 0) {
69
    error("cannot position to start of disk file");
70
  }
71
  if (fwrite(sectorBuffer, 1, SECTOR_SIZE, diskFile) != SECTOR_SIZE) {
72
    error("cannot write boot sector to disk file");
73
  }
74
  fclose(diskFile);
75
  return 0;
76
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.