Channagoud Kadabi | f280563 | 2015-01-07 16:46:50 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2015, The Linux Foundation. All rights reserved. |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 2 | |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 12 | * * Neither the name of The Linux Foundation nor the names of its |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <debug.h> |
| 30 | #include <arch/arm.h> |
| 31 | #include <dev/udc.h> |
| 32 | #include <string.h> |
| 33 | #include <kernel/thread.h> |
| 34 | #include <arch/ops.h> |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 35 | #include <arch/defines.h> |
| 36 | #include <malloc.h> |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 37 | |
| 38 | #include <dev/flash.h> |
| 39 | #include <lib/ptable.h> |
| 40 | #include <dev/keys.h> |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 41 | #include <platform.h> |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 42 | #include <target.h> |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 43 | #include <partition_parser.h> |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 44 | #include <mmc.h> |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 45 | #include <malloc.h> |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 46 | |
| 47 | #include "recovery.h" |
| 48 | #include "bootimg.h" |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 49 | #include "smem.h" |
| 50 | |
| 51 | #define BOOT_FLAGS 1 |
| 52 | #define UPDATE_STATUS 2 |
| 53 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 54 | |
| 55 | static const int MISC_PAGES = 3; // number of pages to save |
| 56 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 57 | static char buf[4096]; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 58 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 59 | unsigned boot_into_recovery = 0; |
| 60 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 61 | extern uint32_t get_page_size(); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 62 | extern void reset_device_info(); |
| 63 | extern void set_device_root(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 64 | |
| 65 | int get_recovery_message(struct recovery_message *out) |
| 66 | { |
| 67 | struct ptentry *ptn; |
| 68 | struct ptable *ptable; |
| 69 | unsigned offset = 0; |
| 70 | unsigned pagesize = flash_page_size(); |
| 71 | |
| 72 | ptable = flash_get_ptable(); |
| 73 | |
| 74 | if (ptable == NULL) { |
| 75 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 76 | return -1; |
| 77 | } |
| 78 | ptn = ptable_find(ptable, "misc"); |
| 79 | |
| 80 | if (ptn == NULL) { |
| 81 | dprintf(CRITICAL, "ERROR: No misc partition found\n"); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | offset += (pagesize * MISC_COMMAND_PAGE); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 86 | if (flash_read(ptn, offset, (void *) buf, pagesize)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 87 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 88 | return -1; |
| 89 | } |
| 90 | memcpy(out, buf, sizeof(*out)); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int set_recovery_message(const struct recovery_message *in) |
| 95 | { |
| 96 | struct ptentry *ptn; |
| 97 | struct ptable *ptable; |
| 98 | unsigned offset = 0; |
| 99 | unsigned pagesize = flash_page_size(); |
| 100 | unsigned n = 0; |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 101 | void *scratch_addr = target_get_scratch_address(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 102 | |
| 103 | ptable = flash_get_ptable(); |
| 104 | |
| 105 | if (ptable == NULL) { |
| 106 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 107 | return -1; |
| 108 | } |
| 109 | ptn = ptable_find(ptable, "misc"); |
| 110 | |
| 111 | if (ptn == NULL) { |
| 112 | dprintf(CRITICAL, "ERROR: No misc partition found\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | n = pagesize * (MISC_COMMAND_PAGE + 1); |
| 117 | |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 118 | if (flash_read(ptn, offset, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 119 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | offset += (pagesize * MISC_COMMAND_PAGE); |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 124 | offset += (unsigned) scratch_addr; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 125 | memcpy((void *) offset, in, sizeof(*in)); |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 126 | if (flash_write(ptn, 0, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 127 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 128 | return -1; |
| 129 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 130 | return 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | int read_update_header_for_bootloader(struct update_header *header) |
| 134 | { |
| 135 | struct ptentry *ptn; |
| 136 | struct ptable *ptable; |
| 137 | unsigned offset = 0; |
| 138 | unsigned pagesize = flash_page_size(); |
| 139 | |
| 140 | ptable = flash_get_ptable(); |
| 141 | if (ptable == NULL) { |
| 142 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 143 | return -1; |
| 144 | } |
| 145 | ptn = ptable_find(ptable, "cache"); |
| 146 | |
| 147 | if (ptn == NULL) { |
| 148 | dprintf(CRITICAL, "ERROR: No cache partition found\n"); |
| 149 | return -1; |
| 150 | } |
| 151 | if (flash_read(ptn, offset, buf, pagesize)) { |
| 152 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 153 | return -1; |
| 154 | } |
| 155 | memcpy(header, buf, sizeof(*header)); |
| 156 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 157 | if (strncmp((char *) header->MAGIC, UPDATE_MAGIC, UPDATE_MAGIC_SIZE)) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 158 | { |
| 159 | return -1; |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | int update_firmware_image (struct update_header *header, char *name) |
| 165 | { |
| 166 | struct ptentry *ptn; |
| 167 | struct ptable *ptable; |
| 168 | unsigned offset = 0; |
| 169 | unsigned pagesize = flash_page_size(); |
| 170 | unsigned pagemask = pagesize -1; |
| 171 | unsigned n = 0; |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 172 | void *scratch_addr = target_get_scratch_address(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 173 | |
| 174 | ptable = flash_get_ptable(); |
| 175 | if (ptable == NULL) { |
| 176 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | ptn = ptable_find(ptable, "cache"); |
| 181 | if (ptn == NULL) { |
| 182 | dprintf(CRITICAL, "ERROR: No cache partition found\n"); |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | offset += header->image_offset; |
| 187 | n = (header->image_length + pagemask) & (~pagemask); |
| 188 | |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 189 | if (flash_read(ptn, offset, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 190 | dprintf(CRITICAL, "ERROR: Cannot read radio image\n"); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | ptn = ptable_find(ptable, name); |
| 195 | if (ptn == NULL) { |
| 196 | dprintf(CRITICAL, "ERROR: No %s partition found\n", name); |
| 197 | return -1; |
| 198 | } |
| 199 | |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 200 | if (flash_write(ptn, 0, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 201 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | dprintf(INFO, "Partition writen successfully!"); |
| 206 | return 0; |
| 207 | } |
| 208 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 209 | static int set_ssd_radio_update (char *name) |
| 210 | { |
| 211 | struct ptentry *ptn; |
| 212 | struct ptable *ptable; |
| 213 | unsigned int ssd_cookie[2] = {0x53534443, 0x4F4F4B49}; |
| 214 | unsigned pagesize = flash_page_size(); |
| 215 | unsigned pagemask = pagesize -1; |
| 216 | unsigned n = 0; |
| 217 | |
| 218 | ptable = flash_get_ptable(); |
| 219 | if (ptable == NULL) { |
| 220 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | n = (sizeof(ssd_cookie) + pagemask) & (~pagemask); |
| 225 | |
| 226 | ptn = ptable_find(ptable, name); |
| 227 | if (ptn == NULL) { |
| 228 | dprintf(CRITICAL, "ERROR: No %s partition found\n", name); |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | if (flash_write(ptn, 0, ssd_cookie, n)) { |
| 233 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | dprintf(INFO, "FOTA partition written successfully!"); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | int get_boot_info_apps (char type, unsigned int *status) |
| 242 | { |
| 243 | boot_info_for_apps apps_boot_info; |
| 244 | int ret = 0; |
| 245 | |
| 246 | ret = smem_read_alloc_entry(SMEM_BOOT_INFO_FOR_APPS, |
| 247 | &apps_boot_info, sizeof(apps_boot_info)); |
| 248 | if (ret) |
| 249 | { |
| 250 | dprintf(CRITICAL, "ERROR: unable to read shared memory for apps boot info %d\n",ret); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | dprintf(INFO,"boot flag %x update status %x\n",apps_boot_info.boot_flags, |
| 255 | apps_boot_info.status.update_status); |
| 256 | |
| 257 | if(type == BOOT_FLAGS) |
| 258 | *status = apps_boot_info.boot_flags; |
| 259 | else if(type == UPDATE_STATUS) |
| 260 | *status = apps_boot_info.status.update_status; |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 265 | /* Bootloader / Recovery Flow |
| 266 | * |
| 267 | * On every boot, the bootloader will read the recovery_message |
| 268 | * from flash and check the command field. The bootloader should |
| 269 | * deal with the command field not having a 0 terminator correctly |
| 270 | * (so as to not crash if the block is invalid or corrupt). |
| 271 | * |
| 272 | * The bootloader will have to publish the partition that contains |
| 273 | * the recovery_message to the linux kernel so it can update it. |
| 274 | * |
| 275 | * if command == "boot-recovery" -> boot recovery.img |
| 276 | * else if command == "update-radio" -> update radio image (below) |
| 277 | * else -> boot boot.img (normal boot) |
| 278 | * |
| 279 | * Radio Update Flow |
| 280 | * 1. the bootloader will attempt to load and validate the header |
| 281 | * 2. if the header is invalid, status="invalid-update", goto #8 |
| 282 | * 3. display the busy image on-screen |
| 283 | * 4. if the update image is invalid, status="invalid-radio-image", goto #8 |
| 284 | * 5. attempt to update the firmware (depending on the command) |
| 285 | * 6. if successful, status="okay", goto #8 |
| 286 | * 7. if failed, and the old image can still boot, status="failed-update" |
| 287 | * 8. write the recovery_message, leaving the recovery field |
| 288 | * unchanged, updating status, and setting command to |
| 289 | * "boot-recovery" |
| 290 | * 9. reboot |
| 291 | * |
| 292 | * The bootloader will not modify or erase the cache partition. |
| 293 | * It is recovery's responsibility to clean up the mess afterwards. |
| 294 | */ |
| 295 | |
| 296 | int recovery_init (void) |
| 297 | { |
| 298 | struct recovery_message msg; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 299 | char partition_name[32]; |
| 300 | unsigned valid_command = 0; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 301 | int update_status = 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 302 | |
| 303 | // get recovery message |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 304 | if (get_recovery_message(&msg)) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 305 | return -1; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 306 | msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 307 | if (msg.command[0] != 0 && msg.command[0] != 255) { |
| 308 | dprintf(INFO,"Recovery command: %d %s\n", |
| 309 | sizeof(msg.command), msg.command); |
| 310 | } |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 311 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 312 | if (!strcmp("boot-recovery",msg.command)) |
| 313 | { |
| 314 | if(!strcmp("RADIO",msg.status)) |
| 315 | { |
| 316 | /* We're now here due to radio update, so check for update status */ |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 317 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 318 | |
| 319 | if(!ret && (update_status & 0x01)) |
| 320 | { |
| 321 | dprintf(INFO,"radio update success\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 322 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 323 | } |
| 324 | else |
| 325 | { |
| 326 | dprintf(INFO,"radio update failed\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 327 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 328 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 329 | strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 330 | set_recovery_message(&msg); // send recovery message |
| 331 | boot_into_recovery = 1; // Boot in recovery mode |
| 332 | return 0; |
| 333 | } |
| 334 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 335 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 336 | strlcpy(msg.command, "", sizeof(msg.command)); // to safe against multiple reboot into recovery |
| 337 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 338 | set_recovery_message(&msg); // send recovery message |
| 339 | boot_into_recovery = 1; // Boot in recovery mode |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | if (!strcmp("update-radio",msg.command)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 344 | dprintf(INFO,"start radio update\n"); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 345 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 346 | strlcpy(partition_name, "FOTA", sizeof(partition_name)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | //Todo: Add support for bootloader update too. |
| 350 | |
| 351 | if(!valid_command) { |
| 352 | //We need not to do anything |
| 353 | return 0; // Boot in normal mode |
| 354 | } |
| 355 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 356 | #ifdef OLD_FOTA_UPGRADE |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 357 | if (read_update_header_for_bootloader(&header)) { |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 358 | strlcpy(msg.status, "invalid-update", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 359 | goto SEND_RECOVERY_MSG; |
| 360 | } |
| 361 | |
| 362 | if (update_firmware_image (&header, partition_name)) { |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 363 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 364 | goto SEND_RECOVERY_MSG; |
| 365 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 366 | #else |
| 367 | if (set_ssd_radio_update(partition_name)) { |
| 368 | /* If writing to FOTA partition fails */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 369 | strlcpy(msg.command, "", sizeof(msg.command)); |
| 370 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 371 | goto SEND_RECOVERY_MSG; |
| 372 | } |
| 373 | else { |
| 374 | /* Setting this to check the radio update status */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 375 | strlcpy(msg.command, "boot-recovery", sizeof(msg.command)); |
Ajay Dudani | f4caa94 | 2011-10-02 08:57:15 -0700 | [diff] [blame] | 376 | strlcpy(msg.status, "RADIO", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 377 | goto SEND_RECOVERY_MSG; |
| 378 | } |
| 379 | #endif |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 380 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 381 | |
| 382 | SEND_RECOVERY_MSG: |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 383 | set_recovery_message(&msg); // send recovery message |
| 384 | boot_into_recovery = 1; // Boot in recovery mode |
| 385 | reboot_device(0); |
| 386 | return 0; |
| 387 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 388 | |
| 389 | static int emmc_set_recovery_msg(struct recovery_message *out) |
| 390 | { |
| 391 | char *ptn_name = "misc"; |
| 392 | unsigned long long ptn = 0; |
| 393 | unsigned int size = ROUND_TO_PAGE(sizeof(*out),511); |
| 394 | unsigned char data[size]; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 395 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 396 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 397 | index = partition_get_index((const char *) ptn_name); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 398 | ptn = partition_get_offset(index); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 399 | mmc_set_lun(partition_get_lun(index)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 400 | if(ptn == 0) { |
| 401 | dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name); |
| 402 | return -1; |
| 403 | } |
| 404 | memcpy(data, out, sizeof(*out)); |
| 405 | if (mmc_write(ptn , size, (unsigned int*)data)) { |
| 406 | dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out)); |
| 407 | return -1; |
| 408 | } |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int emmc_get_recovery_msg(struct recovery_message *in) |
| 413 | { |
| 414 | char *ptn_name = "misc"; |
| 415 | unsigned long long ptn = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 416 | unsigned int size; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 417 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 418 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 419 | size = mmc_get_device_blocksize(); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 420 | index = partition_get_index((const char *) ptn_name); |
Channagoud Kadabi | f280563 | 2015-01-07 16:46:50 -0800 | [diff] [blame] | 421 | if (index < 0) |
| 422 | { |
| 423 | dprintf(CRITICAL, "%s: Partition not found\n", ptn_name); |
| 424 | return -1; |
| 425 | } |
| 426 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 427 | ptn = partition_get_offset(index); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 428 | mmc_set_lun(partition_get_lun(index)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 429 | if(ptn == 0) { |
| 430 | dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name); |
| 431 | return -1; |
| 432 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 433 | if (mmc_read(ptn , (unsigned int*)in, size)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 434 | dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size); |
| 435 | return -1; |
| 436 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 437 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | int _emmc_recovery_init(void) |
| 442 | { |
| 443 | int update_status = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 444 | struct recovery_message *msg; |
| 445 | uint32_t block_size = 0; |
| 446 | |
| 447 | block_size = mmc_get_device_blocksize(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 448 | |
| 449 | // get recovery message |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 450 | msg = (struct recovery_message *)memalign(CACHE_LINE, block_size); |
| 451 | ASSERT(msg); |
| 452 | |
| 453 | if(emmc_get_recovery_msg(msg)) |
| 454 | { |
| 455 | if(msg) |
| 456 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 457 | return -1; |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 458 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 459 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 460 | msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination |
| 461 | if (msg->command[0] != 0 && msg->command[0] != 255) { |
| 462 | dprintf(INFO,"Recovery command: %d %s\n", |
| 463 | sizeof(msg->command), msg->command); |
| 464 | } |
| 465 | |
vijay kumar | 32e2e3c | 2014-08-01 21:10:28 +0530 | [diff] [blame] | 466 | if (!strcmp(msg->command, "boot-recovery")) { |
Stanimir Varbanov | be04199 | 2013-04-26 14:29:21 +0300 | [diff] [blame] | 467 | boot_into_recovery = 1; |
| 468 | } |
| 469 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 470 | if (!strcmp("update-radio",msg->command)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 471 | { |
| 472 | /* We're now here due to radio update, so check for update status */ |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 473 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 474 | |
| 475 | if(!ret && (update_status & 0x01)) |
| 476 | { |
| 477 | dprintf(INFO,"radio update success\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 478 | strlcpy(msg->status, "OKAY", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 479 | } |
| 480 | else |
| 481 | { |
| 482 | dprintf(INFO,"radio update failed\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 483 | strlcpy(msg->status, "failed-update", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 484 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 485 | boot_into_recovery = 1; // Boot in recovery mode |
| 486 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 487 | if (!strcmp("reset-device-info",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 488 | { |
| 489 | reset_device_info(); |
| 490 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 491 | if (!strcmp("root-detect",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 492 | { |
| 493 | set_device_root(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 494 | } |
| 495 | else |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 496 | goto out;// do nothing |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 497 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 498 | strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command |
| 499 | emmc_set_recovery_msg(msg); // send recovery message |
| 500 | |
| 501 | out: |
| 502 | if(msg) |
| 503 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 504 | return 0; |
| 505 | } |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 506 | |
| 507 | static int read_misc(unsigned page_offset, void *buf, unsigned size) |
| 508 | { |
| 509 | const char *ptn_name = "misc"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 510 | uint32_t pagesize = get_page_size(); |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 511 | unsigned offset; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 512 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 513 | if (size == 0 || buf == NULL) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 514 | return -1; |
| 515 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 516 | offset = page_offset * pagesize; |
| 517 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 518 | if (target_is_emmc_boot()) |
| 519 | { |
| 520 | int index; |
| 521 | unsigned long long ptn; |
| 522 | unsigned long long ptn_size; |
| 523 | |
| 524 | index = partition_get_index(ptn_name); |
| 525 | if (index == INVALID_PTN) |
| 526 | { |
| 527 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | ptn = partition_get_offset(index); |
| 532 | ptn_size = partition_get_size(index); |
| 533 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 534 | mmc_set_lun(partition_get_lun(index)); |
| 535 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 536 | if (ptn_size < offset + size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 537 | { |
| 538 | dprintf(CRITICAL, "Read request out of '%s' boundaries\n", |
| 539 | ptn_name); |
| 540 | return -1; |
| 541 | } |
| 542 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 543 | if (mmc_read(ptn + offset, (unsigned int *)buf, size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 544 | { |
| 545 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 546 | return -1; |
| 547 | } |
| 548 | } |
| 549 | else |
| 550 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 551 | dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n"); |
| 552 | return -1; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 553 | } |
| 554 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 558 | int write_misc(unsigned page_offset, void *buf, unsigned size) |
| 559 | { |
| 560 | const char *ptn_name = "misc"; |
| 561 | void *scratch_addr = target_get_scratch_address(); |
| 562 | unsigned offset; |
| 563 | unsigned aligned_size; |
| 564 | |
| 565 | if (size == 0 || buf == NULL || scratch_addr == NULL) |
| 566 | return -1; |
| 567 | |
| 568 | if (target_is_emmc_boot()) |
| 569 | { |
| 570 | int index; |
| 571 | unsigned long long ptn; |
| 572 | unsigned long long ptn_size; |
| 573 | |
| 574 | index = partition_get_index(ptn_name); |
| 575 | if (index == INVALID_PTN) |
| 576 | { |
| 577 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | ptn = partition_get_offset(index); |
| 582 | ptn_size = partition_get_size(index); |
| 583 | |
| 584 | offset = page_offset * BLOCK_SIZE; |
| 585 | aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1); |
| 586 | if (ptn_size < offset + aligned_size) |
| 587 | { |
| 588 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 589 | ptn_name); |
| 590 | return -1; |
| 591 | } |
| 592 | |
| 593 | if (scratch_addr != buf) |
| 594 | memcpy(scratch_addr, buf, size); |
| 595 | if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr)) |
| 596 | { |
| 597 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 598 | return -1; |
| 599 | } |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | struct ptentry *ptn; |
| 604 | struct ptable *ptable; |
| 605 | unsigned pagesize = flash_page_size(); |
| 606 | |
| 607 | ptable = flash_get_ptable(); |
| 608 | if (ptable == NULL) |
| 609 | { |
| 610 | dprintf(CRITICAL, "Partition table not found\n"); |
| 611 | return -1; |
| 612 | } |
| 613 | |
| 614 | ptn = ptable_find(ptable, ptn_name); |
| 615 | if (ptn == NULL) |
| 616 | { |
| 617 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 618 | return -1; |
| 619 | } |
| 620 | |
| 621 | offset = page_offset * pagesize; |
| 622 | aligned_size = ROUND_TO_PAGE(size, pagesize - 1); |
| 623 | if (ptn->length < offset + aligned_size) |
| 624 | { |
| 625 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 626 | ptn_name); |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | if (scratch_addr != buf) |
| 631 | memcpy(scratch_addr, buf, size); |
| 632 | if (flash_write(ptn, offset, scratch_addr, aligned_size)) { |
| 633 | dprintf(CRITICAL, "Writing flash failed\n"); |
| 634 | return -1; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 641 | int get_ffbm(char *ffbm, unsigned size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 642 | { |
| 643 | const char *ffbm_cmd = "ffbm-"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 644 | uint32_t page_size = get_page_size(); |
| 645 | char *ffbm_page_buffer = NULL; |
| 646 | int retval = 0; |
| 647 | if (size < FFBM_MODE_BUF_SIZE || size >= page_size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 648 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 649 | dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n"); |
| 650 | retval = -1; |
| 651 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 652 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 653 | ffbm_page_buffer = (char*)malloc(page_size); |
| 654 | if (!ffbm_page_buffer) |
| 655 | { |
| 656 | dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n"); |
| 657 | retval = -1; |
| 658 | goto cleanup; |
| 659 | } |
| 660 | if (read_misc(0, ffbm_page_buffer, page_size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 661 | { |
| 662 | dprintf(CRITICAL, "Error reading MISC partition\n"); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 663 | retval = -1; |
| 664 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 665 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 666 | ffbm_page_buffer[size] = '\0'; |
| 667 | if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd))) |
| 668 | { |
| 669 | retval = 0; |
| 670 | goto cleanup; |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | if (strlcpy(ffbm, ffbm_page_buffer, size) < |
| 675 | FFBM_MODE_BUF_SIZE -1) |
| 676 | { |
| 677 | dprintf(CRITICAL, "Invalid string in misc partition\n"); |
| 678 | retval = -1; |
| 679 | } |
| 680 | else |
| 681 | retval = 1; |
| 682 | } |
| 683 | cleanup: |
| 684 | if(ffbm_page_buffer) |
| 685 | free(ffbm_page_buffer); |
| 686 | return retval; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 687 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 688 | |
| 689 | |