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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [cmds/] [load.c] - Blame information for rev 393

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

Line No. Rev Author Line
1 389 tac2
#include "common.h" 
2 2 marcus.erl
#include "support.h"
3
#include "flash.h"
4
#include "net.h"
5
#include "uart.h"
6 389 tac2
#include "dosfs.h"
7 246 julius
#include "spr-defs.h"
8 2 marcus.erl
 
9
#ifndef MAX_IMAGES
10
#define MAX_IMAGES 20
11
#endif
12
 
13
extern unsigned long fprog_addr;
14
extern char *tftp_filename;
15
 
16 140 julius
static flash_cfg_struct gcfg =
17
  { BOARD_DEF_IP, BOARD_DEF_MASK, BOARD_DEF_GW,  BOARD_DEF_TBOOT_SRVR };
18 2 marcus.erl
 
19 140 julius
// Not booting from flash, so just set these from board.h -- jb
20
//static flash_cfg_struct __attribute__ ((section(".config"))) gcfg = { BOARD_DEF_IP, BOARD_DEF_MASK, BOARD_DEF_GW,  BOARD_DEF_TBOOT_SRVR };
21
//static flash_cfg_struct __attribute__ ((section(".config"))) gcfg = { 0, 0, 0, 0 };
22
 
23 2 marcus.erl
#define FLASH_IMAGES_BASE 0xf0300000
24
 
25
#define ALIGN(addr,size) ((addr + (size-1))&(~(size-1)))
26 140 julius
// If the image buffer is word aligned, then uncomment this, but it was set up 
27
// so that the tftp images would download quicker
28
#define COPY_AND_BOOT_WORD_ALIGNED
29
#ifdef COPY_AND_BOOT_WORD_ALIGNED
30 2 marcus.erl
void copy_and_boot(unsigned long src,
31
                   unsigned long dst,
32
                   unsigned long len,
33
                   int tx_next)
34
{
35
  __asm__ __volatile__("   ;\
36
        l.addi  r8,r0,0x1  ;\
37
        l.mtspr r0,r8,0x11  ;\
38
        l.nop               ;\
39
        l.nop               ;\
40
        l.nop               ;\
41
        l.nop               ;\
42
        l.nop               ;\
43
2:                          ;\
44
        l.sfgeu r4,r5       ;\
45
        l.bf    1f          ;\
46
        l.nop               ;\
47
        l.lwz   r8,0(r3)    ;\
48
        l.sw    0(r4),r8    ;\
49
        l.addi  r3,r3,4     ;\
50
        l.j     2b          ;\
51
        l.addi  r4,r4,4     ;\
52
1:      l.sw    0x0(r0),r6  ;\
53
        l.ori   r8,r0,0x100 ;\
54
        l.jr    r8          ;\
55
        l.nop");
56
}
57 140 julius
#else
58
void copy_and_boot(unsigned long src,
59
                   unsigned long dst,
60
                   unsigned long len,
61
                   int tx_next)
62
{
63
  __asm__ __volatile__("   ;\
64
        l.addi  r8,r0,0x1  ;\
65
        l.mtspr r0,r8,0x11  ;\
66
        l.nop               ;\
67
        l.nop               ;\
68
        l.nop               ;\
69
        l.nop               ;\
70
        l.nop               ;\
71
2:                          ;\
72
        l.sfgeu r4,r5       ;\
73
        l.bf    1f          ;\
74
        l.nop               ;\
75
        l.lbz   r8,0(r3)    ;\
76
        l.sb    0(r4),r8    ;\
77
        l.addi  r3,r3,1     ;\
78
        l.j     2b          ;\
79
        l.addi  r4,r4,1     ;\
80
1:      l.sw    0x0(r0),r6  ;\
81
        l.ori   r8,r0,0x100 ;\
82
        l.jr    r8          ;\
83
        l.nop");
84
}
85
#endif
86
/* WARNING: stack and non-const globals should not be used in this function
87
   -- it may corrupt what have we loaded;
88 2 marcus.erl
   start_addr should be 0xffffffff if only copying should be made
89
   no return, when start_addr != 0xffffffff, if successful */
90 140 julius
int copy_memory_run (register unsigned long src_addr,
91
                     register unsigned long dst_addr,
92
                     register unsigned long length,
93
                     register int erase,
94
                     register unsigned long start_addr)
95 2 marcus.erl
{
96
  unsigned long i, flags;
97
 
98
  register char *dst = (char *) dst_addr;
99
  register const char *src = (const char *) src_addr;
100
 
101
  if (dst_addr >= FLASH_BASE_ADDR) {
102
    if (dst_addr + length >= FLASH_BASE_ADDR + FLASH_SIZE) {
103
      printf ("error: region does not fit into flash.\n");
104
       return 1;
105
    }
106
#ifndef CFG_IN_FLASH
107
    fl_program (src_addr, dst_addr, length, erase, 1 /* do verify */);
108
#else
109
    /* we must disable interrupts! */
110
    flags=mfspr(SPR_SR);
111
    mtspr(SPR_SR,flags & ~(SPR_SR_TEE | SPR_SR_IEE));
112
 
113
    printf("Unlocking flash... ");
114
    for(i = 0; i < length; i += FLASH_BLOCK_SIZE)
115
      fl_ext_unlock(dst_addr + i);
116
    printf("done\n");
117
 
118
    printf("Erasing flash... ");
119
    for(i = 0; i < length; i += FLASH_BLOCK_SIZE)
120
      fl_ext_erase(dst_addr+i);
121
    printf("done\n");
122
 
123
    printf("Programing flash:\n\t");
124
    for (i = 0; i < length; i += INC_ADDR) {
125
      if(((i+INC_ADDR) % 1000) == 0)
126
        printf("#");
127
      if((i % (65*1000)) == 0)
128
        printf("\n\t");
129
      if (fl_ext_program (dst_addr + i, reg_read(src_addr + i))) {
130
        printf("error programing at 0x%08lx!\n", dst_addr+i);
131
        return 1;
132
      }
133
    }
134
    printf("Verifying flash... ");
135
    for(i = 0; i < length; i += INC_ADDR) {
136
      if( reg_read(dst_addr+i) != reg_read(src_addr + i)) {
137 140 julius
        printf ("error at %08lx: %08lx != %08lx\n", src_addr + i,
138
                reg_read(src_addr + i), reg_read(dst_addr + i));
139 2 marcus.erl
        return 1;
140
      }
141
    }
142
    printf("OK!\n");
143
    mtspr(SPR_SR, flags);
144
#endif
145
    if(start_addr == 0xffffffff)
146
      return 0;
147
  }
148
  else {
149
    while (length--) *dst++ = *src++;
150
    if (start_addr == 0xffffffff)
151
      return 0;
152
  }
153
  /* Run the program */
154
  ((void (*)(void)) start_addr)();
155
  return 0; /* just to satisfy the cc */
156
}
157
 
158
void bf_jump(unsigned long addr)
159
{
160
  asm("l.jr   r3");
161
  asm("l.nop  0x0");
162
}
163
 
164
int boot_flash_cmd(int argc, char *argv[])
165
{
166
  unsigned long addr,val,jaddr;
167
  addr = 17;
168
  val = 0;
169
  /* clear SR */
170
 
171
  asm("l.mtspr %0,%1,0": : "r" (addr), "r" (val));
172
  /* jump */
173
  if(argc == 0)
174
    bf_jump(FLASH_BASE_ADDR+0x100);
175
  else {
176
    jaddr = strtoul(argv[0], 0, 0);
177
    bf_jump(jaddr);
178
  }
179
  return 0;
180
}
181
 
182
void
183
init_load (void)
184
{
185 140 julius
#if 0 // JB - removing flash stuff
186
#  ifdef CFG_IN_FLASH
187 2 marcus.erl
  copy_memory_run((unsigned long)&fl_word_program, (unsigned long)&fprog_addr,
188
                  95, 0, 0xffffffff);
189 140 julius
  copy_memory_run((unsigned long)&fl_block_erase, (unsigned long)&fprog_addr+96,
190 2 marcus.erl
                  119, 0, 0xffffffff);
191
  copy_memory_run((unsigned long)&fl_unlock_one_block,
192
                  (unsigned long)&fprog_addr+96+120,
193
                  115, 0, 0xffffffff);
194
 
195
  fl_ext_program = (t_fl_ext_program)&fprog_addr;
196
  fl_ext_erase = (t_fl_erase)&fprog_addr+96;
197
  fl_ext_unlock = (t_fl_erase)&fprog_addr+96+120;
198
 
199 140 julius
#    if 0
200 2 marcus.erl
  printf("fl_word_program(): 0x%x\tfl_ext_program(): 0x%x\n",
201
         &fl_word_program, fl_ext_program);
202
  printf("fl_block_erase: 0x%x\tfl_ext_erase(): 0x%x\n",
203
         &fl_block_erase, fl_ext_erase);
204
  printf("fl_unlock_one_block(): 0x%x\tfl_ext_unlock(): 0x%x\n",
205
         &fl_unlock_one_block, fl_ext_unlock);
206 140 julius
#    endif
207 2 marcus.erl
 
208 140 julius
#  else /* not CFG_IN_FLASH */
209 2 marcus.erl
  fl_ext_program = (t_fl_ext_program)&fl_word_program;
210
  fl_ext_erase = (t_fl_erase)&fl_block_erase;
211
  fl_ext_unlock = (t_fl_erase)&fl_unlock_one_block;
212 140 julius
#  endif /* CFG_IN_FLASH */
213
#endif
214 2 marcus.erl
 
215 140 julius
  /*
216 2 marcus.erl
  global.ip = gcfg.eth_ip;
217
  global.gw_ip = gcfg.eth_gw;
218
  global.mask = gcfg.eth_mask;
219
  global.srv_ip = gcfg.tftp_srv_ip;
220 140 julius
  global.src_addr = 0x100000;
221 2 marcus.erl
  tftp_filename = "boot.img";
222 140 julius
  */
223
 
224
  global.ip = BOARD_DEF_IP;
225
  global.gw_ip = BOARD_DEF_GW;
226
  global.mask = BOARD_DEF_MASK;
227
  global.srv_ip = BOARD_DEF_TBOOT_SRVR;
228
  global.src_addr = BOARD_DEF_LOAD_SPACE;
229
  tftp_filename = BOARD_DEF_IMAGE_NAME;
230
 
231 2 marcus.erl
  /*memcpy(tftp_filename, gcfg.tftp_filename, strlen(gcfg.tftp_filename));
232
    tftp_filename[strlen(gcfg.tftp_filename)] = '\0';*/
233
}
234
 
235
int tftp_cmd (int argc, char *argv[])
236
{
237
  switch (argc) {
238
    case 0: tftp_filename = "boot.img";
239
                  break;
240
    case 3: global.src_addr = strtoul (argv[2], 0, 0);
241
    case 2: global.srv_ip = parse_ip (argv[1]);
242
    case 1: tftp_filename = &argv[0][0];
243
            break;
244
  }
245
  NetLoop(TFTP);
246
  return 0;
247
}
248
 
249
int tftp_conf_cmd(int argc, char *argv[])
250
{
251
  switch(argc) {
252
  case 0:
253
    printf("Image filename: %s", tftp_filename);
254
    printf("\nSrc addr: 0x%lx", global.src_addr);
255
    printf("\nServer IP: %s", inet_ntoa(global.srv_ip));
256
    return 0;
257
  case 3:
258
    global.src_addr = strtoul(argv[2], 0, 0);
259
    global.srv_ip = inet_aton(argv[1]);
260
    tftp_filename = argv[0];
261
    tftp_filename[strlen(argv[0])] = '\0';
262
    break;
263
  case 2:
264
    global.srv_ip = inet_aton(argv[1]);
265
    tftp_filename = argv[0];
266
    break;
267
  case 1:
268
    tftp_filename = argv[0];
269
    break;
270
  }
271
  return 0;
272
}
273
 
274
void save_global_cfg(flash_cfg_struct cfg)
275
{
276
  unsigned long dst = (unsigned long)&gcfg, src = (unsigned long)&cfg;
277
  unsigned long i, end, flags;
278
 
279
  end = (unsigned long)&cfg + sizeof(flash_cfg_struct);
280
 
281
  printf("Saving global cfg from 0x%lx (end: 0x%lx) to 0x%lx...", src, end, dst);
282
 
283
  /* we must disable interrupts! */
284
  flags=mfspr(SPR_SR);
285
  mtspr(SPR_SR,flags & ~(SPR_SR_TEE | SPR_SR_IEE));
286
  /*  printf("Unlocking... ");*/
287
  for(i = 0; (src+i <= end); i += FLASH_BLOCK_SIZE) {
288
    fl_ext_unlock(dst+i);
289
  }
290
  /*  printf("done\n");*/
291
  /*  printf("Erasing... ");*/
292
  for(i = 0; (src+i <= end); i += FLASH_BLOCK_SIZE)
293
    fl_ext_erase(dst);
294
  /*  printf("done\n");*/
295
  /*  printf("Programing... ");*/
296
  for(i = 0; (src+i <= end); i +=INC_ADDR) {
297
    if(fl_ext_program(dst+i, reg_read(src+i))) {
298
      printf("Error ocurred while saving.\n");
299
      return;
300
    }
301
  }
302
  printf("done\n");
303
 
304
  /* and than enable it back */
305
  mtspr(SPR_SR, flags);
306
  return;
307
}
308
 
309
int save_conf_cmd(int argc, char *argv[])
310
{
311
  flash_cfg_struct newCfg;
312
 
313
  newCfg = gcfg;
314
 
315
  newCfg.eth_ip = global.ip;
316
  newCfg.eth_mask = global.mask;
317
  newCfg.eth_gw = global.gw_ip;
318
  newCfg.tftp_srv_ip = global.srv_ip;
319
  /*  memcpy(newCfg.tftp_filename, tftp_filename, strlen(tftp_filename));*/
320
 
321
  save_global_cfg(newCfg);
322
  return 0;
323
}
324
int copy_cmd (int argc, char *argv[])
325
{
326
  switch (argc) {
327
    case 3: global.src_addr = strtoul (argv[2], 0, 0);
328
    case 2: global.length = strtoul (argv[2], 0, 0);
329
    case 1: global.src_addr = strtoul (argv[2], 0, 0);
330 140 julius
    case 0: return copy_memory_run (global.src_addr, global.dst_addr,
331
                                    global.length, global.erase_method,
332
                                    0xffffffff);
333 2 marcus.erl
  }
334
  return -1;
335
}
336
 
337
void
338
images_info(void)
339
{
340
  int i;
341
  printf("Number of images: 0x%lx\n", gcfg.img_number);
342
  for(i = 0; i < gcfg.img_number; i++)
343
    printf("%d. image size: 0x%lx (at 0x%08lx)\n", i+1,
344
           gcfg.img_length[i], gcfg.img_start_addr[i]);
345
}
346
 
347
/*
348
 * get_good_addr()
349
 *
350
 * Here we try to find the most suitable place for our image. We search for
351
 * a hole between images, that is big enough (but as small as possible).
352
 *
353
 */
354
unsigned long
355
get_good_addr(unsigned int size)
356
{
357
  unsigned long start_addr[MAX_IMAGES], end_addr[MAX_IMAGES];
358
  unsigned long free[MAX_IMAGES], st_addr[MAX_IMAGES];
359
  unsigned long tmpval;
360
  unsigned int i = 0, j;
361
 
362
  flash_cfg_struct myCfg;
363
  myCfg = gcfg;
364
 
365
  /* we are full */
366
  if(gcfg.img_number == MAX_IMAGES)
367
    return 0xffffffff;
368
 
369
  if(gcfg.img_number == 0)
370
    return FLASH_IMAGES_BASE;
371
 
372
  for(i = 0; i < MAX_IMAGES; i++) {
373
    start_addr[i] = 0;
374
    end_addr[i] = 0;
375
    free[i] = 0;
376
    st_addr[i] = 0;
377
  }
378
 
379
  for(i = 0; i < myCfg.img_number; i++) {
380
    start_addr[i] = myCfg.img_start_addr[i];
381
    end_addr[i] = ALIGN((myCfg.img_start_addr[i] + myCfg.img_length[i]),
382
                        FLASH_BLOCK_SIZE);
383
  }
384
  /*  printf("\n");
385
  for(i = 0; i < myCfg.img_number; i++)
386
    printf("start: 0x%08x, end: 0x%08x\n", start_addr[i], end_addr[i]);
387
    printf("\n");*/
388
  /* bubble sorting by start_addr */
389
 
390
  for(j = myCfg.img_number - 1; j > 0; j--)
391
    for(i = 0; i < j; i++)
392
      if(start_addr[i] > start_addr[i+1]) {
393
        tmpval = start_addr[i];
394
        start_addr[i] = start_addr[i+1];
395
        start_addr[i+1] = tmpval;
396
        tmpval = end_addr[i];
397
        end_addr[i] = end_addr[i+1];
398
        end_addr[i+1] = tmpval;
399
      }
400
 
401
  /*  for(i = 0; i < myCfg.img_number; i++)
402
    printf("start: 0x%08x, end: 0x%08x\n", start_addr[i], end_addr[i]);
403
    printf("\n");*/
404
 
405
  /* now we calculate free space betwens segments */
406
  for(i = 1; i < myCfg.img_number; i++) {
407
    st_addr[i] = end_addr[i - 1];
408
    free[i] = start_addr[i] - end_addr[i - 1];
409
  }
410
 
411
  /* here we calcuta first position (starting with FLASH_IMAGES_BASE)... */
412
  st_addr[0] = FLASH_IMAGES_BASE + 0;
413
  free[0] = start_addr[0] - FLASH_IMAGES_BASE;
414
  /* ... and last one (ending with FLASH_IMAGES_BASE + FLASH_SIZE). */
415
  st_addr[myCfg.img_number] = end_addr[myCfg.img_number-1];
416 140 julius
  free[myCfg.img_number] = (FLASH_IMAGES_BASE + FLASH_SIZE) -
417
    end_addr[myCfg.img_number-1];
418 2 marcus.erl
 
419
  /* yet another bubble sort by free (space) */
420
  for(j = myCfg.img_number; j > 0; j--)
421
    for(i = 0; i < j; i++)
422
      if(free[i] > free[i+1]) {
423
        tmpval = free[i];
424
        free[i] = free[i+1];
425
        free[i+1] = tmpval;
426
        tmpval = st_addr[i];
427
        st_addr[i] = st_addr[i+1];
428
        st_addr[i+1] = tmpval;
429
      }
430
 
431
  /* now we pick the smallest but just big enough for our size */
432
  for(i = 0; i <= myCfg.img_number; i++)
433
    if(free[i] >= size)
434
      return st_addr[i];
435
 
436
  /* there is not enough space (in one segment) left */
437
  return 0;
438
}
439
 
440
unsigned long
441
prepare_img_data(unsigned int num, unsigned int size)
442
{
443
  int i;
444
  unsigned long addr=0;
445
  flash_cfg_struct newCfg;
446
 
447
  newCfg = gcfg;
448
 
449
  if(newCfg.img_number >= MAX_IMAGES) {
450
    printf("Maximum images exceeded: %d\n", MAX_IMAGES);
451
    return 0xffffffff;
452
  }
453
 
454
  newCfg.img_number++;
455
 
456
  if((num > newCfg.img_number) || (num == 0))
457
    num = newCfg.img_number;
458
 
459
  addr = get_good_addr(size);
460
  if(addr == 0x00) {
461
    printf("Can not find suitable place in flash. (None of free segments are big enough)\n");
462
    return 0xffffffff;
463
  }
464
 
465
  if(num < newCfg.img_number)
466
    for(i=newCfg.img_number-1; i >= num; i--) {
467
      newCfg.img_length[i] = newCfg.img_length[i-1];
468
      newCfg.img_start_addr[i] = newCfg.img_start_addr[i-1];
469
    }
470
 
471
  newCfg.img_length[num-1] = size;
472
  newCfg.img_start_addr[num-1] = addr;
473
 
474
  save_global_cfg(newCfg);
475
  return addr;
476
}
477
 
478
int
479
del_image_cmd(int argc, char *argv[])
480
{
481
  unsigned num, i;
482
  flash_cfg_struct newCfg = gcfg;
483
 
484
  newCfg.img_number = gcfg.img_number;
485
  for(i = 0; i < MAX_IMAGES; i++)
486
    newCfg.img_length[i] = gcfg.img_length[i];
487
 
488
  printf("Number of images available: 0x%lx\n", newCfg.img_number);
489
 
490
  if(argc == 0) {
491
    newCfg.img_number = 0;
492
    for(i = 0; i < MAX_IMAGES; i++) {
493
      newCfg.img_length[i] = 0;
494
      newCfg.img_start_addr[i] = 0;
495
    }
496
    save_global_cfg(newCfg);
497
    return 0;
498
  }
499
  else {
500
    num = strtoul(argv[0], 0, 0);
501
  }
502
 
503
  if(newCfg.img_number == 0) {
504
    printf("Nothing to delete!\n");
505
    return 0;
506
  }
507
  if((num == 0) || (num > newCfg.img_number))
508
    num = newCfg.img_number;
509
 
510
  for(i=num-1; i < newCfg.img_number; i++) {
511
    newCfg.img_length[i] = newCfg.img_length[i+1];
512
    newCfg.img_start_addr[i] = newCfg.img_start_addr[i+1];
513
  }
514
 
515
  newCfg.img_number--;
516
  save_global_cfg(newCfg);
517
  return 0;
518
}
519
 
520
int
521
boot_cmd(int argc, char *argv[])
522
{
523
  int num;
524
  extern int tx_next;
525
 
526
  if(argc == 0) {
527
    images_info();
528
    return 0;
529
  }
530
 
531
  num = strtoul(argv[0],0,0);
532
  if(gcfg.img_number < num) {
533 140 julius
    printf("There are only %lu images, you requested %d!\n",
534
           gcfg.img_number, num);
535 2 marcus.erl
    return -1;
536
  }
537
 
538
  printf("Copying image number %d from 0x%lx, size: 0x%lx...",
539
         num, gcfg.img_start_addr[num-1], gcfg.img_length[num-1]);
540
 
541
  printf("booting...\n");
542 140 julius
  copy_and_boot(gcfg.img_start_addr[num-1], 0x0, gcfg.img_length[num-1],
543
                tx_next);
544 2 marcus.erl
  return 0;
545
}
546
 
547
int mGetData(unsigned long);
548
 
549 140 julius
#if 0 // Disable sboot - JB
550 2 marcus.erl
int sboot_cmd (int argc, char *argv[])
551
{
552
  int copied;
553
  unsigned int num = 0xffffffff, addr = 0x0;
554
 
555
  switch(argc) {
556
  case 0:
557
    num = 0xffffffff;
558
    break;
559
  case 1:
560
    num = strtoul(argv[0], 0, 0);
561
    break;
562
  }
563
 
564
  copied = mGetData(global.src_addr);
565
  if(copied <= 0) {
566
    printf("sboot: error while getting the image!");
567
    return -1;
568
  }
569
  printf("image size: 0x%x\n", copied);
570
 
571
  if(num != 0xffffffff) {
572
    addr = prepare_img_data(num, copied);
573
    if(addr == 0xffffffff)
574
      printf("Image not written to flash!\n");
575
    else {
576
      printf("Copying image to flash, image number: %d, dst_addr: 0x%x\n",
577
             num, addr);
578
      copy_memory_run(global.src_addr, gcfg.img_start_addr[num-1], copied, 2, 0xffffffff);
579
    }
580
  }
581
 
582
  return 0;
583
}
584 140 julius
#endif
585 2 marcus.erl
 
586 140 julius
void relocate_code(void* destination, void* function, int length_words)
587
{
588
  // Just copy the function word at a time from one place to another
589
  int i;
590
  unsigned long * p1 = (unsigned long*) destination;
591
  unsigned long * p2 = (unsigned long*) function;
592
  for(i=0;i<length_words;i++)
593
    p1[i] = p2[i];
594
}
595
 
596 246 julius
// DC disable command in cpu.c
597
extern int dc_disable_cmd(int argc, char *argv[]);
598
 
599 2 marcus.erl
int tboot_cmd (int argc, char *argv[])
600
{
601
  int copied;
602
  unsigned int num = 0xffffffff, addr = 0x0;
603
  extern int tx_next;
604 140 julius
  // NetTxPacket wasn't getting cleared before we used it...
605
  NetTxPacket = 0;
606
  NetBootFileSize = 0;
607 2 marcus.erl
 
608
  switch (argc) {
609 140 julius
  case 0:
610 2 marcus.erl
    num = 0xffffffff;
611
    break;
612
  case 1:
613
    printf("argv[0] %p\n", argv[0]);
614
    num = strtoul(argv[0], 0, 0);
615
    printf("num %d\n", num);
616
    break;
617
  }
618
 
619 246 julius
  // Disable data cache if present
620
  if (mfspr(SPR_SR) & SPR_SR_DCE)
621
    {
622
      printf("Disabling data cache\n");
623
      dc_disable_cmd(0, 0);
624
    }
625
 
626
  // Kick off copy
627 2 marcus.erl
  copied =NetLoop(TFTP);
628
  if (copied <= 0) {
629 140 julius
    printf("tboot: error while getting the image '%s'", tftp_filename);
630 2 marcus.erl
    return -1;
631
  }
632
 
633 140 julius
  if (global.src_addr > 0)
634
    {
635
      /* the point of no return */
636
      printf("tboot: copying 0x%lx -> 0x0, image size 0x%x...\n",
637
             global.src_addr, copied);
638 2 marcus.erl
    }
639
 
640 140 julius
  // Disable timer: clear it all!
641
  mtspr (SPR_SR, mfspr(SPR_SR) & ~SPR_SR_TEE);
642
  mtspr(SPR_TTMR, 0);
643
 
644
  // Put the copyboot program at 24MB mark in memory
645
#define COPYBOOT_LOCATION (1024*1024*24) 
646
  printf("tboot: relocating copy loop to 0x%x ...\n", (unsigned long)COPYBOOT_LOCATION);
647
  // Setup where we'll copy the relocation function to
648
  void (*relocated_function)(unsigned long, unsigned long, unsigned long, int)
649
    = (void*) COPYBOOT_LOCATION;
650
  // Now copy the function there, 32 words worth, increase this if needed...
651
  relocate_code((void*)COPYBOOT_LOCATION, copy_and_boot, 32);
652
  // Indicate we'll jump there...
653
  printf("tboot: Relocate (%d bytes from 0x%x to 0) and boot image, ...\n", copied, (unsigned long) global.src_addr);
654
  // Now do the copy and boot
655
  (*relocated_function)(global.src_addr, 0x0, 0x0 + copied, tx_next);
656
 
657 2 marcus.erl
  return 0;
658
}
659
 
660 389 tac2
int sdboot_cmd (int argc, char *argv[])
661
{
662
        VOLINFO vi;
663
        unsigned char *buf_ptr;
664
 
665
        unsigned char sector[SECTOR_SIZE], sector2[SECTOR_SIZE];
666
        FILEINFO fi;
667
        unsigned long int pstart,psize, i,fisz;
668
        unsigned char pactive, ptype;
669
        DIRINFO di;
670
        DIRENT de;
671
        unsigned long int cache;
672
 
673
                // Obtain pointer to first partition on first (only) unit
674
          // Disable data cache if present
675
  if (mfspr(SPR_SR) & SPR_SR_DCE)
676
    {
677
      printf("Disabling data cache\n");
678
      dc_disable_cmd(0, 0);
679
    }
680
 
681 140 julius
 
682 389 tac2
        buf_ptr=global.src_addr;
683
 
684
 
685
        printf("SD-BOOT start \n");
686
        i=init_fat(&vi);
687
 
688
 
689
 
690
        printf("Volume label '%-11.11s'\n", vi.label);
691
        printf("%d sector/s per cluster, %d reserved sector/s, volume total %d sectors.\n", vi.secperclus, vi.reservedsecs, vi.numsecs);
692
        printf("%d sectors per FAT, first FAT at sector #%d, root dir at #%d.\n",vi.secperfat,vi.fat1,vi.rootdir);
693
        printf("(For FAT32, the root dir is a CLUSTER number, FAT12/16 it is a SECTOR number)\n");
694
        printf("%d root dir entries, data area commences at sector #%d.\n",vi.rootentries,vi.dataarea);
695
        printf("%d clusters (%d bytes) in data area, filesystem IDd as ", vi.numclusters, vi.numclusters * vi.secperclus * SECTOR_SIZE);
696
        if (vi.filesystem == FAT12)
697
                printf("FAT12.\n");
698
        else if (vi.filesystem == FAT16)
699
                printf("FAT16.\n");
700
        else if (vi.filesystem == FAT32)
701
                printf("FAT32.\n");
702
        else
703
                printf("[unknown]\n");
704
 
705
 
706
 
707
        if (DFS_OpenDir(&vi, "", &di)) {
708
                        printf("Error opening root directory\n");
709
                return -1;
710
        }
711 140 julius
 
712 389 tac2
 
713
        printf("Readback test\n");
714
        if (DFS_OpenFile(&vi,"vmlinux.bin", DFS_READ, sector, &fi)) {
715
                printf("error opening file\n");
716
 
717
                return -1;
718
        }
719
 
720
        printf("fi.filen %d, pointer adress:%d, data:%d \n", fi.filelen, buf_ptr, *buf_ptr);
721
 
722
 
723
        DFS_ReadFile(&fi, sector, buf_ptr, &i, fi.filelen);
724
        printf("\n read complete %d bytes (expected %d) pointer %d\n", i, fi.filelen, fi.pointer);
725
 
726
 
727
 
728
      if (global.src_addr > 0)
729
      {
730
       /* the point of no return */
731
       printf("tboot: copying 0x%lx -> 0x0, image size 0x%x...\n",
732
              global.src_addr, i);
733
      }
734 140 julius
 
735 389 tac2
 
736
 
737
 
738
  // Disable timer: clear it all!
739
      mtspr (SPR_SR, mfspr(SPR_SR) & ~SPR_SR_TEE);
740
      mtspr(SPR_TTMR, 0);
741
 
742
      // Put the copyboot program at 24MB mark in memory
743
      #define COPYBOOT_LOCATION (1024*1024*24) 
744
      printf("tboot: relocating copy loop to 0x%x ...\n", (unsigned long)COPYBOOT_LOCATION);
745
      // Setup where we'll copy the relocation function to
746
      void (*relocated_function)(unsigned long, unsigned long, unsigned long, int)
747
        = (void*) COPYBOOT_LOCATION;
748
      // Now copy the function there, 32 words worth, increase this if needed...
749
      relocate_code((void*)COPYBOOT_LOCATION, copy_and_boot, 32);
750
      // Indicate we'll jump there...
751
      printf("tboot: Relocate (%d bytes from 0x%x to 0) and boot image, ...\n", i, (unsigned long) global.src_addr);
752
      // Now do the copy and boot
753
      (*relocated_function)(global.src_addr, 0x0, 0x0 + i, 0);
754
 
755
      return 0;
756
 
757
}
758
 
759 2 marcus.erl
void module_load_init (void)
760
{
761 140 julius
 
762
  register_command ("tftp_conf", "[ <file> [ <srv_ip> [ <src_addr>]]]", "TFTP configuration", tftp_conf_cmd);
763
  register_command ("tboot", "[<image number>]", "Bootstrap image downloaded via tftp", tboot_cmd);
764 389 tac2
  register_command ("sdboot", "[<image number>]", "Read image from SD-CARD", sdboot_cmd);
765 140 julius
#if 0
766 2 marcus.erl
  register_command ("tftp", "[<file> [<srv_ip> [<src_addr>]]]",  "TFTP download", tftp_cmd);
767
  register_command ("copy", "[<dst_addr> [<src_addr [<length>]]]", "Copy memory", copy_cmd);
768
  register_command ("sboot", "[<image number>]", "Bootstrap image downloaded via serial (Y/X modem)", sboot_cmd);
769
  register_command ("boot", "[<image number>]", "Bootstrap image copied from flash.", boot_cmd);
770
  register_command ("del_image", "[<image number>]", "Delete image", del_image_cmd);
771
  register_command ("save_conf", "", "Save current configuration into flash", save_conf_cmd);
772
  register_command ("boot_flash", "[<start_addr>]", "Boot image from <start_addr> (default from flash)", boot_flash_cmd);
773 140 julius
#endif
774 2 marcus.erl
  init_load();
775 389 tac2
 
776 2 marcus.erl
}

powered by: WebSVN 2.1.0

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