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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [disk/] [tools/] [mkmboot/] [stage2/] [mboot.c] - Blame information for rev 261

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 hellwig
/*
2
 * mboot.c -- the master bootstrap (boot manager)
3
 */
4
 
5
 
6
#include "stdarg.h"
7
#include "biolib.h"
8
 
9
 
10
#define DEFAULT_PARTITION       ""      /* default boot partition number */
11
 
12 87 hellwig
#define LOAD_ADDR               0xC0010000
13
 
14 17 hellwig
#define LINE_SIZE               80
15
#define SECTOR_SIZE             512
16
#define NPE                     (SECTOR_SIZE / sizeof(PartEntry))
17
#define DESCR_SIZE              20
18
 
19
 
20
unsigned int bootDisk = 0;       /* gets loaded by previous stage */
21
unsigned int startSector = 0;    /* gets loaded by previous stage */
22
unsigned int numSectors = 0;     /* gets loaded by previous stage */
23
 
24
 
25
typedef struct {
26 258 hellwig
  unsigned int type;
27
  unsigned int start;
28
  unsigned int size;
29 17 hellwig
  char descr[DESCR_SIZE];
30
} PartEntry;
31
 
32
PartEntry ptr[NPE];
33
 
34
 
35
int strlen(char *str) {
36
  int i;
37
 
38
  i = 0;
39
  while (*str++ != '\0') {
40
    i++;
41
  }
42
  return i;
43
}
44
 
45
 
46
void strcpy(char *dst, char *src) {
47
  while ((*dst++ = *src++) != '\0') ;
48
}
49
 
50
 
51
char getchar(void) {
52
  return getc();
53
}
54
 
55
 
56
void putchar(char c) {
57
  if (c == '\n') {
58
    putchar('\r');
59
  }
60
  putc(c);
61
}
62
 
63
 
64
void puts(char *s) {
65
  char c;
66
 
67
  while ((c = *s++) != '\0') {
68
    putchar(c);
69
  }
70
}
71
 
72
 
73
void getline(char *prompt, char *line, int n) {
74
  int i;
75
  char c;
76
 
77
  puts(prompt);
78
  puts(line);
79
  i = strlen(line);
80
  while (i < n - 1) {
81
    c = getchar();
82
    if (c >= ' ' && c < 0x7F) {
83
      putchar(c);
84
      line[i] = c;
85
      i++;
86
    } else
87
    if (c == '\r') {
88
      putchar('\n');
89
      line[i] = '\0';
90
      i = n - 1;
91
    } else
92
    if (c == '\b' || c == 0x7F) {
93
      if (i > 0) {
94
        putchar('\b');
95
        putchar(' ');
96
        putchar('\b');
97
        i--;
98
      }
99
    }
100
  }
101
  line[n - 1] = '\0';
102
}
103
 
104
 
105 261 hellwig
int countPrintn(int n) {
106
  int a;
107 17 hellwig
  int res;
108
 
109
  res = 0;
110
  if (n < 0) {
111
    res++;
112
    n = -n;
113
  }
114
  a = n / 10;
115
  if (a != 0) {
116
    res += countPrintn(a);
117
  }
118
  return res + 1;
119
}
120
 
121
 
122 261 hellwig
void printn(int n) {
123
  int a;
124 17 hellwig
 
125
  if (n < 0) {
126
    putchar('-');
127
    n = -n;
128
  }
129
  a = n / 10;
130
  if (a != 0) {
131
    printn(a);
132
  }
133
  putchar(n % 10 + '0');
134
}
135
 
136
 
137
void printf(char *fmt, ...) {
138
  va_list ap;
139
  char c;
140
  int n;
141
  unsigned int u;
142
  char *s;
143
  char filler;
144
  int width, count, i;
145
 
146
  va_start(ap, fmt);
147
  while (1) {
148
    while ((c = *fmt++) != '%') {
149
      if (c == '\0') {
150
        va_end(ap);
151
        return;
152
      }
153
      putchar(c);
154
    }
155
    c = *fmt++;
156
    if (c == '0') {
157
      filler = '0';
158
      c = *fmt++;
159
    } else {
160
      filler = ' ';
161
    }
162
    width = 0;
163
    if (c >= '0' && c <= '9') {
164
      width = c - '0';
165
      c = *fmt++;
166
    }
167
    if (c == 'd') {
168
      n = va_arg(ap, int);
169
      if (width > 0) {
170
        count = countPrintn(n);
171
        for (i = 0; i < width - count; i++) {
172
          putchar(filler);
173
        }
174
      }
175
      printn(n);
176
    } else
177
    if (c == 's') {
178
      s = va_arg(ap, char *);
179
      puts(s);
180
    } else
181
    if (c == 'c') {
182
      c = va_arg(ap, char);
183
      putchar(c);
184
    } else {
185
      putchar(c);
186
    }
187
  }
188
}
189
 
190
 
191
void halt(void) {
192
  printf("bootstrap halted\n");
193
  while (1) ;
194
}
195
 
196
 
197
void readDisk(unsigned int sector, unsigned char *buffer, int count) {
198
  int result;
199
 
200
  if (sector + count > numSectors) {
201
    printf("sector number exceeds disk or partition size\n");
202
    halt();
203
  }
204
  result = rwscts(bootDisk, 'r', sector + startSector,
205
                  (unsigned int) buffer & 0x3FFFFFFF, count);
206
  if (result != 0) {
207
    printf("disk read error\n");
208
    halt();
209
  }
210
}
211
 
212
 
213
unsigned int entryPoint;        /* where to continue from main() */
214
 
215
 
216
int main(void) {
217
  int i;
218
  char line[LINE_SIZE];
219
  char *p;
220
  int part;
221
 
222
  printf("Bootstrap manager executing...\n");
223
  strcpy(line, DEFAULT_PARTITION);
224
  readDisk(1, (unsigned char *) ptr, 1);
225
  while (1) {
226
    printf("\nPartitions:\n");
227
    printf(" # | b | description\n");
228
    printf("---+---+----------------------\n");
229
    for (i = 0; i < NPE; i++) {
230
      if (ptr[i].type != 0) {
231
        printf("%2d | %s | %s\n",
232
               i, ptr[i].type & 0x80000000 ? "*" : " ", ptr[i].descr);
233
      }
234
    }
235
    getline("\nBoot partition #: ", line, LINE_SIZE);
236
    part = 0;
237
    if (line[0] == '\0') {
238
      continue;
239
    }
240
    p = line;
241
    while (*p >= '0' && *p <= '9') {
242
      part = part * 10 + (*p - '0');
243
      p++;
244
    }
245
    if (*p != '\0' || part < 0 || part > 15) {
246
      printf("illegal partition number\n");
247
      continue;
248
    }
249
    if ((ptr[part].type & 0x7FFFFFFF) == 0) {
250
      printf("partition %d does not contain a file system\n", part);
251
      continue;
252
    }
253
    if ((ptr[part].type & 0x80000000) == 0) {
254
      printf("partition %d is not bootable\n", part);
255
      continue;
256
    }
257
    /* load boot sector of selected partition */
258 87 hellwig
    readDisk(ptr[part].start, (unsigned char *) LOAD_ADDR, 1);
259 17 hellwig
    /* check for signature */
260 87 hellwig
    if ((*((unsigned char *) LOAD_ADDR + SECTOR_SIZE - 2) != 0x55) ||
261
        (*((unsigned char *) LOAD_ADDR + SECTOR_SIZE - 1) != 0xAA)) {
262 17 hellwig
      printf("boot sector of partition %d has no signature\n", part);
263
      continue;
264
    }
265
    /* we have a valid boot sector, leave loop */
266
    break;
267
  }
268
  /* boot manager finished, now go executing loaded boot sector */
269
  startSector = ptr[part].start;
270
  numSectors = ptr[part].size;
271 87 hellwig
  entryPoint = LOAD_ADDR;
272 17 hellwig
  return 0;
273
}

powered by: WebSVN 2.1.0

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