common_util.c 15 KB

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