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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [orpmon/] [cmds/] [load.c] - Blame information for rev 1778

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

Line No. Rev Author Line
1 816 markom
#include "common.h"
2
#include "support.h"
3
#include "flash.h"
4
#include "net.h"
5 1312 jurem
#include "uart.h"
6
#include "spr_defs.h"
7 816 markom
 
8 1312 jurem
#ifndef MAX_IMAGES
9
#define MAX_IMAGES 20
10
#endif
11
 
12
extern unsigned long fprog_addr;
13 816 markom
extern char *tftp_filename;
14
 
15 1312 jurem
static flash_cfg_struct __attribute__ ((section(".config"))) gcfg = { 0, 0, 0, 0 };
16
 
17
#define FLASH_IMAGES_BASE 0xf0300000
18
 
19
/* Buffer Address == for example internal RAM */
20
#define BUF_BASE_ADDR   0x00400000 -  0x00042000
21
#define BUF_LENGTH      4096
22
 
23
 
24
#define ALIGN(addr,size) ((addr + (size-1))&(~(size-1)))
25
 
26
void copy_and_boot(unsigned long src,
27
                   unsigned long dst,
28
                   unsigned long len,
29
                   int tx_next)
30
{
31
  __asm__ __volatile__("   ;\
32
        l.addi  r8,r0,0x1  ;\
33
        l.mtspr r0,r8,0x11  ;\
34
        l.nop               ;\
35
        l.nop               ;\
36
        l.nop               ;\
37
        l.nop               ;\
38
        l.nop               ;\
39
2:                          ;\
40
        l.sfgeu r4,r5       ;\
41
        l.bf    1f          ;\
42
        l.nop               ;\
43
        l.lwz   r8,0(r3)    ;\
44
        l.sw    0(r4),r8    ;\
45
        l.addi  r3,r3,4     ;\
46
        l.j     2b          ;\
47
        l.addi  r4,r4,4     ;\
48
1:      l.sw    0x0(r0),r6  ;\
49
        l.ori   r8,r0,0x100 ;\
50
        l.jr    r8          ;\
51
        l.nop");
52
}
53
 
54
/* WARNING: stack and non-const globals should not be used in this function -- it may corrupt what have we loaded;
55
   start_addr should be 0xffffffff if only copying should be made
56
   no return, when start_addr != 0xffffffff, if successful */
57
int copy_memory_run (register unsigned long src_addr, register unsigned long dst_addr, register unsigned long length, register int erase, register unsigned long start_addr)
58
{
59
  unsigned long i, flags;
60
 
61
  register char *dst = (char *) dst_addr;
62
  register const char *src = (const char *) src_addr;
63
 
64
  if (dst_addr >= FLASH_BASE_ADDR) {
65
    if (dst_addr + length >= FLASH_BASE_ADDR + FLASH_SIZE) {
66
      printf ("error: region does not fit into flash.\n");
67
       return 1;
68
    }
69
#ifndef CFG_IN_FLASH
70
    fl_program (src_addr, dst_addr, length, erase, 1 /* do verify */);
71
#else
72
    /* we must disable interrupts! */
73
    flags=mfspr(SPR_SR);
74
    mtspr(SPR_SR,flags & ~(SPR_SR_TEE | SPR_SR_IEE));
75
 
76
    printf("Unlocking flash... ");
77
    for(i = 0; i < length; i += FLASH_BLOCK_SIZE)
78
      fl_ext_unlock(dst_addr + i);
79
    printf("done\n");
80
 
81
    printf("Erasing flash... ");
82
    for(i = 0; i < length; i += FLASH_BLOCK_SIZE)
83
      fl_ext_erase(dst_addr+i);
84
    printf("done\n");
85
 
86
    printf("Programing flash:\n\t");
87
    for (i = 0; i < length; i += INC_ADDR) {
88
      if(((i+INC_ADDR) % 1000) == 0)
89
        printf("#");
90
      if((i % (65*1000)) == 0)
91
        printf("\n\t");
92
      if (fl_ext_program (dst_addr + i, reg_read(src_addr + i))) {
93
        printf("error programing at 0x%08lx!\n", dst_addr+i);
94
        return 1;
95
      }
96
    }
97
    printf("Verifying flash... ");
98
    for(i = 0; i < length; i += INC_ADDR) {
99
      if( reg_read(dst_addr+i) != reg_read(src_addr + i)) {
100
        printf ("error at %08lx: %08lx != %08lx\n", src_addr + i, reg_read(src_addr + i), reg_read(dst_addr + i));
101
        return 1;
102
      }
103
    }
104
    printf("OK!\n");
105
    mtspr(SPR_SR, flags);
106
#endif
107
    if(start_addr == 0xffffffff)
108
      return 0;
109
  }
110
  else {
111
    while (length--) *dst++ = *src++;
112
    if (start_addr == 0xffffffff)
113
      return 0;
114
  }
115
  /* Run the program */
116
  ((void (*)(void)) start_addr)();
117
  return 0; /* just to satisfy the cc */
118
}
119
 
120
void bf_jump(unsigned long addr)
121
{
122
  asm("l.jr   r3");
123
  asm("l.nop  0x0");
124
}
125
 
126
int boot_flash_cmd(int argc, char *argv[])
127
{
128
  unsigned long addr,val,jaddr;
129
  addr = 17;
130
  val = 0;
131
  /* clear SR */
132
 
133
  asm("l.mtspr %0,%1,0": : "r" (addr), "r" (val));
134
  /* jump */
135
  if(argc == 0)
136
    bf_jump(FLASH_BASE_ADDR+0x100);
137
  else {
138
    jaddr = strtoul(argv[0], 0, 0);
139
    bf_jump(jaddr);
140
  }
141
  return 0;
142
}
143
 
144
void
145
init_load (void)
146
{
147
#ifdef CFG_IN_FLASH
148
  copy_memory_run((unsigned long)&fl_word_program, (unsigned long)&fprog_addr,
149
                  95, 0, 0xffffffff);
150
  copy_memory_run((unsigned long)&fl_block_erase, (unsigned long)&fprog_addr+96,
151
                  119, 0, 0xffffffff);
152
  copy_memory_run((unsigned long)&fl_unlock_one_block,
153
                  (unsigned long)&fprog_addr+96+120,
154
                  115, 0, 0xffffffff);
155
 
156
  fl_ext_program = (t_fl_ext_program)&fprog_addr;
157
  fl_ext_erase = (t_fl_erase)&fprog_addr+96;
158
  fl_ext_unlock = (t_fl_erase)&fprog_addr+96+120;
159
 
160
#if 0
161
  printf("fl_word_program(): 0x%x\tfl_ext_program(): 0x%x\n",
162
         &fl_word_program, fl_ext_program);
163
  printf("fl_block_erase: 0x%x\tfl_ext_erase(): 0x%x\n",
164
         &fl_block_erase, fl_ext_erase);
165
  printf("fl_unlock_one_block(): 0x%x\tfl_ext_unlock(): 0x%x\n",
166
         &fl_unlock_one_block, fl_ext_unlock);
167
#endif
168
 
169
#else /* not CFG_IN_FLASH */
170
  fl_ext_program = (t_fl_ext_program)&fl_word_program;
171
  fl_ext_erase = (t_fl_erase)&fl_block_erase;
172
  fl_ext_unlock = (t_fl_erase)&fl_unlock_one_block;
173
#endif /* CFG_IN_FLASH */
174
 
175
  global.ip = gcfg.eth_ip;
176
  global.gw_ip = gcfg.eth_gw;
177
  global.mask = gcfg.eth_mask;
178
  global.srv_ip = gcfg.tftp_srv_ip;
179
  tftp_filename = "boot.img";
180
  /*memcpy(tftp_filename, gcfg.tftp_filename, strlen(gcfg.tftp_filename));
181
    tftp_filename[strlen(gcfg.tftp_filename)] = '\0';*/
182
}
183
 
184 816 markom
int tftp_cmd (int argc, char *argv[])
185
{
186
  switch (argc) {
187
    case 0: tftp_filename = "boot.img";
188
                  break;
189
    case 3: global.src_addr = strtoul (argv[2], 0, 0);
190
    case 2: global.srv_ip = parse_ip (argv[1]);
191
    case 1: tftp_filename = &argv[0][0];
192
            break;
193
  }
194
  NetLoop(TFTP);
195
  return 0;
196
}
197
 
198 1312 jurem
int tftp_conf_cmd(int argc, char *argv[])
199 816 markom
{
200 1312 jurem
  switch(argc) {
201
  case 0:
202
    printf("Image filename: %s", tftp_filename);
203
    printf("\nSrc addr: 0x%lx", global.src_addr);
204
    printf("\nServer IP: %s", inet_ntoa(global.srv_ip));
205
    return 0;
206
  case 3:
207
    global.src_addr = strtoul(argv[2], 0, 0);
208
    global.srv_ip = inet_aton(argv[1]);
209
    tftp_filename = argv[0];
210
    tftp_filename[strlen(argv[0])] = '\0';
211
    break;
212
  case 2:
213
    global.srv_ip = inet_aton(argv[1]);
214
    tftp_filename = argv[0];
215
    break;
216
  case 1:
217
    tftp_filename = argv[0];
218
    break;
219
  }
220
  return 0;
221
}
222
 
223
void save_global_cfg(flash_cfg_struct cfg)
224
{
225
  unsigned long dst = (unsigned long)&gcfg, src = (unsigned long)&cfg;
226
  unsigned long i, end, flags;
227
 
228
  end = (unsigned long)&cfg + sizeof(flash_cfg_struct);
229
 
230
  printf("Saving global cfg from 0x%lx (end: 0x%lx) to 0x%lx...", src, end, dst);
231
 
232
  /* we must disable interrupts! */
233
  flags=mfspr(SPR_SR);
234
  mtspr(SPR_SR,flags & ~(SPR_SR_TEE | SPR_SR_IEE));
235
  /*  printf("Unlocking... ");*/
236
  for(i = 0; (src+i <= end); i += FLASH_BLOCK_SIZE) {
237
    fl_ext_unlock(dst+i);
238
  }
239
  /*  printf("done\n");*/
240
  /*  printf("Erasing... ");*/
241
  for(i = 0; (src+i <= end); i += FLASH_BLOCK_SIZE)
242
    fl_ext_erase(dst);
243
  /*  printf("done\n");*/
244
  /*  printf("Programing... ");*/
245
  for(i = 0; (src+i <= end); i +=INC_ADDR) {
246
    if(fl_ext_program(dst+i, reg_read(src+i))) {
247
      printf("Error ocurred while saving.\n");
248
      return;
249 816 markom
    }
250
  }
251 1312 jurem
  printf("done\n");
252
 
253
  /* and than enable it back */
254
  mtspr(SPR_SR, flags);
255
  return;
256 816 markom
}
257
 
258 1312 jurem
int save_conf_cmd(int argc, char *argv[])
259
{
260
  flash_cfg_struct newCfg;
261
 
262
  newCfg = gcfg;
263
 
264
  newCfg.eth_ip = global.ip;
265
  newCfg.eth_mask = global.mask;
266
  newCfg.eth_gw = global.gw_ip;
267
  newCfg.tftp_srv_ip = global.srv_ip;
268
  /*  memcpy(newCfg.tftp_filename, tftp_filename, strlen(tftp_filename));*/
269
 
270
  save_global_cfg(newCfg);
271
  return 0;
272
}
273 816 markom
int copy_cmd (int argc, char *argv[])
274
{
275
  switch (argc) {
276
    case 3: global.src_addr = strtoul (argv[2], 0, 0);
277
    case 2: global.length = strtoul (argv[2], 0, 0);
278
    case 1: global.src_addr = strtoul (argv[2], 0, 0);
279 1312 jurem
    case 0: return copy_memory_run (global.src_addr, global.dst_addr, global.length,
280
                                    global.erase_method, 0xffffffff);
281 816 markom
  }
282
  return -1;
283
}
284
 
285 1312 jurem
void
286
images_info(void)
287 816 markom
{
288 1312 jurem
  int i;
289
  printf("Number of images: 0x%lx\n", gcfg.img_number);
290
  for(i = 0; i < gcfg.img_number; i++)
291
    printf("%d. image size: 0x%lx (at 0x%08lx)\n", i+1,
292
           gcfg.img_length[i], gcfg.img_start_addr[i]);
293
}
294
 
295
/*
296
 * get_good_addr()
297
 *
298
 * Here we try to find the most suitable place for our image. We search for
299
 * a hole between images, that is big enough (but as small as possible).
300
 *
301
 */
302
unsigned long
303
get_good_addr(unsigned int size)
304
{
305
  unsigned long start_addr[MAX_IMAGES], end_addr[MAX_IMAGES];
306
  unsigned long free[MAX_IMAGES], st_addr[MAX_IMAGES];
307
  unsigned long tmpval;
308
  unsigned int i = 0, j;
309
 
310
  flash_cfg_struct myCfg;
311
  myCfg = gcfg;
312
 
313
  /* we are full */
314
  if(gcfg.img_number == MAX_IMAGES)
315
    return 0xffffffff;
316
 
317
  if(gcfg.img_number == 0)
318
    return FLASH_IMAGES_BASE;
319
 
320
  for(i = 0; i < MAX_IMAGES; i++) {
321
    start_addr[i] = 0;
322
    end_addr[i] = 0;
323
    free[i] = 0;
324
    st_addr[i] = 0;
325
  }
326
 
327
  for(i = 0; i < myCfg.img_number; i++) {
328
    start_addr[i] = myCfg.img_start_addr[i];
329
    end_addr[i] = ALIGN((myCfg.img_start_addr[i] + myCfg.img_length[i]),
330
                        FLASH_BLOCK_SIZE);
331
  }
332
  /*  printf("\n");
333
  for(i = 0; i < myCfg.img_number; i++)
334
    printf("start: 0x%08x, end: 0x%08x\n", start_addr[i], end_addr[i]);
335
    printf("\n");*/
336
  /* bubble sorting by start_addr */
337
 
338
  for(j = myCfg.img_number - 1; j > 0; j--)
339
    for(i = 0; i < j; i++)
340
      if(start_addr[i] > start_addr[i+1]) {
341
        tmpval = start_addr[i];
342
        start_addr[i] = start_addr[i+1];
343
        start_addr[i+1] = tmpval;
344
        tmpval = end_addr[i];
345
        end_addr[i] = end_addr[i+1];
346
        end_addr[i+1] = tmpval;
347
      }
348
 
349
  /*  for(i = 0; i < myCfg.img_number; i++)
350
    printf("start: 0x%08x, end: 0x%08x\n", start_addr[i], end_addr[i]);
351
    printf("\n");*/
352
 
353
  /* now we calculate free space betwens segments */
354
  for(i = 1; i < myCfg.img_number; i++) {
355
    st_addr[i] = end_addr[i - 1];
356
    free[i] = start_addr[i] - end_addr[i - 1];
357
  }
358
 
359
  /* here we calcuta first position (starting with FLASH_IMAGES_BASE)... */
360
  st_addr[0] = FLASH_IMAGES_BASE + 0;
361
  free[0] = start_addr[0] - FLASH_IMAGES_BASE;
362
  /* ... and last one (ending with FLASH_IMAGES_BASE + FLASH_SIZE). */
363
  st_addr[myCfg.img_number] = end_addr[myCfg.img_number-1];
364
  free[myCfg.img_number] = (FLASH_IMAGES_BASE + FLASH_SIZE) - end_addr[myCfg.img_number-1];
365
 
366
  /*  for(i = 0; i < myCfg.img_number+1; i++)
367
    printf("start: 0x%08x, free: %x\n", st_addr[i], free[i]);
368
    printf("\n");*/
369
 
370
  /* yet another bubble sort by free (space) */
371
  for(j = myCfg.img_number; j > 0; j--)
372
    for(i = 0; i < j; i++)
373
      if(free[i] > free[i+1]) {
374
        tmpval = free[i];
375
        free[i] = free[i+1];
376
        free[i+1] = tmpval;
377
        tmpval = st_addr[i];
378
        st_addr[i] = st_addr[i+1];
379
        st_addr[i+1] = tmpval;
380
      }
381
 
382
  /*  for(i = 0; i < myCfg.img_number+1; i++)
383
    printf("start: 0x%08x, free: %x\n", st_addr[i], free[i]);
384
    printf("\n");*/
385
 
386
  /* now we pick the smallest but just big enough for our size */
387
  for(i = 0; i <= myCfg.img_number; i++)
388
    if(free[i] >= size)
389
      return st_addr[i];
390
 
391
  /* there is not enough space (in one segment) left */
392
  return 0;
393
}
394
 
395
unsigned long
396
prepare_img_data(unsigned int num, unsigned int size)
397
{
398
  int i;
399
  unsigned long addr=0;
400
  flash_cfg_struct newCfg;
401
 
402
  newCfg = gcfg;
403
 
404
  if(newCfg.img_number >= MAX_IMAGES) {
405
    printf("Maximum images exceeded: %d\n", MAX_IMAGES);
406
    return 0xffffffff;
407
  }
408
 
409
  newCfg.img_number++;
410
 
411
  if((num > newCfg.img_number) || (num == 0))
412
    num = newCfg.img_number;
413
 
414
  addr = get_good_addr(size);
415
  if(addr == 0x00) {
416
    printf("Can not find suitable place in flash. (None of free segments are big enough)\n");
417
    return 0xffffffff;
418
  }
419
 
420
  if(num < newCfg.img_number)
421
    for(i=newCfg.img_number-1; i >= num; i--) {
422
      newCfg.img_length[i] = newCfg.img_length[i-1];
423
      newCfg.img_start_addr[i] = newCfg.img_start_addr[i-1];
424
    }
425
 
426
  newCfg.img_length[num-1] = size;
427
  newCfg.img_start_addr[num-1] = addr;
428
 
429
  save_global_cfg(newCfg);
430
  return addr;
431
}
432
 
433
int
434
del_image_cmd(int argc, char *argv[])
435
{
436
  unsigned num, i;
437
  flash_cfg_struct newCfg = gcfg;
438
 
439
  newCfg.img_number = gcfg.img_number;
440
  for(i = 0; i < MAX_IMAGES; i++)
441
    newCfg.img_length[i] = gcfg.img_length[i];
442
 
443
  printf("Number of images available: 0x%lx\n", newCfg.img_number);
444
 
445
  if(argc == 0) {
446
    newCfg.img_number = 0;
447
    for(i = 0; i < MAX_IMAGES; i++) {
448
      newCfg.img_length[i] = 0;
449
      newCfg.img_start_addr[i] = 0;
450
    }
451
    save_global_cfg(newCfg);
452
    return 0;
453
  }
454
  else {
455
    num = strtoul(argv[0], 0, 0);
456
  }
457
 
458
  if(newCfg.img_number == 0) {
459
    printf("Nothing to delete!\n");
460
    return 0;
461
  }
462
  if((num == 0) || (num > newCfg.img_number))
463
    num = newCfg.img_number;
464
 
465
  for(i=num-1; i < newCfg.img_number; i++) {
466
    newCfg.img_length[i] = newCfg.img_length[i+1];
467
    newCfg.img_start_addr[i] = newCfg.img_start_addr[i+1];
468
  }
469
 
470
  newCfg.img_number--;
471
  save_global_cfg(newCfg);
472
  return 0;
473
}
474
 
475
int
476
boot_cmd(int argc, char *argv[])
477
{
478
  int num;
479
  extern int tx_next;
480
 
481
  if(argc == 0) {
482
    images_info();
483
    return 0;
484
  }
485
 
486
  num = strtoul(argv[0],0,0);
487
  if(gcfg.img_number < num) {
488
    printf("There are only %lu images, you requested %d!\n", gcfg.img_number, num);
489
    return -1;
490
  }
491
 
492
  printf("Copying image number %d from 0x%lx, size: 0x%lx...",
493
         num, gcfg.img_start_addr[num-1], gcfg.img_length[num-1]);
494
 
495
  printf("booting...\n");
496
  copy_and_boot(gcfg.img_start_addr[num-1], 0x0, gcfg.img_length[num-1], tx_next);
497
  return 0;
498
}
499
 
500
int mGetData(unsigned long);
501
 
502
int sboot_cmd (int argc, char *argv[])
503
{
504
  int copied;
505
  unsigned int num = 0xffffffff, addr = 0x0;
506
 
507
  switch(argc) {
508
  case 0:
509
    num = 0xffffffff;
510
    break;
511
  case 1:
512
    num = strtoul(argv[0], 0, 0);
513
    break;
514
  }
515
 
516
  copied = mGetData(global.src_addr);
517
  if(copied <= 0) {
518
    printf("sboot: error while getting the image!");
519
    return -1;
520
  }
521
  printf("image size: 0x%x\n", copied);
522
 
523
  if(num != 0xffffffff) {
524
    addr = prepare_img_data(num, copied);
525
    if(addr == 0xffffffff)
526
      printf("Image not written to flash!\n");
527
    else {
528
      printf("Copying image to flash, image number: %d, dst_addr: 0x%x\n",
529
             num, addr);
530
      copy_memory_run(global.src_addr, gcfg.img_start_addr[num-1], copied, 2, 0xffffffff);
531
    }
532
  }
533
 
534
  return 0;
535
}
536
 
537
int tboot_cmd (int argc, char *argv[])
538
{
539
  int copied;
540
  unsigned int num = 0xffffffff, addr = 0x0;
541
  extern int tx_next;
542
 
543 816 markom
  switch (argc) {
544 1312 jurem
  case 0:
545
    num = 0xffffffff;
546
    break;
547
  case 1:
548
    printf("argv[0] %p\n", argv[0]);
549
    num = strtoul(argv[0], 0, 0);
550
    printf("num %d\n", num);
551
    break;
552 816 markom
  }
553 1312 jurem
 
554
  //  global.src_addr = (unsigned long)0x0;
555
 
556
  copied =NetLoop(TFTP);
557
  if (copied <= 0) {
558
    printf("tboot: error while getting the image '%s'",
559
           tftp_filename);
560
    return -1;
561
  }
562
 
563
  if(num != 0xffffffff) {
564
    addr = prepare_img_data(num, copied);
565
    if(addr == 0xffffffff)
566
      printf("Image not written to flash!\n");
567
    else {
568
      printf("Copying image to flash, image number: %d, dst_addr: 0x%x\n",
569
             num, addr);
570
      copy_memory_run(global.src_addr, gcfg.img_start_addr[num-1], copied, 2, 0xffffffff);
571
    }
572
  }
573
  /* the point of no return */
574
  printf("tboot: copying 0x%lx -> 0x0, image size 0x%x...\n",
575
         global.src_addr, copied);
576
 
577
  printf("tboot: jumping to 0x100, booting image ...\n");
578
  copy_and_boot(global.src_addr, 0x0, 0x0 + copied, tx_next);
579
  return 0;
580 816 markom
}
581
 
582
void module_load_init (void)
583
{
584
  register_command ("tftp", "[<file> [<srv_ip> [<src_addr>]]]",  "TFTP download", tftp_cmd);
585 1312 jurem
  register_command ("tftp_conf", "[ <file> [ <srv_ip> [ <src_addr>]]]", "TFTP configuration", tftp_conf_cmd);
586 816 markom
  register_command ("copy", "[<dst_addr> [<src_addr [<length>]]]", "Copy memory", copy_cmd);
587 1312 jurem
  register_command ("tboot", "[<image number>]", "Bootstrap image downloaded via tftp", tboot_cmd);
588
  register_command ("sboot", "[<image number>]", "Bootstrap image downloaded via serial (Y/X modem)", sboot_cmd);
589
  register_command ("boot", "[<image number>]", "Bootstrap image copied from flash.", boot_cmd);
590
  register_command ("del_image", "[<image number>]", "Delete image", del_image_cmd);
591
  register_command ("save_conf", "", "Save current configuration into flash", save_conf_cmd);
592
  register_command ("boot_flash", "[<start_addr>]", "Boot image from <start_addr> (default from flash)", boot_flash_cmd);
593
  init_load();
594 816 markom
}

powered by: WebSVN 2.1.0

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