fwupdate.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * (C) Copyright 2007 Schindler Lift Inc.
  3. * (C) Copyright 2007 DENX Software Engineering
  4. *
  5. * Author: Michel Marti <mma@objectxp.com>
  6. * Adapted for U-Boot 1.2 by Piotr Kruszynski <ppk@semihalf.com>:
  7. * - code clean-up
  8. * - bugfix for overwriting bootargs by user
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <command.h>
  30. #include <malloc.h>
  31. #include <image.h>
  32. #include <usb.h>
  33. #include <fat.h>
  34. #include "fwupdate.h"
  35. extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
  36. extern long do_fat_read(const char *, void *, unsigned long, int);
  37. extern int do_fat_fsload(cmd_tbl_t *, int, int, char *[]);
  38. static int load_rescue_image(ulong);
  39. void cm5200_fwupdate(void)
  40. {
  41. cmd_tbl_t *bcmd;
  42. char *rsargs;
  43. char *tmp = NULL;
  44. char ka[16];
  45. char *argv[3] = { "bootm", ka, NULL };
  46. /* Check if rescue system is disabled... */
  47. if (getenv("norescue")) {
  48. printf(LOG_PREFIX "Rescue System disabled.\n");
  49. return;
  50. }
  51. /* Check if we have a USB storage device and load image */
  52. if (load_rescue_image(LOAD_ADDR))
  53. return;
  54. bcmd = find_cmd("bootm");
  55. if (!bcmd)
  56. return;
  57. sprintf(ka, "%lx", LOAD_ADDR);
  58. /* prepare our bootargs */
  59. rsargs = getenv("rs-args");
  60. if (!rsargs)
  61. rsargs = RS_BOOTARGS;
  62. else {
  63. tmp = malloc(strlen(rsargs+1));
  64. if (!tmp) {
  65. printf(LOG_PREFIX "Memory allocation failed\n");
  66. return;
  67. }
  68. strcpy(tmp, rsargs);
  69. rsargs = tmp;
  70. }
  71. setenv("bootargs", rsargs);
  72. if (rsargs == tmp)
  73. free(rsargs);
  74. printf(LOG_PREFIX "Starting update system (bootargs=%s)...\n", rsargs);
  75. do_bootm(bcmd, 0, 2, argv);
  76. }
  77. static int load_rescue_image(ulong addr)
  78. {
  79. disk_partition_t info;
  80. int devno;
  81. int partno;
  82. int i;
  83. char fwdir[64];
  84. char nxri[128];
  85. char *tmp;
  86. char dev[7];
  87. char addr_str[16];
  88. char *argv[6] = { "fatload", "usb", dev, addr_str, nxri, NULL };
  89. block_dev_desc_t *stor_dev = NULL;
  90. cmd_tbl_t *bcmd;
  91. /* Get name of firmware directory */
  92. tmp = getenv("fw-dir");
  93. /* Copy it into fwdir */
  94. strncpy(fwdir, tmp ? tmp : FW_DIR, sizeof(fwdir));
  95. fwdir[sizeof(fwdir) - 1] = 0; /* Terminate string */
  96. printf(LOG_PREFIX "Checking for firmware image directory '%s' on USB"
  97. " storage...\n", fwdir);
  98. usb_stop();
  99. if (usb_init() != 0)
  100. return 1;
  101. /* Check for storage device */
  102. if (usb_stor_scan(1) != 0) {
  103. usb_stop();
  104. return 1;
  105. }
  106. /* Detect storage device */
  107. for (devno = 0; devno < USB_MAX_STOR_DEV; devno++) {
  108. stor_dev = usb_stor_get_dev(devno);
  109. if (stor_dev->type != DEV_TYPE_UNKNOWN)
  110. break;
  111. }
  112. if (!stor_dev || stor_dev->type == DEV_TYPE_UNKNOWN) {
  113. printf(LOG_PREFIX "No valid storage device found...\n");
  114. usb_stop();
  115. return 1;
  116. }
  117. /* Detect partition */
  118. for (partno = -1, i = 0; i < 6; i++) {
  119. if (get_partition_info(stor_dev, i, &info) == 0) {
  120. if (fat_register_device(stor_dev, i) == 0) {
  121. /* Check if rescue image is present */
  122. FW_DEBUG("Looking for firmware directory '%s'"
  123. " on partition %d\n", fwdir, i);
  124. if (do_fat_read(fwdir, NULL, 0, LS_NO) == -1) {
  125. FW_DEBUG("No NX rescue image on "
  126. "partition %d.\n", i);
  127. partno = -2;
  128. } else {
  129. partno = i;
  130. FW_DEBUG("Partition %d contains "
  131. "firmware directory\n", partno);
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. if (partno < 0) {
  138. switch (partno) {
  139. case -1:
  140. printf(LOG_PREFIX "Error: No valid (FAT) partition "
  141. "detected\n");
  142. break;
  143. case -2:
  144. printf(LOG_PREFIX "Error: No NX rescue image on FAT "
  145. "partition\n");
  146. break;
  147. default:
  148. printf(LOG_PREFIX "Error: Failed with code %d\n",
  149. partno);
  150. }
  151. usb_stop();
  152. return 1;
  153. }
  154. /* Load the rescue image */
  155. bcmd = find_cmd("fatload");
  156. if (!bcmd) {
  157. printf(LOG_PREFIX "Error - 'fatload' command not present.\n");
  158. usb_stop();
  159. return 1;
  160. }
  161. tmp = getenv("nx-rescue-image");
  162. sprintf(nxri, "%s/%s", fwdir, tmp ? tmp : RESCUE_IMAGE);
  163. sprintf(dev, "%d:%d", devno, partno);
  164. sprintf(addr_str, "%lx", addr);
  165. FW_DEBUG("fat_fsload device='%s', addr='%s', file: %s\n",
  166. dev, addr_str, nxri);
  167. if (do_fat_fsload(bcmd, 0, 5, argv) != 0) {
  168. usb_stop();
  169. return 1;
  170. }
  171. /* Stop USB */
  172. usb_stop();
  173. return 0;
  174. }