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 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 (uimage_to_cpu (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 ((char *)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;
  163. uchar *data;
  164. image_header_t *hdr = (image_header_t *)ld_addr;
  165. int rc;
  166. #if defined(CONFIG_FIT)
  167. if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
  168. puts ("Non legacy image format not supported\n");
  169. return -1;
  170. }
  171. #endif
  172. if (!image_check_magic (hdr)) {
  173. puts("Bad Magic Number\n");
  174. return 1;
  175. }
  176. image_print_contents (hdr);
  177. if (!image_check_os (hdr, IH_OS_U_BOOT)) {
  178. puts("No U-Boot Image\n");
  179. return 1;
  180. }
  181. if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
  182. puts("No Firmware Image\n");
  183. return 1;
  184. }
  185. if (!image_check_hcrc (hdr)) {
  186. puts("Bad Header Checksum\n");
  187. return 1;
  188. }
  189. puts("Verifying Checksum ... ");
  190. if (!image_check_dcrc (hdr)) {
  191. puts("Bad Data CRC\n");
  192. return 1;
  193. }
  194. puts("OK\n");
  195. data = (uchar *)image_get_data (hdr);
  196. len = image_get_data_size (hdr);
  197. if (image_get_comp (hdr) != 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 (image_get_comp (hdr)) {
  205. case IH_COMP_GZIP:
  206. puts("Uncompressing (GZIP) ... ");
  207. rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
  208. if (rc != 0) {
  209. puts("GUNZIP ERROR\n");
  210. free(buf);
  211. return 1;
  212. }
  213. puts("OK\n");
  214. break;
  215. #ifdef 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",
  234. image_get_comp (hdr));
  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 */