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

Subversion Repositories eco32

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

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

powered by: WebSVN 2.1.0

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