2 * (C) Copyright 2007 Schindler Lift Inc.
3 * (C) Copyright 2007 DENX Software Engineering
5 * Author: Michel Marti <mma@objectxp.com>
6 * Adapted for U-Boot 1.2 by Piotr Kruszynski <ppk@semihalf.com>:
8 * - bugfix for overwriting bootargs by user
10 * SPDX-License-Identifier: GPL-2.0+
23 static int load_rescue_image(ulong);
25 void cm5200_fwupdate(void)
31 char * const argv[3] = { "bootm", ka, NULL };
33 /* Check if rescue system is disabled... */
34 if (getenv("norescue")) {
35 printf(LOG_PREFIX "Rescue System disabled.\n");
39 /* Check if we have a USB storage device and load image */
40 if (load_rescue_image(LOAD_ADDR))
43 bcmd = find_cmd("bootm");
47 sprintf(ka, "%lx", (ulong)LOAD_ADDR);
49 /* prepare our bootargs */
50 rsargs = getenv("rs-args");
54 tmp = malloc(strlen(rsargs+1));
56 printf(LOG_PREFIX "Memory allocation failed\n");
63 setenv("bootargs", rsargs);
68 printf(LOG_PREFIX "Starting update system (bootargs=%s)...\n", rsargs);
69 do_bootm(bcmd, 0, 2, argv);
72 static int load_rescue_image(ulong addr)
74 disk_partition_t info;
83 char * const argv[6] = { "fatload", "usb", dev, addr_str, nxri, NULL };
84 struct blk_desc *stor_dev = NULL;
87 /* Get name of firmware directory */
88 tmp = getenv("fw-dir");
90 /* Copy it into fwdir */
91 strncpy(fwdir, tmp ? tmp : FW_DIR, sizeof(fwdir));
92 fwdir[sizeof(fwdir) - 1] = 0; /* Terminate string */
94 printf(LOG_PREFIX "Checking for firmware image directory '%s' on USB"
95 " storage...\n", fwdir);
100 /* Check for storage device */
101 if (usb_stor_scan(1) != 0) {
106 /* Detect storage device */
107 for (devno = 0; devno < USB_MAX_STOR_DEV; devno++) {
108 stor_dev = blk_get_devnum_by_type(IF_TYPE_USB, devno);
109 if (stor_dev->type != DEV_TYPE_UNKNOWN)
112 if (!stor_dev || stor_dev->type == DEV_TYPE_UNKNOWN) {
113 printf(LOG_PREFIX "No valid storage device found...\n");
118 /* Detect partition */
119 for (partno = -1, i = 0; i < 6; i++) {
120 if (part_get_info(stor_dev, i, &info) == 0) {
121 if (fat_register_device(stor_dev, i) == 0) {
122 /* Check if rescue image is present */
123 FW_DEBUG("Looking for firmware directory '%s'"
124 " on partition %d\n", fwdir, i);
125 if (!fat_exists(fwdir)) {
126 FW_DEBUG("No NX rescue image on "
127 "partition %d.\n", i);
131 FW_DEBUG("Partition %d contains "
132 "firmware directory\n", partno);
142 printf(LOG_PREFIX "Error: No valid (FAT) partition "
146 printf(LOG_PREFIX "Error: No NX rescue image on FAT "
150 printf(LOG_PREFIX "Error: Failed with code %d\n",
157 /* Load the rescue image */
158 bcmd = find_cmd("fatload");
160 printf(LOG_PREFIX "Error - 'fatload' command not present.\n");
165 tmp = getenv("nx-rescue-image");
166 sprintf(nxri, "%s/%s", fwdir, tmp ? tmp : RESCUE_IMAGE);
167 sprintf(dev, "%d:%d", devno, partno);
168 sprintf(addr_str, "%lx", addr);
170 FW_DEBUG("fat_fsload device='%s', addr='%s', file: %s\n",
171 dev, addr_str, nxri);
173 if (do_fat_fsload(bcmd, 0, 5, argv) != 0) {