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

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.24/] [disk/] [tools/] [fs-NetBSD/] [wrboot/] [wrboot.c] - Blame information for rev 211

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 hellwig
/*
2
 * wrboot.c -- write the boot block
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
 
11
 
12
/**************************************************************/
13
 
14
 
15
#define SSIZE           512     /* disk sector size in bytes */
16
#define BSIZE           8192    /* disk block size in bytes */
17
 
18
 
19
/**************************************************************/
20
 
21
 
22
void error(char *fmt, ...) {
23
  va_list ap;
24
 
25
  va_start(ap, fmt);
26
  printf("Error: ");
27
  vprintf(fmt, ap);
28
  printf("\n");
29
  va_end(ap);
30
  exit(1);
31
}
32
 
33
 
34
/**************************************************************/
35
 
36
 
37
unsigned int read4FromEco(unsigned char *p) {
38
  return (unsigned int) p[0] << 24 |
39
         (unsigned int) p[1] << 16 |
40
         (unsigned int) p[2] <<  8 |
41
         (unsigned int) p[3] <<  0;
42
}
43
 
44
 
45
/**************************************************************/
46
 
47
 
48
void usage(char *myself) {
49
  printf("Usage: %s <bootblock> <disk> <partition or '*'>\n", myself);
50
}
51
 
52
 
53
int main(int argc, char *argv[]) {
54
  int i;
55
  char *bootName;
56
  FILE *bootFile;
57
  int bootSize;
58
  char *diskName;
59
  FILE *diskFile;
60
  unsigned long sliceStart;
61
  unsigned long sliceSize;
62
  int part;
63
  char *endptr;
64
  unsigned char partTable[SSIZE];
65
  unsigned char *ptptr;
66
  unsigned long partType;
67
  unsigned char bootCode[BSIZE];
68
  unsigned char diskLabel[SSIZE];
69
 
70
  if (argc != 4) {
71
    usage(argv[0]);
72
    exit(1);
73
  }
74
  /* read bootblock from file */
75
  for (i = 0; i < BSIZE; i++) {
76
    bootCode[i] = 0;
77
  }
78
  bootName = argv[1];
79
  bootFile = fopen(bootName, "rb");
80
  if (bootFile == NULL) {
81
    error("cannot open bootblock '%s'", bootName);
82
  }
83
  fseek(bootFile, 0, SEEK_END);
84
  bootSize = ftell(bootFile);
85
  fseek(bootFile, 0, SEEK_SET);
86
  if (bootSize > BSIZE) {
87
    error("bootblock '%s' too big", bootName);
88
  }
89
  if (fread(bootCode, 1, bootSize, bootFile) != bootSize) {
90
    error("cannot read bootblock '%s'", bootName);
91
  }
92
  fclose(bootFile);
93
  printf("Bootblock '%s' read, %d bytes.\n", bootName, bootSize);
94
  /* determine NetBSD slice position on disk */
95
  diskName = argv[2];
96
  diskFile = fopen(diskName, "r+b");
97
  if (diskFile == NULL) {
98
    error("cannot open disk image '%s'", diskName);
99
  }
100
  if (strcmp(argv[3], "*") == 0) {
101
    /* whole disk contains one single slice */
102
    sliceStart = 0;
103
    fseek(diskFile, 0, SEEK_END);
104
    sliceSize = ftell(diskFile) / SSIZE;
105
  } else {
106
    /* argv[3] is partition number of NetBSD slice */
107
    part = strtoul(argv[3], &endptr, 10);
108
    if (*endptr != '\0' || part < 0 || part > 15) {
109
      error("illegal partition number '%s'", argv[3]);
110
    }
111
    fseek(diskFile, 1 * SSIZE, SEEK_SET);
112
    if (fread(partTable, 1, SSIZE, diskFile) != SSIZE) {
113
      error("cannot read partition table of disk '%s'", diskName);
114
    }
115
    ptptr = partTable + part * 32;
116
    partType = read4FromEco(ptptr + 0);
117
    if ((partType & 0x7FFFFFFF) != 0x000000A9) {
118
      error("partition %d of disk '%s' is not a NetBSD slice",
119
            part, diskName);
120
    }
121
    sliceStart = read4FromEco(ptptr + 4);
122
    sliceSize = read4FromEco(ptptr + 8);
123
  }
124
  printf("Slice size is %lu (0x%lX) sectors of %d bytes each.\n",
125
         sliceSize, sliceSize, SSIZE);
126
  /* read disklabel from disk */
127
  fseek(diskFile, (sliceStart + 1) * SSIZE, SEEK_SET);
128
  if (fread(diskLabel, 1, SSIZE, diskFile) != SSIZE) {
129
    error("cannot read disklabel of disk '%s'", diskName);
130
  }
131
  printf("Disklabel read from disk '%s', %d bytes\n", diskName, SSIZE);
132
  /* copy disklabel to bootblock in memory */
133
  for (i = 0; i < SSIZE; i++) {
134
    bootCode[SSIZE + i] = diskLabel[i];
135
  }
136
  /* write bootblock to disk */
137
  fseek(diskFile, (sliceStart + 0) * SSIZE, SEEK_SET);
138
  if (fwrite(bootCode, 1, bootSize, diskFile) != bootSize) {
139
    error("cannot write bootblock to disk '%s'", diskName);
140
  }
141
  printf("Bootblock written to disk '%s', %d bytes\n", diskName, bootSize);
142
  fclose(diskFile);
143
  return 0;
144
}

powered by: WebSVN 2.1.0

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