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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [mkpart/] [mkptbl/] [mkptbl.c] - Blame information for rev 266

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

Line No. Rev Author Line
1 18 hellwig
/*
2
 * mkptbl.c -- make partition table
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
#define NPE             (SECTOR_SIZE / sizeof(PartEntry))
14
#define DESCR_SIZE      20
15
 
16
#define LINE_SIZE       100
17
 
18
 
19
typedef struct {
20 259 hellwig
  unsigned int type;
21
  unsigned int start;
22
  unsigned int size;
23 18 hellwig
  char descr[DESCR_SIZE];
24
} PartEntry;
25
 
26
PartEntry ptr[NPE];
27
 
28
 
29
void error(char *fmt, ...) {
30
  va_list ap;
31
 
32
  va_start(ap, fmt);
33
  printf("Error: ");
34
  vprintf(fmt, ap);
35
  printf("\n");
36
  va_end(ap);
37
  exit(1);
38
}
39
 
40
 
41 259 hellwig
void convertNumber(unsigned char *p, unsigned int val) {
42 18 hellwig
  *(p + 0) = val >> 24;
43
  *(p + 1) = val >> 16;
44
  *(p + 2) = val >>  8;
45
  *(p + 3) = val >>  0;
46
}
47
 
48
 
49
void convertPartitionTable(PartEntry *e, int n) {
50
  int i;
51
  unsigned char *p;
52
 
53
  for (i = 0; i < n; i++) {
54
    p = (unsigned char *) &e[i];
55
    convertNumber(p + 0, e[i].type);
56
    convertNumber(p + 4, e[i].start);
57
    convertNumber(p + 8, e[i].size);
58
  }
59
}
60
 
61
 
62 259 hellwig
int parseNumber(char **pc, unsigned int *pi) {
63 18 hellwig
  char *p;
64
  unsigned int base, dval;
65 259 hellwig
  unsigned int n;
66 18 hellwig
 
67
  p = *pc;
68
  while (*p == ' ' || *p == '\t') {
69
    p++;
70
  }
71
  if (*p == '\0' || *p == '\n') {
72
    printf("Error: number is missing!\n");
73
    return 0;
74
  }
75
  base = 10;
76
  if (*p == '0') {
77
    p++;
78
    if (*p != '\0' && *p != '\n') {
79
      if (*p == 'x' || *p == 'X') {
80
        base = 16;
81
        p++;
82
      } else {
83
        base = 8;
84
      }
85
    }
86
  }
87
  n = 0;
88
  while ((*p >= '0' && *p <= '9') ||
89
         (*p >= 'a' && *p <= 'f') ||
90
         (*p >= 'A' && *p <= 'F')) {
91
    if (*p >= '0' && *p <= '9') {
92
      dval = (*p - '0');
93
    } else
94
    if (*p >= 'a' && *p <= 'f') {
95
      dval = (*p - 'a' + 10);
96
    } else
97
    if (*p >= 'A' && *p <= 'F') {
98
      dval = (*p - 'A' + 10);
99
    }
100
    if (dval >= base) {
101
      printf("Error: digit value %d is illegal in number base %d\n",
102
             dval, base);
103
      return 0;
104
    }
105
    n *= base;
106
    n += dval;
107
    p++;
108
  }
109
  while (*p == ' ' || *p == '\t') {
110
    p++;
111
  }
112
  *pc = p;
113
  *pi = n;
114
  return 1;
115
}
116
 
117
 
118
int parseString(char **pc, char *dst) {
119
  char *p;
120
 
121
  p = *pc;
122
  while (*p == ' ' || *p == '\t') {
123
    p++;
124
  }
125
  if (*p != '\"') {
126
    return 0;
127
  }
128
  p++;
129
  while (*p != '\"' && *p != '\0' && *p != '\n') {
130
    *dst++ = *p++;
131
  }
132
  if (*p != '\"') {
133
    return 0;
134
  }
135
  p++;
136
  while (*p == ' ' || *p == '\t') {
137
    p++;
138
  }
139
  *pc = p;
140
  *dst = '\0';
141
  return 1;
142
}
143
 
144
 
145
int main(int argc, char *argv[]) {
146
  char *confName;
147
  char *outName;
148
  FILE *conf;
149
  FILE *out;
150
  char line[LINE_SIZE];
151
  char *p;
152
  int lineNumber;
153 259 hellwig
  unsigned int partNum;
154
  unsigned int bootable;
155
  unsigned int partType;
156
  unsigned int partStart;
157
  unsigned int partLast;
158
  unsigned int partSize;
159 18 hellwig
  char descr[LINE_SIZE];
160
 
161
  /* check command line arguments */
162
  if (argc != 3) {
163
    printf("Usage: %s <configuration file> <output file>\n", argv[0]);
164
    exit(1);
165
  }
166
  confName = argv[1];
167
  outName = argv[2];
168
  /* create partition table */
169
  conf = fopen(confName, "rt");
170
  if (conf == NULL) {
171
    error("cannot open configuration file '%s'", confName);
172
  }
173
  lineNumber = 0;
174
  /* handle partition table entries */
175
  while (fgets(line, LINE_SIZE, conf) != NULL) {
176
    lineNumber++;
177
    p = line;
178
    while (*p == ' ' || *p == '\t') {
179
      p++;
180
    }
181
    if (*p == '\0' || *p == '\n' || *p == '#') {
182
      continue;
183
    }
184
    if (!parseNumber(&p, &partNum)) {
185
      error("cannot read partition number in config file '%s', line %d",
186
            confName, lineNumber);
187
    }
188
    if (partNum >= 16) {
189
      error("illegal partition number in config file '%s', line %d",
190
            confName, lineNumber);
191
    }
192
    if (*p == '*') {
193
      p++;
194
      bootable = 0x80000000;
195
    } else {
196
      bootable = 0x00000000;
197
    }
198
    if (!parseNumber(&p, &partType)) {
199
      error("cannot read partition type in config file '%s', line %d",
200
            confName, lineNumber);
201
    }
202
    if ((partType & 0x80000000) != 0) {
203
      error("illegal partition type in config file '%s', line %d",
204
            confName, lineNumber);
205
    }
206
    if (!parseNumber(&p, &partStart)) {
207
      error("cannot read start sector in config file '%s', line %d",
208
            confName, lineNumber);
209
    }
210
    if (partStart < 32) {
211
      error("illegal start sector in config file '%s', line %d",
212
            confName, lineNumber);
213
    }
214
    if (!parseNumber(&p, &partLast)) {
215
      error("cannot read last sector in config file '%s', line %d",
216
            confName, lineNumber);
217
    }
218
    if (partLast < partStart) {
219
      error("illegal last sector in config file '%s', line %d",
220
            confName, lineNumber);
221
    }
222
    partSize = partLast - partStart + 1;
223
    if (!parseString(&p, descr)) {
224
      error("cannot read description in config file '%s', line %d",
225
            confName, lineNumber);
226
    }
227
    if (strlen(descr) >= DESCR_SIZE) {
228
      error("description too long in config file '%s', line %d",
229
            confName, lineNumber);
230
    }
231
    if (partType != 0) {
232
      ptr[partNum].type = bootable | partType;
233
      ptr[partNum].start = partStart;
234
      ptr[partNum].size = partSize;
235
      memset(ptr[partNum].descr, 0, DESCR_SIZE);
236
      strcpy(ptr[partNum].descr, descr);
237
    } else {
238
      ptr[partNum].type = 0;
239
      ptr[partNum].start = 0;
240
      ptr[partNum].size = 0;
241
      memset(ptr[partNum].descr, 0, DESCR_SIZE);
242
    }
243
  }
244
  fclose(conf);
245
  /* next, show partition table */
246
  printf("Partitions:\n");
247
  printf(" # b type       start      last       size       description\n");
248
  for (partNum = 0; partNum < NPE; partNum++) {
249
    if (ptr[partNum].type != 0) {
250
      partLast = ptr[partNum].start + ptr[partNum].size - 1;
251
    } else {
252
      partLast = 0;
253
    }
254 259 hellwig
    printf("%2u %s 0x%08X 0x%08X 0x%08X 0x%08X %s\n",
255 18 hellwig
           partNum,
256
           ptr[partNum].type & 0x80000000 ? "*" : " ",
257
           ptr[partNum].type & 0x7FFFFFFF,
258
           ptr[partNum].start,
259
           partLast,
260
           ptr[partNum].size,
261
           ptr[partNum].descr);
262
  }
263
  /* finally, write partition table to output file */
264
  convertPartitionTable(ptr, NPE);
265
  out = fopen(outName, "wb");
266
  if (out == NULL) {
267
    error("cannot open output file '%s'", outName);
268
  }
269
  if (fwrite(ptr, 1, SECTOR_SIZE, out) != SECTOR_SIZE) {
270
    error("cannot write partition table to output file '%s'", outName);
271
  }
272
  fclose(out);
273
  /* done */
274
  return 0;
275
}

powered by: WebSVN 2.1.0

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