common_util.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. #include <common.h>
  25. #include <command.h>
  26. #include <video_fb.h>
  27. #include "common_util.h"
  28. #include <asm/processor.h>
  29. #include <asm/byteorder.h>
  30. #include <i2c.h>
  31. #include <devices.h>
  32. #include <pci.h>
  33. #include <malloc.h>
  34. #include <bzlib.h>
  35. #ifdef CONFIG_PIP405
  36. #include "../pip405/pip405.h"
  37. #include <405gp_pci.h>
  38. #endif
  39. #ifdef CONFIG_MIP405
  40. #include "../mip405/mip405.h"
  41. #include <405gp_pci.h>
  42. #endif
  43. extern int gunzip(void *, int, uchar *, int *);
  44. extern int mem_test(ulong start, ulong ramsize, int quiet);
  45. #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
  46. #define IMAGE_SIZE CFG_MONITOR_LEN /* ugly, but it works for now */
  47. extern flash_info_t flash_info[]; /* info for FLASH chips */
  48. static image_header_t header;
  49. static int
  50. mpl_prg(uchar *src, ulong size)
  51. {
  52. ulong start;
  53. flash_info_t *info;
  54. int i, rc;
  55. #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
  56. char *copystr = (char *)src;
  57. ulong *magic = (ulong *)src;
  58. #endif
  59. info = &flash_info[0];
  60. #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
  61. if (ntohl(magic[0]) != IH_MAGIC) {
  62. puts("Bad Magic number\n");
  63. return -1;
  64. }
  65. /* some more checks before we delete the Flash... */
  66. /* Checking the ISO_STRING prevents to program a
  67. * wrong Firmware Image into the flash.
  68. */
  69. i = 4; /* skip Magic number */
  70. while (1) {
  71. if (strncmp(&copystr[i], "MEV-", 4) == 0)
  72. break;
  73. if (i++ >= 0x100) {
  74. puts("Firmware Image for unknown Target\n");
  75. return -1;
  76. }
  77. }
  78. /* we have the ISO STRING, check */
  79. if (strncmp(&copystr[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
  80. printf("Wrong Firmware Image: %s\n", &copystr[i]);
  81. return -1;
  82. }
  83. start = 0 - size;
  84. for (i = info->sector_count-1; i > 0; i--) {
  85. info->protect[i] = 0; /* unprotect this sector */
  86. if (start >= info->start[i])
  87. break;
  88. }
  89. /* set-up flash location */
  90. /* now erase flash */
  91. printf("Erasing at %lx (sector %d) (start %lx)\n",
  92. start,i,info->start[i]);
  93. if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
  94. puts("ERROR ");
  95. flash_perror(rc);
  96. return (1);
  97. }
  98. #elif defined(CONFIG_VCMA9)
  99. start = 0;
  100. for (i = 0; i <info->sector_count; i++) {
  101. info->protect[i] = 0; /* unprotect this sector */
  102. if (size < info->start[i])
  103. break;
  104. }
  105. /* set-up flash location */
  106. /* now erase flash */
  107. printf("Erasing at %lx (sector %d) (start %lx)\n",
  108. start,0,info->start[0]);
  109. if ((rc = flash_erase (info, 0, i)) != 0) {
  110. puts("ERROR ");
  111. flash_perror(rc);
  112. return (1);
  113. }
  114. #endif
  115. printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
  116. (ulong)src, size);
  117. if ((rc = flash_write (src, start, size)) != 0) {
  118. puts("ERROR ");
  119. flash_perror(rc);
  120. return (1);
  121. }
  122. puts("OK programming done\n");
  123. return 0;
  124. }
  125. static int
  126. mpl_prg_image(uchar *ld_addr)
  127. {
  128. unsigned long len, checksum;
  129. uchar *data;
  130. image_header_t *hdr = &header;
  131. int rc;
  132. /* Copy header so we can blank CRC field for re-calculation */
  133. memcpy (&header, (char *)ld_addr, sizeof(image_header_t));
  134. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  135. puts("Bad Magic Number\n");
  136. return 1;
  137. }
  138. print_image_hdr(hdr);
  139. if (hdr->ih_os != IH_OS_U_BOOT) {
  140. puts("No U-Boot Image\n");
  141. return 1;
  142. }
  143. if (hdr->ih_type != IH_TYPE_FIRMWARE) {
  144. puts("No Firmware Image\n");
  145. return 1;
  146. }
  147. data = (uchar *)&header;
  148. len = sizeof(image_header_t);
  149. checksum = ntohl(hdr->ih_hcrc);
  150. hdr->ih_hcrc = 0;
  151. if (crc32 (0, (char *)data, len) != checksum) {
  152. puts("Bad Header Checksum\n");
  153. return 1;
  154. }
  155. data = ld_addr + sizeof(image_header_t);
  156. len = ntohl(hdr->ih_size);
  157. puts("Verifying Checksum ... ");
  158. if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
  159. puts("Bad Data CRC\n");
  160. return 1;
  161. }
  162. puts("OK\n");
  163. if (hdr->ih_comp != IH_COMP_NONE) {
  164. uchar *buf;
  165. /* reserve space for uncompressed image */
  166. if ((buf = malloc(IMAGE_SIZE)) == NULL) {
  167. puts("Insufficient space for decompression\n");
  168. return 1;
  169. }
  170. switch (hdr->ih_comp) {
  171. case IH_COMP_GZIP:
  172. puts("Uncompressing (GZIP) ... ");
  173. rc = gunzip ((void *)(buf), IMAGE_SIZE, data, (int *)&len);
  174. if (rc != 0) {
  175. puts("GUNZIP ERROR\n");
  176. free(buf);
  177. return 1;
  178. }
  179. puts("OK\n");
  180. break;
  181. #if CONFIG_BZIP2
  182. case IH_COMP_BZIP2:
  183. puts("Uncompressing (BZIP2) ... ");
  184. {
  185. uint retlen = IMAGE_SIZE;
  186. rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
  187. (char *)data, len, 0, 0);
  188. len = retlen;
  189. }
  190. if (rc != BZ_OK) {
  191. printf ("BUNZIP2 ERROR: %d\n", rc);
  192. free(buf);
  193. return 1;
  194. }
  195. puts("OK\n");
  196. break;
  197. #endif
  198. default:
  199. printf ("Unimplemented compression type %d\n", hdr->ih_comp);
  200. free(buf);
  201. return 1;
  202. }
  203. rc = mpl_prg(buf, len);
  204. free(buf);
  205. } else {
  206. rc = mpl_prg(data, len);
  207. }
  208. return(rc);
  209. }
  210. void get_backup_values(backup_t *buf)
  211. {
  212. i2c_read(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
  213. }
  214. void set_backup_values(int overwrite)
  215. {
  216. backup_t back;
  217. int i;
  218. get_backup_values(&back);
  219. if(!overwrite) {
  220. if(strncmp(back.signature,"MPL\0",4)==0) {
  221. puts("Not possible to write Backup\n");
  222. return;
  223. }
  224. }
  225. memcpy(back.signature,"MPL\0",4);
  226. i = getenv_r("serial#",back.serial_name,16);
  227. if(i < 0) {
  228. puts("Not possible to write Backup\n");
  229. return;
  230. }
  231. back.serial_name[16]=0;
  232. i = getenv_r("ethaddr",back.eth_addr,20);
  233. if(i < 0) {
  234. puts("Not possible to write Backup\n");
  235. return;
  236. }
  237. back.eth_addr[20]=0;
  238. i2c_write(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
  239. }
  240. void clear_env_values(void)
  241. {
  242. backup_t back;
  243. unsigned char env_crc[4];
  244. memset(&back,0xff,sizeof(backup_t));
  245. memset(env_crc,0x00,4);
  246. i2c_write(CFG_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
  247. i2c_write(CFG_DEF_EEPROM_ADDR,CFG_ENV_OFFSET,2,(void *)env_crc,4);
  248. }
  249. /*
  250. * check crc of "older" environment
  251. */
  252. int check_env_old_size(ulong oldsize)
  253. {
  254. ulong crc, len, new;
  255. unsigned off;
  256. uchar buf[64];
  257. /* read old CRC */
  258. eeprom_read (CFG_DEF_EEPROM_ADDR,
  259. CFG_ENV_OFFSET,
  260. (uchar *)&crc, sizeof(ulong));
  261. new = 0;
  262. len = oldsize;
  263. off = sizeof(long);
  264. len = oldsize-off;
  265. while (len > 0) {
  266. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  267. eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, buf, n);
  268. new = crc32 (new, buf, n);
  269. len -= n;
  270. off += n;
  271. }
  272. return (crc == new);
  273. }
  274. static ulong oldsizes[] = {
  275. 0x200,
  276. 0x800,
  277. 0
  278. };
  279. void copy_old_env(ulong size)
  280. {
  281. uchar name_buf[64];
  282. uchar value_buf[0x800];
  283. uchar c;
  284. ulong len;
  285. unsigned off;
  286. uchar *name, *value;
  287. name=&name_buf[0];
  288. value=&value_buf[0];
  289. len=size;
  290. off = sizeof(long);
  291. while (len > off) {
  292. eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
  293. if(c != '=') {
  294. *name++=c;
  295. off++;
  296. }
  297. else {
  298. *name++='\0';
  299. off++;
  300. do {
  301. eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
  302. *value++=c;
  303. off++;
  304. if(c == '\0')
  305. break;
  306. } while(len > off);
  307. name=&name_buf[0];
  308. value=&value_buf[0];
  309. if(strncmp(name,"baudrate",8)!=0) {
  310. setenv(name,value);
  311. }
  312. }
  313. }
  314. }
  315. void check_env(void)
  316. {
  317. unsigned char *s;
  318. int i=0;
  319. char buf[32];
  320. backup_t back;
  321. s=getenv("serial#");
  322. if(!s) {
  323. while(oldsizes[i]) {
  324. if(check_env_old_size(oldsizes[i]))
  325. break;
  326. i++;
  327. }
  328. if(!oldsizes[i]) {
  329. /* no old environment has been found */
  330. get_backup_values (&back);
  331. if (strncmp (back.signature, "MPL\0", 4) == 0) {
  332. sprintf (buf, "%s", back.serial_name);
  333. setenv ("serial#", buf);
  334. sprintf (buf, "%s", back.eth_addr);
  335. setenv ("ethaddr", buf);
  336. printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
  337. return;
  338. }
  339. }
  340. else {
  341. copy_old_env(oldsizes[i]);
  342. puts("INFO: old environment ajusted, use saveenv\n");
  343. }
  344. }
  345. else {
  346. /* check if back up is set */
  347. get_backup_values(&back);
  348. if(strncmp(back.signature,"MPL\0",4)!=0) {
  349. set_backup_values(0);
  350. }
  351. }
  352. }
  353. extern device_t *stdio_devices[];
  354. extern char *stdio_names[];
  355. void show_stdio_dev(void)
  356. {
  357. /* Print information */
  358. puts("In: ");
  359. if (stdio_devices[stdin] == NULL) {
  360. puts("No input devices available!\n");
  361. } else {
  362. printf ("%s\n", stdio_devices[stdin]->name);
  363. }
  364. puts("Out: ");
  365. if (stdio_devices[stdout] == NULL) {
  366. puts("No output devices available!\n");
  367. } else {
  368. printf ("%s\n", stdio_devices[stdout]->name);
  369. }
  370. puts("Err: ");
  371. if (stdio_devices[stderr] == NULL) {
  372. puts("No error devices available!\n");
  373. } else {
  374. printf ("%s\n", stdio_devices[stderr]->name);
  375. }
  376. }
  377. int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  378. {
  379. ulong size,src,ld_addr;
  380. int result;
  381. backup_t back;
  382. src = MULTI_PURPOSE_SOCKET_ADDR;
  383. size = IMAGE_SIZE;
  384. if (strcmp(argv[1], "flash") == 0)
  385. {
  386. #if (CONFIG_COMMANDS & CFG_CMD_FDC)
  387. if (strcmp(argv[2], "floppy") == 0) {
  388. char *local_args[3];
  389. extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
  390. puts("\nupdating bootloader image from floppy\n");
  391. local_args[0] = argv[0];
  392. if(argc==4) {
  393. local_args[1] = argv[3];
  394. local_args[2] = NULL;
  395. ld_addr=simple_strtoul(argv[3], NULL, 16);
  396. result=do_fdcboot(cmdtp, 0, 2, local_args);
  397. }
  398. else {
  399. local_args[1] = NULL;
  400. ld_addr=CFG_LOAD_ADDR;
  401. result=do_fdcboot(cmdtp, 0, 1, local_args);
  402. }
  403. result=mpl_prg_image((uchar *)ld_addr);
  404. return result;
  405. }
  406. #endif /* (CONFIG_COMMANDS & CFG_CMD_FDC) */
  407. if (strcmp(argv[2], "mem") == 0) {
  408. if(argc==4) {
  409. ld_addr=simple_strtoul(argv[3], NULL, 16);
  410. }
  411. else {
  412. ld_addr=load_addr;
  413. }
  414. printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
  415. result=mpl_prg_image((uchar *)ld_addr);
  416. return result;
  417. }
  418. if (strcmp(argv[2], "mps") == 0) {
  419. puts("\nupdating bootloader image from MPS\n");
  420. result=mpl_prg((uchar *)src,size);
  421. return result;
  422. }
  423. }
  424. if (strcmp(argv[1], "mem") == 0)
  425. {
  426. result=0;
  427. if(argc==3)
  428. {
  429. result = (int)simple_strtol(argv[2], NULL, 16);
  430. }
  431. src=(unsigned long)&result;
  432. src-=CFG_MEMTEST_START;
  433. src-=(100*1024); /* - 100k */
  434. src&=0xfff00000;
  435. size=0;
  436. do {
  437. size++;
  438. printf("\n\nPass %ld\n",size);
  439. mem_test(CFG_MEMTEST_START,src,1);
  440. if(ctrlc())
  441. break;
  442. if(result>0)
  443. result--;
  444. }while(result);
  445. return 0;
  446. }
  447. if (strcmp(argv[1], "clearenvvalues") == 0)
  448. {
  449. if (strcmp(argv[2], "yes") == 0)
  450. {
  451. clear_env_values();
  452. return 0;
  453. }
  454. }
  455. if (strcmp(argv[1], "getback") == 0) {
  456. get_backup_values(&back);
  457. back.signature[3]=0;
  458. back.serial_name[16]=0;
  459. back.eth_addr[20]=0;
  460. printf("GetBackUp: signature: %s\n",back.signature);
  461. printf(" serial#: %s\n",back.serial_name);
  462. printf(" ethaddr: %s\n",back.eth_addr);
  463. return 0;
  464. }
  465. if (strcmp(argv[1], "setback") == 0) {
  466. set_backup_values(1);
  467. return 0;
  468. }
  469. printf("Usage:\n%s\n", cmdtp->usage);
  470. return 1;
  471. }
  472. #if (CONFIG_COMMANDS & CFG_CMD_DOC)
  473. extern void doc_probe(ulong physadr);
  474. void doc_init (void)
  475. {
  476. doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
  477. }
  478. #endif
  479. #ifdef CONFIG_VIDEO
  480. /******************************************************
  481. * Routines to display the Board information
  482. * to the screen (since the VGA will be initialized as last,
  483. * we must resend the infos)
  484. */
  485. #ifdef CONFIG_CONSOLE_EXTRA_INFO
  486. extern GraphicDevice ctfb;
  487. extern int get_boot_mode(void);
  488. void video_get_info_str (int line_number, char *info)
  489. {
  490. /* init video info strings for graphic console */
  491. DECLARE_GLOBAL_DATA_PTR;
  492. PPC405_SYS_INFO sys_info;
  493. char rev;
  494. int i,boot;
  495. unsigned long pvr;
  496. char buf[64];
  497. char tmp[16];
  498. char cpustr[16];
  499. unsigned char *s, *e, bc;
  500. switch (line_number)
  501. {
  502. case 2:
  503. /* CPU and board infos */
  504. pvr=get_pvr();
  505. get_sys_info (&sys_info);
  506. switch (pvr) {
  507. case PVR_405GP_RB: rev='B'; break;
  508. case PVR_405GP_RC: rev='C'; break;
  509. case PVR_405GP_RD: rev='D'; break;
  510. case PVR_405GP_RE: rev='E'; break;
  511. case PVR_405GPR_RB: rev='B'; break;
  512. default: rev='?'; break;
  513. }
  514. if(pvr==PVR_405GPR_RB)
  515. sprintf(cpustr,"PPC405GPr %c",rev);
  516. else
  517. sprintf(cpustr,"PPC405GP %c",rev);
  518. /* Board info */
  519. i=0;
  520. s=getenv ("serial#");
  521. #ifdef CONFIG_PIP405
  522. if (!s || strncmp (s, "PIP405", 6)) {
  523. sprintf(buf,"### No HW ID - assuming PIP405");
  524. }
  525. #endif
  526. #ifdef CONFIG_MIP405
  527. if (!s || strncmp (s, "MIP405", 6)) {
  528. sprintf(buf,"### No HW ID - assuming MIP405");
  529. }
  530. #endif
  531. else {
  532. for (e = s; *e; ++e) {
  533. if (*e == ' ')
  534. break;
  535. }
  536. for (; s < e; ++s) {
  537. if (*s == '_') {
  538. ++s;
  539. break;
  540. }
  541. buf[i++]=*s;
  542. }
  543. sprintf(&buf[i]," SN ");
  544. i+=4;
  545. for (; s < e; ++s) {
  546. buf[i++]=*s;
  547. }
  548. buf[i++]=0;
  549. }
  550. sprintf (info," %s %s %s MHz (%lu/%lu/%lu MHz)",
  551. buf, cpustr,
  552. strmhz (tmp, gd->cpu_clk), sys_info.freqPLB / 1000000,
  553. sys_info.freqPLB / sys_info.pllOpbDiv / 1000000,
  554. sys_info.freqPLB / sys_info.pllExtBusDiv / 1000000);
  555. return;
  556. case 3:
  557. /* Memory Info */
  558. boot = get_boot_mode();
  559. bc = in8 (CONFIG_PORT_ADDR);
  560. sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
  561. gd->bd->bi_memsize / 0x100000,
  562. gd->bd->bi_flashsize / 0x100000,
  563. bc,
  564. (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
  565. ctfb.modeIdent);
  566. return;
  567. case 1:
  568. sprintf (buf, "%s",CONFIG_IDENT_STRING);
  569. sprintf (info, " %s", &buf[1]);
  570. return;
  571. }
  572. /* no more info lines */
  573. *info = 0;
  574. return;
  575. }
  576. #endif /* CONFIG_CONSOLE_EXTRA_INFO */
  577. #endif /* CONFIG_VIDEO */