common_util.c 15 KB

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