blob: 389b91dc6396e8b6e37c37c072c9f9698d13b805 [file] [log] [blame]
Mayank Grover98c4c742019-04-25 17:21:37 +05301/* Copyright (c) 2010-2017,2019 The Linux Foundation. All rights reserved.
Shashank Mittal024c0332010-02-03 11:44:00 -08002
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 Truongf3ac7b32013-02-13 01:07:28 -080012 * * Neither the name of The Linux Foundation nor the names of its
Shashank Mittal024c0332010-02-03 11:44:00 -080013 * 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 Srinivasan151c04d2014-07-17 15:52:12 -070035#include <arch/defines.h>
36#include <malloc.h>
Shashank Mittal024c0332010-02-03 11:44:00 -080037
38#include <dev/flash.h>
39#include <lib/ptable.h>
40#include <dev/keys.h>
Greg Griscod6250552011-06-29 14:40:23 -070041#include <platform.h>
Pavel Nedeva1e62322013-04-05 15:21:36 +030042#include <target.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070043#include <partition_parser.h>
Greg Griscod2471ef2011-07-14 13:00:42 -070044#include <mmc.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053045#include <malloc.h>
Mayank Grover7e40ad22017-11-13 17:17:53 +053046#include <stdlib.h>
Shashank Mittal024c0332010-02-03 11:44:00 -080047#include "recovery.h"
48#include "bootimg.h"
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -070049#include "smem.h"
50
51#define BOOT_FLAGS 1
52#define UPDATE_STATUS 2
Shashank Mittal024c0332010-02-03 11:44:00 -080053
54static const int MISC_PAGES = 3; // number of pages to save
55static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
56static char buf[4096];
Pavel Nedeva1e62322013-04-05 15:21:36 +030057
Shashank Mittal024c0332010-02-03 11:44:00 -080058unsigned boot_into_recovery = 0;
59
Deepa Dinamani41fa8d62013-05-23 13:25:36 -070060extern uint32_t get_page_size();
Shashank Mittal162244e2011-08-08 19:01:25 -070061extern void reset_device_info();
62extern void set_device_root();
Shashank Mittal024c0332010-02-03 11:44:00 -080063
64int get_recovery_message(struct recovery_message *out)
65{
66 struct ptentry *ptn;
67 struct ptable *ptable;
68 unsigned offset = 0;
69 unsigned pagesize = flash_page_size();
70
71 ptable = flash_get_ptable();
72
73 if (ptable == NULL) {
74 dprintf(CRITICAL, "ERROR: Partition table not found\n");
75 return -1;
76 }
77 ptn = ptable_find(ptable, "misc");
78
79 if (ptn == NULL) {
80 dprintf(CRITICAL, "ERROR: No misc partition found\n");
81 return -1;
82 }
83
84 offset += (pagesize * MISC_COMMAND_PAGE);
Greg Griscod6250552011-06-29 14:40:23 -070085 if (flash_read(ptn, offset, (void *) buf, pagesize)) {
Shashank Mittal024c0332010-02-03 11:44:00 -080086 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
87 return -1;
88 }
89 memcpy(out, buf, sizeof(*out));
90 return 0;
91}
92
93int set_recovery_message(const struct recovery_message *in)
94{
95 struct ptentry *ptn;
96 struct ptable *ptable;
97 unsigned offset = 0;
98 unsigned pagesize = flash_page_size();
99 unsigned n = 0;
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300100 void *scratch_addr = target_get_scratch_address();
Shashank Mittal024c0332010-02-03 11:44:00 -0800101
102 ptable = flash_get_ptable();
103
104 if (ptable == NULL) {
105 dprintf(CRITICAL, "ERROR: Partition table not found\n");
106 return -1;
107 }
108 ptn = ptable_find(ptable, "misc");
109
110 if (ptn == NULL) {
111 dprintf(CRITICAL, "ERROR: No misc partition found\n");
112 return -1;
113 }
114
115 n = pagesize * (MISC_COMMAND_PAGE + 1);
116
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300117 if (flash_read(ptn, offset, scratch_addr, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800118 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
119 return -1;
120 }
121
122 offset += (pagesize * MISC_COMMAND_PAGE);
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300123 offset += (unsigned) scratch_addr;
Greg Griscod6250552011-06-29 14:40:23 -0700124 memcpy((void *) offset, in, sizeof(*in));
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300125 if (flash_write(ptn, 0, scratch_addr, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800126 dprintf(CRITICAL, "ERROR: flash write fail!\n");
127 return -1;
128 }
Greg Griscod6250552011-06-29 14:40:23 -0700129 return 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800130}
131
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700132static int set_ssd_radio_update (char *name)
133{
134 struct ptentry *ptn;
135 struct ptable *ptable;
Mayank Groverba853de2017-06-15 16:20:18 +0530136 unsigned int *ssd_cookie;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700137 unsigned pagesize = flash_page_size();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700138
139 ptable = flash_get_ptable();
140 if (ptable == NULL) {
141 dprintf(CRITICAL, "ERROR: Partition table not found\n");
142 return -1;
143 }
144
Mayank Groverba853de2017-06-15 16:20:18 +0530145 ssd_cookie = malloc(pagesize);
146 if (!ssd_cookie){
147 dprintf(CRITICAL, "ERROR: Memory allocation failure\n");
148 return -1;
149 }
150 memset(ssd_cookie, 0, pagesize);
151 ssd_cookie[0] = 0x53534443;
152 ssd_cookie[1] = 0x4F4F4B49;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700153
154 ptn = ptable_find(ptable, name);
155 if (ptn == NULL) {
156 dprintf(CRITICAL, "ERROR: No %s partition found\n", name);
Mayank Groverba853de2017-06-15 16:20:18 +0530157 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700158 }
159
Mayank Groverba853de2017-06-15 16:20:18 +0530160 if (flash_write(ptn, 0, ssd_cookie, pagesize)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700161 dprintf(CRITICAL, "ERROR: flash write fail!\n");
Mayank Groverba853de2017-06-15 16:20:18 +0530162 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700163 }
164
Mayank Groverba853de2017-06-15 16:20:18 +0530165 free(ssd_cookie);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700166 dprintf(INFO, "FOTA partition written successfully!");
167 return 0;
Mayank Groverba853de2017-06-15 16:20:18 +0530168out:
169 free(ssd_cookie);
170 return -1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700171}
172
173int get_boot_info_apps (char type, unsigned int *status)
174{
175 boot_info_for_apps apps_boot_info;
176 int ret = 0;
177
178 ret = smem_read_alloc_entry(SMEM_BOOT_INFO_FOR_APPS,
179 &apps_boot_info, sizeof(apps_boot_info));
180 if (ret)
181 {
182 dprintf(CRITICAL, "ERROR: unable to read shared memory for apps boot info %d\n",ret);
183 return ret;
184 }
185
186 dprintf(INFO,"boot flag %x update status %x\n",apps_boot_info.boot_flags,
187 apps_boot_info.status.update_status);
188
189 if(type == BOOT_FLAGS)
190 *status = apps_boot_info.boot_flags;
191 else if(type == UPDATE_STATUS)
192 *status = apps_boot_info.status.update_status;
193
194 return ret;
195}
196
Shashank Mittal024c0332010-02-03 11:44:00 -0800197/* Bootloader / Recovery Flow
198 *
199 * On every boot, the bootloader will read the recovery_message
200 * from flash and check the command field. The bootloader should
201 * deal with the command field not having a 0 terminator correctly
202 * (so as to not crash if the block is invalid or corrupt).
203 *
204 * The bootloader will have to publish the partition that contains
205 * the recovery_message to the linux kernel so it can update it.
206 *
207 * if command == "boot-recovery" -> boot recovery.img
208 * else if command == "update-radio" -> update radio image (below)
209 * else -> boot boot.img (normal boot)
210 *
211 * Radio Update Flow
212 * 1. the bootloader will attempt to load and validate the header
213 * 2. if the header is invalid, status="invalid-update", goto #8
214 * 3. display the busy image on-screen
215 * 4. if the update image is invalid, status="invalid-radio-image", goto #8
216 * 5. attempt to update the firmware (depending on the command)
217 * 6. if successful, status="okay", goto #8
218 * 7. if failed, and the old image can still boot, status="failed-update"
219 * 8. write the recovery_message, leaving the recovery field
220 * unchanged, updating status, and setting command to
221 * "boot-recovery"
222 * 9. reboot
223 *
224 * The bootloader will not modify or erase the cache partition.
225 * It is recovery's responsibility to clean up the mess afterwards.
226 */
227
228int recovery_init (void)
229{
230 struct recovery_message msg;
Shashank Mittal024c0332010-02-03 11:44:00 -0800231 char partition_name[32];
232 unsigned valid_command = 0;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700233 int update_status = 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800234
235 // get recovery message
Greg Griscod6250552011-06-29 14:40:23 -0700236 if (get_recovery_message(&msg))
Shashank Mittal024c0332010-02-03 11:44:00 -0800237 return -1;
Shashank Mittal024c0332010-02-03 11:44:00 -0800238 msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800239 if (msg.command[0] != 0 && msg.command[0] != 255) {
240 dprintf(INFO,"Recovery command: %d %s\n",
241 sizeof(msg.command), msg.command);
242 }
Shashank Mittal024c0332010-02-03 11:44:00 -0800243
Mayank Grover98c4c742019-04-25 17:21:37 +0530244 if (!strcmp(RECOVERY_BOOT_RECOVERY_CMD, msg.command))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700245 {
246 if(!strcmp("RADIO",msg.status))
247 {
248 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700249 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700250
251 if(!ret && (update_status & 0x01))
252 {
253 dprintf(INFO,"radio update success\n");
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700254 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700255 }
256 else
257 {
258 dprintf(INFO,"radio update failed\n");
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700259 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700260 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700261 strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700262 set_recovery_message(&msg); // send recovery message
263 boot_into_recovery = 1; // Boot in recovery mode
264 return 0;
265 }
Shashank Mittal024c0332010-02-03 11:44:00 -0800266 boot_into_recovery = 1; // Boot in recovery mode
267 return 0;
268 }
269
Mayank Grover98c4c742019-04-25 17:21:37 +0530270 if (target_dynamic_partition_supported() &&
271 !strcmp(RECOVERY_BOOT_FASTBOOT_CMD, msg.command)) {
272 boot_into_recovery = 1; // Boot in userspace fastboot mode
273 return 0;
274 }
275
Shashank Mittal024c0332010-02-03 11:44:00 -0800276 if (!strcmp("update-radio",msg.command)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700277 dprintf(INFO,"start radio update\n");
Shashank Mittal024c0332010-02-03 11:44:00 -0800278 valid_command = 1;
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700279 strlcpy(partition_name, "FOTA", sizeof(partition_name));
Shashank Mittal024c0332010-02-03 11:44:00 -0800280 }
281
282 //Todo: Add support for bootloader update too.
283
284 if(!valid_command) {
285 //We need not to do anything
286 return 0; // Boot in normal mode
287 }
288
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700289 if (set_ssd_radio_update(partition_name)) {
290 /* If writing to FOTA partition fails */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700291 strlcpy(msg.command, "", sizeof(msg.command));
292 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700293 goto SEND_RECOVERY_MSG;
294 }
295 else {
296 /* Setting this to check the radio update status */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700297 strlcpy(msg.command, "boot-recovery", sizeof(msg.command));
Ajay Dudanif4caa942011-10-02 08:57:15 -0700298 strlcpy(msg.status, "RADIO", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700299 goto SEND_RECOVERY_MSG;
300 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700301 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Shashank Mittal024c0332010-02-03 11:44:00 -0800302
303SEND_RECOVERY_MSG:
Shashank Mittal024c0332010-02-03 11:44:00 -0800304 set_recovery_message(&msg); // send recovery message
305 boot_into_recovery = 1; // Boot in recovery mode
306 reboot_device(0);
307 return 0;
308}
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700309
310static int emmc_set_recovery_msg(struct recovery_message *out)
311{
312 char *ptn_name = "misc";
313 unsigned long long ptn = 0;
Mayank Grover9db97242017-06-16 12:41:56 +0530314 unsigned blocksize = mmc_get_device_blocksize();
315 unsigned int size = ROUND_TO_PAGE(sizeof(*out), (unsigned)blocksize - 1);
316 unsigned char *data = NULL;
317 int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700318 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700319
Mayank Grover9db97242017-06-16 12:41:56 +0530320 data = malloc(size);
321 if(!data)
322 {
323 dprintf(CRITICAL,"memory allocation error \n");
324 ret = -1;
325 goto out;
326 }
327
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530328 index = partition_get_index((const char *) ptn_name);
Kinson Chikf1a43512011-07-14 11:28:39 -0700329 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700330 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700331 if(ptn == 0) {
332 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
Mayank Grover9db97242017-06-16 12:41:56 +0530333 ret = -1;
334 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700335 }
Mayank Grover9db97242017-06-16 12:41:56 +0530336 memset(data, 0, size);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700337 memcpy(data, out, sizeof(*out));
338 if (mmc_write(ptn , size, (unsigned int*)data)) {
339 dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out));
Mayank Grover9db97242017-06-16 12:41:56 +0530340 ret = -1;
341 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700342 }
Mayank Grover9db97242017-06-16 12:41:56 +0530343out:
344 if (data)
345 free(data);
346 return ret;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700347}
348
349static int emmc_get_recovery_msg(struct recovery_message *in)
350{
351 char *ptn_name = "misc";
352 unsigned long long ptn = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700353 unsigned int size;
Kinson Chikf1a43512011-07-14 11:28:39 -0700354 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700355
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700356 size = mmc_get_device_blocksize();
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530357 index = partition_get_index((const char *) ptn_name);
Channagoud Kadabif2805632015-01-07 16:46:50 -0800358 if (index < 0)
359 {
360 dprintf(CRITICAL, "%s: Partition not found\n", ptn_name);
361 return -1;
362 }
363
Kinson Chikf1a43512011-07-14 11:28:39 -0700364 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700365 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700366 if(ptn == 0) {
367 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
368 return -1;
369 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700370 if (mmc_read(ptn , (unsigned int*)in, size)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700371 dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size);
372 return -1;
373 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700374
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700375 return 0;
376}
377
Mayank Grover98c4c742019-04-25 17:21:37 +0530378/* Generic funcition to write misc commands. */
379int send_recovery_cmd(const char *command)
380{
381 struct recovery_message msg;
382 int status = 0;
383 memset(&msg, 0, sizeof(msg));
384
385 /* Populate command to msg */
386 snprintf(msg.command,
387 sizeof(msg.command),
388 command);
389
390 dprintf(INFO,"Recovery command: %s\n", msg.command);
391 if (target_is_emmc_boot())
392 /* Update emmc partition */
393 status = emmc_set_recovery_msg(&msg);
394 else
395 status = set_recovery_message(&msg);
396
397 return status;
398}
399
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700400int _emmc_recovery_init(void)
401{
402 int update_status = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700403 struct recovery_message *msg;
404 uint32_t block_size = 0;
405
406 block_size = mmc_get_device_blocksize();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700407
408 // get recovery message
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700409 msg = (struct recovery_message *)memalign(CACHE_LINE, block_size);
410 ASSERT(msg);
411
412 if(emmc_get_recovery_msg(msg))
413 {
414 if(msg)
415 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700416 return -1;
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800417 }
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700418
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700419 msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination
420 if (msg->command[0] != 0 && msg->command[0] != 255) {
421 dprintf(INFO,"Recovery command: %d %s\n",
422 sizeof(msg->command), msg->command);
423 }
424
Mayank Grover98c4c742019-04-25 17:21:37 +0530425 if (!strcmp(msg->command, RECOVERY_BOOT_RECOVERY_CMD)) {
426 boot_into_recovery = 1;
427 }
428
429 if (target_dynamic_partition_supported() &&
430 !strcmp(msg->command, RECOVERY_BOOT_FASTBOOT_CMD)) {
Stanimir Varbanovbe041992013-04-26 14:29:21 +0300431 boot_into_recovery = 1;
432 }
433
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700434 if (!strcmp("update-radio",msg->command))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700435 {
436 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700437 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700438
439 if(!ret && (update_status & 0x01))
440 {
441 dprintf(INFO,"radio update success\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700442 strlcpy(msg->status, "OKAY", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700443 }
444 else
445 {
446 dprintf(INFO,"radio update failed\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700447 strlcpy(msg->status, "failed-update", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700448 }
Shashank Mittal162244e2011-08-08 19:01:25 -0700449 boot_into_recovery = 1; // Boot in recovery mode
450 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700451 if (!strcmp("reset-device-info",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700452 {
453 reset_device_info();
454 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700455 if (!strcmp("root-detect",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700456 {
457 set_device_root();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700458 }
459 else
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700460 goto out;// do nothing
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700461
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700462 strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command
463 emmc_set_recovery_msg(msg); // send recovery message
464
465out:
466 if(msg)
467 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700468 return 0;
469}
Pavel Nedeva1e62322013-04-05 15:21:36 +0300470
471static int read_misc(unsigned page_offset, void *buf, unsigned size)
472{
473 const char *ptn_name = "misc";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700474 uint32_t pagesize = get_page_size();
Pavel Nedeva1e62322013-04-05 15:21:36 +0300475 unsigned offset;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300476
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700477 if (size == 0 || buf == NULL)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300478 return -1;
479
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700480 offset = page_offset * pagesize;
481
Pavel Nedeva1e62322013-04-05 15:21:36 +0300482 if (target_is_emmc_boot())
483 {
484 int index;
485 unsigned long long ptn;
486 unsigned long long ptn_size;
487
488 index = partition_get_index(ptn_name);
489 if (index == INVALID_PTN)
490 {
491 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
492 return -1;
493 }
494
495 ptn = partition_get_offset(index);
496 ptn_size = partition_get_size(index);
497
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700498 mmc_set_lun(partition_get_lun(index));
499
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700500 if (ptn_size < offset + size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300501 {
502 dprintf(CRITICAL, "Read request out of '%s' boundaries\n",
503 ptn_name);
504 return -1;
505 }
506
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700507 if (mmc_read(ptn + offset, (unsigned int *)buf, size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300508 {
509 dprintf(CRITICAL, "Reading MMC failed\n");
510 return -1;
511 }
512 }
513 else
514 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700515 dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n");
516 return -1;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300517 }
518
Pavel Nedeva1e62322013-04-05 15:21:36 +0300519 return 0;
520}
521
Pavel Nedevc728bb32013-04-05 16:58:05 +0300522int write_misc(unsigned page_offset, void *buf, unsigned size)
523{
524 const char *ptn_name = "misc";
525 void *scratch_addr = target_get_scratch_address();
526 unsigned offset;
527 unsigned aligned_size;
528
529 if (size == 0 || buf == NULL || scratch_addr == NULL)
530 return -1;
531
532 if (target_is_emmc_boot())
533 {
534 int index;
535 unsigned long long ptn;
536 unsigned long long ptn_size;
537
538 index = partition_get_index(ptn_name);
539 if (index == INVALID_PTN)
540 {
541 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
542 return -1;
543 }
544
545 ptn = partition_get_offset(index);
546 ptn_size = partition_get_size(index);
547
548 offset = page_offset * BLOCK_SIZE;
549 aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1);
550 if (ptn_size < offset + aligned_size)
551 {
552 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
553 ptn_name);
554 return -1;
555 }
556
Mayank Grover467bf492017-10-24 12:27:53 +0530557 /* This will ensure, we zeored out any extra bytes
558 we will push to emmc, to prevent information leak */
559 if (aligned_size > size)
560 memset((scratch_addr + size), 0, (aligned_size-size));
561
Pavel Nedevc728bb32013-04-05 16:58:05 +0300562 if (scratch_addr != buf)
563 memcpy(scratch_addr, buf, size);
Channagoud Kadabi2776b652015-08-28 15:41:26 -0700564
565 /* Set Lun for misc partition */
566 mmc_set_lun(partition_get_lun(index));
567
Pavel Nedevc728bb32013-04-05 16:58:05 +0300568 if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr))
569 {
570 dprintf(CRITICAL, "Writing MMC failed\n");
571 return -1;
572 }
573 }
574 else
575 {
576 struct ptentry *ptn;
577 struct ptable *ptable;
578 unsigned pagesize = flash_page_size();
579
580 ptable = flash_get_ptable();
581 if (ptable == NULL)
582 {
583 dprintf(CRITICAL, "Partition table not found\n");
584 return -1;
585 }
586
587 ptn = ptable_find(ptable, ptn_name);
588 if (ptn == NULL)
589 {
590 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
591 return -1;
592 }
593
594 offset = page_offset * pagesize;
595 aligned_size = ROUND_TO_PAGE(size, pagesize - 1);
596 if (ptn->length < offset + aligned_size)
597 {
598 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
599 ptn_name);
600 return -1;
601 }
602
Mayank Grover467bf492017-10-24 12:27:53 +0530603 /* This will ensure, we zeored out any extra bytes
604 we will push, to prevent information leak */
605 if (aligned_size > size)
606 memset((scratch_addr + size), 0, (aligned_size-size));
607
Pavel Nedevc728bb32013-04-05 16:58:05 +0300608 if (scratch_addr != buf)
609 memcpy(scratch_addr, buf, size);
Mayank Grover467bf492017-10-24 12:27:53 +0530610
Pavel Nedevc728bb32013-04-05 16:58:05 +0300611 if (flash_write(ptn, offset, scratch_addr, aligned_size)) {
612 dprintf(CRITICAL, "Writing flash failed\n");
613 return -1;
614 }
615 }
616
617 return 0;
618}
619
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700620int get_ffbm(char *ffbm, unsigned size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300621{
622 const char *ffbm_cmd = "ffbm-";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700623 uint32_t page_size = get_page_size();
624 char *ffbm_page_buffer = NULL;
625 int retval = 0;
626 if (size < FFBM_MODE_BUF_SIZE || size >= page_size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300627 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700628 dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n");
629 retval = -1;
630 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300631 }
Parth Dixit75b16812016-04-05 19:17:17 +0530632 ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700633 if (!ffbm_page_buffer)
634 {
635 dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n");
636 retval = -1;
637 goto cleanup;
638 }
639 if (read_misc(0, ffbm_page_buffer, page_size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300640 {
641 dprintf(CRITICAL, "Error reading MISC partition\n");
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700642 retval = -1;
643 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300644 }
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700645 ffbm_page_buffer[size] = '\0';
646 if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd)))
647 {
648 retval = 0;
649 goto cleanup;
650 }
651 else
652 {
653 if (strlcpy(ffbm, ffbm_page_buffer, size) <
654 FFBM_MODE_BUF_SIZE -1)
655 {
656 dprintf(CRITICAL, "Invalid string in misc partition\n");
657 retval = -1;
658 }
659 else
660 retval = 1;
661 }
662cleanup:
663 if(ffbm_page_buffer)
664 free(ffbm_page_buffer);
665 return retval;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300666}
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700667
668