blob: 0cc2cebc1d136ea7576fbe4ab3821a401f353ed0 [file] [log] [blame]
Kishor PK8a2783f2017-04-20 14:51:04 +05301/* Copyright (c) 2010-2017, 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
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700244 if (!strcmp("boot-recovery",msg.command))
245 {
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
270 if (!strcmp("update-radio",msg.command)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700271 dprintf(INFO,"start radio update\n");
Shashank Mittal024c0332010-02-03 11:44:00 -0800272 valid_command = 1;
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700273 strlcpy(partition_name, "FOTA", sizeof(partition_name));
Shashank Mittal024c0332010-02-03 11:44:00 -0800274 }
275
276 //Todo: Add support for bootloader update too.
277
278 if(!valid_command) {
279 //We need not to do anything
280 return 0; // Boot in normal mode
281 }
282
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700283 if (set_ssd_radio_update(partition_name)) {
284 /* If writing to FOTA partition fails */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700285 strlcpy(msg.command, "", sizeof(msg.command));
286 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700287 goto SEND_RECOVERY_MSG;
288 }
289 else {
290 /* Setting this to check the radio update status */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700291 strlcpy(msg.command, "boot-recovery", sizeof(msg.command));
Ajay Dudanif4caa942011-10-02 08:57:15 -0700292 strlcpy(msg.status, "RADIO", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700293 goto SEND_RECOVERY_MSG;
294 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700295 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Shashank Mittal024c0332010-02-03 11:44:00 -0800296
297SEND_RECOVERY_MSG:
Shashank Mittal024c0332010-02-03 11:44:00 -0800298 set_recovery_message(&msg); // send recovery message
299 boot_into_recovery = 1; // Boot in recovery mode
300 reboot_device(0);
301 return 0;
302}
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700303
304static int emmc_set_recovery_msg(struct recovery_message *out)
305{
306 char *ptn_name = "misc";
307 unsigned long long ptn = 0;
Mayank Grover9db97242017-06-16 12:41:56 +0530308 unsigned blocksize = mmc_get_device_blocksize();
309 unsigned int size = ROUND_TO_PAGE(sizeof(*out), (unsigned)blocksize - 1);
310 unsigned char *data = NULL;
311 int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700312 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700313
Mayank Grover9db97242017-06-16 12:41:56 +0530314 data = malloc(size);
315 if(!data)
316 {
317 dprintf(CRITICAL,"memory allocation error \n");
318 ret = -1;
319 goto out;
320 }
321
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530322 index = partition_get_index((const char *) ptn_name);
Kinson Chikf1a43512011-07-14 11:28:39 -0700323 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700324 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700325 if(ptn == 0) {
326 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
Mayank Grover9db97242017-06-16 12:41:56 +0530327 ret = -1;
328 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700329 }
Mayank Grover9db97242017-06-16 12:41:56 +0530330 memset(data, 0, size);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700331 memcpy(data, out, sizeof(*out));
332 if (mmc_write(ptn , size, (unsigned int*)data)) {
333 dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out));
Mayank Grover9db97242017-06-16 12:41:56 +0530334 ret = -1;
335 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700336 }
Mayank Grover9db97242017-06-16 12:41:56 +0530337out:
338 if (data)
339 free(data);
340 return ret;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700341}
342
343static int emmc_get_recovery_msg(struct recovery_message *in)
344{
345 char *ptn_name = "misc";
346 unsigned long long ptn = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700347 unsigned int size;
Kinson Chikf1a43512011-07-14 11:28:39 -0700348 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700349
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700350 size = mmc_get_device_blocksize();
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530351 index = partition_get_index((const char *) ptn_name);
Channagoud Kadabif2805632015-01-07 16:46:50 -0800352 if (index < 0)
353 {
354 dprintf(CRITICAL, "%s: Partition not found\n", ptn_name);
355 return -1;
356 }
357
Kinson Chikf1a43512011-07-14 11:28:39 -0700358 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700359 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700360 if(ptn == 0) {
361 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
362 return -1;
363 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700364 if (mmc_read(ptn , (unsigned int*)in, size)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700365 dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size);
366 return -1;
367 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700368
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700369 return 0;
370}
371
372int _emmc_recovery_init(void)
373{
374 int update_status = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700375 struct recovery_message *msg;
376 uint32_t block_size = 0;
377
378 block_size = mmc_get_device_blocksize();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700379
380 // get recovery message
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700381 msg = (struct recovery_message *)memalign(CACHE_LINE, block_size);
382 ASSERT(msg);
383
384 if(emmc_get_recovery_msg(msg))
385 {
386 if(msg)
387 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700388 return -1;
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800389 }
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700390
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700391 msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination
392 if (msg->command[0] != 0 && msg->command[0] != 255) {
393 dprintf(INFO,"Recovery command: %d %s\n",
394 sizeof(msg->command), msg->command);
395 }
396
vijay kumar32e2e3c2014-08-01 21:10:28 +0530397 if (!strcmp(msg->command, "boot-recovery")) {
Stanimir Varbanovbe041992013-04-26 14:29:21 +0300398 boot_into_recovery = 1;
399 }
400
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700401 if (!strcmp("update-radio",msg->command))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700402 {
403 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700404 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700405
406 if(!ret && (update_status & 0x01))
407 {
408 dprintf(INFO,"radio update success\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700409 strlcpy(msg->status, "OKAY", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700410 }
411 else
412 {
413 dprintf(INFO,"radio update failed\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700414 strlcpy(msg->status, "failed-update", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700415 }
Shashank Mittal162244e2011-08-08 19:01:25 -0700416 boot_into_recovery = 1; // Boot in recovery mode
417 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700418 if (!strcmp("reset-device-info",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700419 {
420 reset_device_info();
421 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700422 if (!strcmp("root-detect",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700423 {
424 set_device_root();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700425 }
426 else
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700427 goto out;// do nothing
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700428
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700429 strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command
430 emmc_set_recovery_msg(msg); // send recovery message
431
432out:
433 if(msg)
434 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700435 return 0;
436}
Pavel Nedeva1e62322013-04-05 15:21:36 +0300437
438static int read_misc(unsigned page_offset, void *buf, unsigned size)
439{
440 const char *ptn_name = "misc";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700441 uint32_t pagesize = get_page_size();
Pavel Nedeva1e62322013-04-05 15:21:36 +0300442 unsigned offset;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300443
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700444 if (size == 0 || buf == NULL)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300445 return -1;
446
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700447 offset = page_offset * pagesize;
448
Pavel Nedeva1e62322013-04-05 15:21:36 +0300449 if (target_is_emmc_boot())
450 {
451 int index;
452 unsigned long long ptn;
453 unsigned long long ptn_size;
454
455 index = partition_get_index(ptn_name);
456 if (index == INVALID_PTN)
457 {
458 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
459 return -1;
460 }
461
462 ptn = partition_get_offset(index);
463 ptn_size = partition_get_size(index);
464
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700465 mmc_set_lun(partition_get_lun(index));
466
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700467 if (ptn_size < offset + size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300468 {
469 dprintf(CRITICAL, "Read request out of '%s' boundaries\n",
470 ptn_name);
471 return -1;
472 }
473
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700474 if (mmc_read(ptn + offset, (unsigned int *)buf, size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300475 {
476 dprintf(CRITICAL, "Reading MMC failed\n");
477 return -1;
478 }
479 }
480 else
481 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700482 dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n");
483 return -1;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300484 }
485
Pavel Nedeva1e62322013-04-05 15:21:36 +0300486 return 0;
487}
488
Pavel Nedevc728bb32013-04-05 16:58:05 +0300489int write_misc(unsigned page_offset, void *buf, unsigned size)
490{
491 const char *ptn_name = "misc";
492 void *scratch_addr = target_get_scratch_address();
493 unsigned offset;
494 unsigned aligned_size;
495
496 if (size == 0 || buf == NULL || scratch_addr == NULL)
497 return -1;
498
499 if (target_is_emmc_boot())
500 {
501 int index;
502 unsigned long long ptn;
503 unsigned long long ptn_size;
504
505 index = partition_get_index(ptn_name);
506 if (index == INVALID_PTN)
507 {
508 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
509 return -1;
510 }
511
512 ptn = partition_get_offset(index);
513 ptn_size = partition_get_size(index);
514
515 offset = page_offset * BLOCK_SIZE;
516 aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1);
517 if (ptn_size < offset + aligned_size)
518 {
519 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
520 ptn_name);
521 return -1;
522 }
523
Mayank Grover467bf492017-10-24 12:27:53 +0530524 /* This will ensure, we zeored out any extra bytes
525 we will push to emmc, to prevent information leak */
526 if (aligned_size > size)
527 memset((scratch_addr + size), 0, (aligned_size-size));
528
Pavel Nedevc728bb32013-04-05 16:58:05 +0300529 if (scratch_addr != buf)
530 memcpy(scratch_addr, buf, size);
Channagoud Kadabi2776b652015-08-28 15:41:26 -0700531
532 /* Set Lun for misc partition */
533 mmc_set_lun(partition_get_lun(index));
534
Pavel Nedevc728bb32013-04-05 16:58:05 +0300535 if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr))
536 {
537 dprintf(CRITICAL, "Writing MMC failed\n");
538 return -1;
539 }
540 }
541 else
542 {
543 struct ptentry *ptn;
544 struct ptable *ptable;
545 unsigned pagesize = flash_page_size();
546
547 ptable = flash_get_ptable();
548 if (ptable == NULL)
549 {
550 dprintf(CRITICAL, "Partition table not found\n");
551 return -1;
552 }
553
554 ptn = ptable_find(ptable, ptn_name);
555 if (ptn == NULL)
556 {
557 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
558 return -1;
559 }
560
561 offset = page_offset * pagesize;
562 aligned_size = ROUND_TO_PAGE(size, pagesize - 1);
563 if (ptn->length < offset + aligned_size)
564 {
565 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
566 ptn_name);
567 return -1;
568 }
569
Mayank Grover467bf492017-10-24 12:27:53 +0530570 /* This will ensure, we zeored out any extra bytes
571 we will push, to prevent information leak */
572 if (aligned_size > size)
573 memset((scratch_addr + size), 0, (aligned_size-size));
574
Pavel Nedevc728bb32013-04-05 16:58:05 +0300575 if (scratch_addr != buf)
576 memcpy(scratch_addr, buf, size);
Mayank Grover467bf492017-10-24 12:27:53 +0530577
Pavel Nedevc728bb32013-04-05 16:58:05 +0300578 if (flash_write(ptn, offset, scratch_addr, aligned_size)) {
579 dprintf(CRITICAL, "Writing flash failed\n");
580 return -1;
581 }
582 }
583
584 return 0;
585}
586
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700587int get_ffbm(char *ffbm, unsigned size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300588{
589 const char *ffbm_cmd = "ffbm-";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700590 uint32_t page_size = get_page_size();
591 char *ffbm_page_buffer = NULL;
592 int retval = 0;
593 if (size < FFBM_MODE_BUF_SIZE || size >= page_size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300594 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700595 dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n");
596 retval = -1;
597 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300598 }
Parth Dixit75b16812016-04-05 19:17:17 +0530599 ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700600 if (!ffbm_page_buffer)
601 {
602 dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n");
603 retval = -1;
604 goto cleanup;
605 }
606 if (read_misc(0, ffbm_page_buffer, page_size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300607 {
608 dprintf(CRITICAL, "Error reading MISC partition\n");
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700609 retval = -1;
610 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300611 }
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700612 ffbm_page_buffer[size] = '\0';
613 if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd)))
614 {
615 retval = 0;
616 goto cleanup;
617 }
618 else
619 {
620 if (strlcpy(ffbm, ffbm_page_buffer, size) <
621 FFBM_MODE_BUF_SIZE -1)
622 {
623 dprintf(CRITICAL, "Invalid string in misc partition\n");
624 retval = -1;
625 }
626 else
627 retval = 1;
628 }
629cleanup:
630 if(ffbm_page_buffer)
631 free(ffbm_page_buffer);
632 return retval;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300633}
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700634
635