blob: 7d339e0059a44a0284d1c6342f462cc02eeb2fc4 [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>
Shashank Mittal024c0332010-02-03 11:44:00 -080046
47#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
53#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Shashank Mittal024c0332010-02-03 11:44:00 -080054
55static const int MISC_PAGES = 3; // number of pages to save
56static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
57static char buf[4096];
Pavel Nedeva1e62322013-04-05 15:21:36 +030058
Shashank Mittal024c0332010-02-03 11:44:00 -080059unsigned boot_into_recovery = 0;
60
Deepa Dinamani41fa8d62013-05-23 13:25:36 -070061extern uint32_t get_page_size();
Shashank Mittal162244e2011-08-08 19:01:25 -070062extern void reset_device_info();
63extern void set_device_root();
Shashank Mittal024c0332010-02-03 11:44:00 -080064
65int 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 Griscod6250552011-06-29 14:40:23 -070086 if (flash_read(ptn, offset, (void *) buf, pagesize)) {
Shashank Mittal024c0332010-02-03 11:44:00 -080087 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
88 return -1;
89 }
90 memcpy(out, buf, sizeof(*out));
91 return 0;
92}
93
94int 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 Nedev5d4a7052013-05-13 15:14:45 +0300101 void *scratch_addr = target_get_scratch_address();
Shashank Mittal024c0332010-02-03 11:44:00 -0800102
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 Nedev5d4a7052013-05-13 15:14:45 +0300118 if (flash_read(ptn, offset, scratch_addr, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800119 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
120 return -1;
121 }
122
123 offset += (pagesize * MISC_COMMAND_PAGE);
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300124 offset += (unsigned) scratch_addr;
Greg Griscod6250552011-06-29 14:40:23 -0700125 memcpy((void *) offset, in, sizeof(*in));
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300126 if (flash_write(ptn, 0, scratch_addr, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800127 dprintf(CRITICAL, "ERROR: flash write fail!\n");
128 return -1;
129 }
Greg Griscod6250552011-06-29 14:40:23 -0700130 return 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800131}
132
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700133static int set_ssd_radio_update (char *name)
134{
135 struct ptentry *ptn;
136 struct ptable *ptable;
137 unsigned int ssd_cookie[2] = {0x53534443, 0x4F4F4B49};
138 unsigned pagesize = flash_page_size();
139 unsigned pagemask = pagesize -1;
140 unsigned n = 0;
141
142 ptable = flash_get_ptable();
143 if (ptable == NULL) {
144 dprintf(CRITICAL, "ERROR: Partition table not found\n");
145 return -1;
146 }
147
148 n = (sizeof(ssd_cookie) + pagemask) & (~pagemask);
149
150 ptn = ptable_find(ptable, name);
151 if (ptn == NULL) {
152 dprintf(CRITICAL, "ERROR: No %s partition found\n", name);
153 return -1;
154 }
155
156 if (flash_write(ptn, 0, ssd_cookie, n)) {
157 dprintf(CRITICAL, "ERROR: flash write fail!\n");
158 return -1;
159 }
160
161 dprintf(INFO, "FOTA partition written successfully!");
162 return 0;
163}
164
165int get_boot_info_apps (char type, unsigned int *status)
166{
167 boot_info_for_apps apps_boot_info;
168 int ret = 0;
169
170 ret = smem_read_alloc_entry(SMEM_BOOT_INFO_FOR_APPS,
171 &apps_boot_info, sizeof(apps_boot_info));
172 if (ret)
173 {
174 dprintf(CRITICAL, "ERROR: unable to read shared memory for apps boot info %d\n",ret);
175 return ret;
176 }
177
178 dprintf(INFO,"boot flag %x update status %x\n",apps_boot_info.boot_flags,
179 apps_boot_info.status.update_status);
180
181 if(type == BOOT_FLAGS)
182 *status = apps_boot_info.boot_flags;
183 else if(type == UPDATE_STATUS)
184 *status = apps_boot_info.status.update_status;
185
186 return ret;
187}
188
Shashank Mittal024c0332010-02-03 11:44:00 -0800189/* Bootloader / Recovery Flow
190 *
191 * On every boot, the bootloader will read the recovery_message
192 * from flash and check the command field. The bootloader should
193 * deal with the command field not having a 0 terminator correctly
194 * (so as to not crash if the block is invalid or corrupt).
195 *
196 * The bootloader will have to publish the partition that contains
197 * the recovery_message to the linux kernel so it can update it.
198 *
199 * if command == "boot-recovery" -> boot recovery.img
200 * else if command == "update-radio" -> update radio image (below)
201 * else -> boot boot.img (normal boot)
202 *
203 * Radio Update Flow
204 * 1. the bootloader will attempt to load and validate the header
205 * 2. if the header is invalid, status="invalid-update", goto #8
206 * 3. display the busy image on-screen
207 * 4. if the update image is invalid, status="invalid-radio-image", goto #8
208 * 5. attempt to update the firmware (depending on the command)
209 * 6. if successful, status="okay", goto #8
210 * 7. if failed, and the old image can still boot, status="failed-update"
211 * 8. write the recovery_message, leaving the recovery field
212 * unchanged, updating status, and setting command to
213 * "boot-recovery"
214 * 9. reboot
215 *
216 * The bootloader will not modify or erase the cache partition.
217 * It is recovery's responsibility to clean up the mess afterwards.
218 */
219
220int recovery_init (void)
221{
222 struct recovery_message msg;
Shashank Mittal024c0332010-02-03 11:44:00 -0800223 char partition_name[32];
224 unsigned valid_command = 0;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700225 int update_status = 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800226
227 // get recovery message
Greg Griscod6250552011-06-29 14:40:23 -0700228 if (get_recovery_message(&msg))
Shashank Mittal024c0332010-02-03 11:44:00 -0800229 return -1;
Shashank Mittal024c0332010-02-03 11:44:00 -0800230 msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800231 if (msg.command[0] != 0 && msg.command[0] != 255) {
232 dprintf(INFO,"Recovery command: %d %s\n",
233 sizeof(msg.command), msg.command);
234 }
Shashank Mittal024c0332010-02-03 11:44:00 -0800235
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700236 if (!strcmp("boot-recovery",msg.command))
237 {
238 if(!strcmp("RADIO",msg.status))
239 {
240 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700241 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700242
243 if(!ret && (update_status & 0x01))
244 {
245 dprintf(INFO,"radio update success\n");
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700246 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700247 }
248 else
249 {
250 dprintf(INFO,"radio update failed\n");
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700251 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700252 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700253 strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700254 set_recovery_message(&msg); // send recovery message
255 boot_into_recovery = 1; // Boot in recovery mode
256 return 0;
257 }
258
Shashank Mittal024c0332010-02-03 11:44:00 -0800259 valid_command = 1;
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700260 strlcpy(msg.command, "", sizeof(msg.command)); // to safe against multiple reboot into recovery
261 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Shashank Mittal024c0332010-02-03 11:44:00 -0800262 set_recovery_message(&msg); // send recovery message
263 boot_into_recovery = 1; // Boot in recovery mode
264 return 0;
265 }
266
267 if (!strcmp("update-radio",msg.command)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700268 dprintf(INFO,"start radio update\n");
Shashank Mittal024c0332010-02-03 11:44:00 -0800269 valid_command = 1;
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700270 strlcpy(partition_name, "FOTA", sizeof(partition_name));
Shashank Mittal024c0332010-02-03 11:44:00 -0800271 }
272
273 //Todo: Add support for bootloader update too.
274
275 if(!valid_command) {
276 //We need not to do anything
277 return 0; // Boot in normal mode
278 }
279
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700280 if (set_ssd_radio_update(partition_name)) {
281 /* If writing to FOTA partition fails */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700282 strlcpy(msg.command, "", sizeof(msg.command));
283 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700284 goto SEND_RECOVERY_MSG;
285 }
286 else {
287 /* Setting this to check the radio update status */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700288 strlcpy(msg.command, "boot-recovery", sizeof(msg.command));
Ajay Dudanif4caa942011-10-02 08:57:15 -0700289 strlcpy(msg.status, "RADIO", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700290 goto SEND_RECOVERY_MSG;
291 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700292 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Shashank Mittal024c0332010-02-03 11:44:00 -0800293
294SEND_RECOVERY_MSG:
Shashank Mittal024c0332010-02-03 11:44:00 -0800295 set_recovery_message(&msg); // send recovery message
296 boot_into_recovery = 1; // Boot in recovery mode
297 reboot_device(0);
298 return 0;
299}
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700300
301static int emmc_set_recovery_msg(struct recovery_message *out)
302{
303 char *ptn_name = "misc";
304 unsigned long long ptn = 0;
Mayank Grover9db97242017-06-16 12:41:56 +0530305 unsigned blocksize = mmc_get_device_blocksize();
306 unsigned int size = ROUND_TO_PAGE(sizeof(*out), (unsigned)blocksize - 1);
307 unsigned char *data = NULL;
308 int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700309 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700310
Mayank Grover9db97242017-06-16 12:41:56 +0530311 data = malloc(size);
312 if(!data)
313 {
314 dprintf(CRITICAL,"memory allocation error \n");
315 ret = -1;
316 goto out;
317 }
318
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530319 index = partition_get_index((const char *) ptn_name);
Kinson Chikf1a43512011-07-14 11:28:39 -0700320 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700321 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700322 if(ptn == 0) {
323 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
Mayank Grover9db97242017-06-16 12:41:56 +0530324 ret = -1;
325 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700326 }
Mayank Grover9db97242017-06-16 12:41:56 +0530327 memset(data, 0, size);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700328 memcpy(data, out, sizeof(*out));
329 if (mmc_write(ptn , size, (unsigned int*)data)) {
330 dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out));
Mayank Grover9db97242017-06-16 12:41:56 +0530331 ret = -1;
332 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700333 }
Mayank Grover9db97242017-06-16 12:41:56 +0530334out:
335 if (data)
336 free(data);
337 return ret;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700338}
339
340static int emmc_get_recovery_msg(struct recovery_message *in)
341{
342 char *ptn_name = "misc";
343 unsigned long long ptn = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700344 unsigned int size;
Kinson Chikf1a43512011-07-14 11:28:39 -0700345 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700346
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700347 size = mmc_get_device_blocksize();
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530348 index = partition_get_index((const char *) ptn_name);
Channagoud Kadabif2805632015-01-07 16:46:50 -0800349 if (index < 0)
350 {
351 dprintf(CRITICAL, "%s: Partition not found\n", ptn_name);
352 return -1;
353 }
354
Kinson Chikf1a43512011-07-14 11:28:39 -0700355 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700356 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700357 if(ptn == 0) {
358 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
359 return -1;
360 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700361 if (mmc_read(ptn , (unsigned int*)in, size)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700362 dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size);
363 return -1;
364 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700365
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700366 return 0;
367}
368
369int _emmc_recovery_init(void)
370{
371 int update_status = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700372 struct recovery_message *msg;
373 uint32_t block_size = 0;
374
375 block_size = mmc_get_device_blocksize();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700376
377 // get recovery message
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700378 msg = (struct recovery_message *)memalign(CACHE_LINE, block_size);
379 ASSERT(msg);
380
381 if(emmc_get_recovery_msg(msg))
382 {
383 if(msg)
384 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700385 return -1;
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800386 }
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700387
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700388 msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination
389 if (msg->command[0] != 0 && msg->command[0] != 255) {
390 dprintf(INFO,"Recovery command: %d %s\n",
391 sizeof(msg->command), msg->command);
392 }
393
vijay kumar32e2e3c2014-08-01 21:10:28 +0530394 if (!strcmp(msg->command, "boot-recovery")) {
Stanimir Varbanovbe041992013-04-26 14:29:21 +0300395 boot_into_recovery = 1;
396 }
397
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700398 if (!strcmp("update-radio",msg->command))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700399 {
400 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700401 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700402
403 if(!ret && (update_status & 0x01))
404 {
405 dprintf(INFO,"radio update success\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700406 strlcpy(msg->status, "OKAY", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700407 }
408 else
409 {
410 dprintf(INFO,"radio update failed\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700411 strlcpy(msg->status, "failed-update", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700412 }
Shashank Mittal162244e2011-08-08 19:01:25 -0700413 boot_into_recovery = 1; // Boot in recovery mode
414 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700415 if (!strcmp("reset-device-info",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700416 {
417 reset_device_info();
418 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700419 if (!strcmp("root-detect",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700420 {
421 set_device_root();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700422 }
423 else
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700424 goto out;// do nothing
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700425
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700426 strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command
427 emmc_set_recovery_msg(msg); // send recovery message
428
429out:
430 if(msg)
431 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700432 return 0;
433}
Pavel Nedeva1e62322013-04-05 15:21:36 +0300434
435static int read_misc(unsigned page_offset, void *buf, unsigned size)
436{
437 const char *ptn_name = "misc";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700438 uint32_t pagesize = get_page_size();
Pavel Nedeva1e62322013-04-05 15:21:36 +0300439 unsigned offset;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300440
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700441 if (size == 0 || buf == NULL)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300442 return -1;
443
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700444 offset = page_offset * pagesize;
445
Pavel Nedeva1e62322013-04-05 15:21:36 +0300446 if (target_is_emmc_boot())
447 {
448 int index;
449 unsigned long long ptn;
450 unsigned long long ptn_size;
451
452 index = partition_get_index(ptn_name);
453 if (index == INVALID_PTN)
454 {
455 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
456 return -1;
457 }
458
459 ptn = partition_get_offset(index);
460 ptn_size = partition_get_size(index);
461
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700462 mmc_set_lun(partition_get_lun(index));
463
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700464 if (ptn_size < offset + size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300465 {
466 dprintf(CRITICAL, "Read request out of '%s' boundaries\n",
467 ptn_name);
468 return -1;
469 }
470
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700471 if (mmc_read(ptn + offset, (unsigned int *)buf, size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300472 {
473 dprintf(CRITICAL, "Reading MMC failed\n");
474 return -1;
475 }
476 }
477 else
478 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700479 dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n");
480 return -1;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300481 }
482
Pavel Nedeva1e62322013-04-05 15:21:36 +0300483 return 0;
484}
485
Pavel Nedevc728bb32013-04-05 16:58:05 +0300486int write_misc(unsigned page_offset, void *buf, unsigned size)
487{
488 const char *ptn_name = "misc";
489 void *scratch_addr = target_get_scratch_address();
490 unsigned offset;
491 unsigned aligned_size;
492
493 if (size == 0 || buf == NULL || scratch_addr == NULL)
494 return -1;
495
496 if (target_is_emmc_boot())
497 {
498 int index;
499 unsigned long long ptn;
500 unsigned long long ptn_size;
501
502 index = partition_get_index(ptn_name);
503 if (index == INVALID_PTN)
504 {
505 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
506 return -1;
507 }
508
509 ptn = partition_get_offset(index);
510 ptn_size = partition_get_size(index);
511
512 offset = page_offset * BLOCK_SIZE;
513 aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1);
514 if (ptn_size < offset + aligned_size)
515 {
516 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
517 ptn_name);
518 return -1;
519 }
520
521 if (scratch_addr != buf)
522 memcpy(scratch_addr, buf, size);
Channagoud Kadabi2776b652015-08-28 15:41:26 -0700523
524 /* Set Lun for misc partition */
525 mmc_set_lun(partition_get_lun(index));
526
Pavel Nedevc728bb32013-04-05 16:58:05 +0300527 if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr))
528 {
529 dprintf(CRITICAL, "Writing MMC failed\n");
530 return -1;
531 }
532 }
533 else
534 {
535 struct ptentry *ptn;
536 struct ptable *ptable;
537 unsigned pagesize = flash_page_size();
538
539 ptable = flash_get_ptable();
540 if (ptable == NULL)
541 {
542 dprintf(CRITICAL, "Partition table not found\n");
543 return -1;
544 }
545
546 ptn = ptable_find(ptable, ptn_name);
547 if (ptn == NULL)
548 {
549 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
550 return -1;
551 }
552
553 offset = page_offset * pagesize;
554 aligned_size = ROUND_TO_PAGE(size, pagesize - 1);
555 if (ptn->length < offset + aligned_size)
556 {
557 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
558 ptn_name);
559 return -1;
560 }
561
562 if (scratch_addr != buf)
563 memcpy(scratch_addr, buf, size);
564 if (flash_write(ptn, offset, scratch_addr, aligned_size)) {
565 dprintf(CRITICAL, "Writing flash failed\n");
566 return -1;
567 }
568 }
569
570 return 0;
571}
572
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700573int get_ffbm(char *ffbm, unsigned size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300574{
575 const char *ffbm_cmd = "ffbm-";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700576 uint32_t page_size = get_page_size();
577 char *ffbm_page_buffer = NULL;
578 int retval = 0;
579 if (size < FFBM_MODE_BUF_SIZE || size >= page_size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300580 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700581 dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n");
582 retval = -1;
583 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300584 }
Parth Dixit75b16812016-04-05 19:17:17 +0530585 ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700586 if (!ffbm_page_buffer)
587 {
588 dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n");
589 retval = -1;
590 goto cleanup;
591 }
592 if (read_misc(0, ffbm_page_buffer, page_size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300593 {
594 dprintf(CRITICAL, "Error reading MISC partition\n");
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700595 retval = -1;
596 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300597 }
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700598 ffbm_page_buffer[size] = '\0';
599 if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd)))
600 {
601 retval = 0;
602 goto cleanup;
603 }
604 else
605 {
606 if (strlcpy(ffbm, ffbm_page_buffer, size) <
607 FFBM_MODE_BUF_SIZE -1)
608 {
609 dprintf(CRITICAL, "Invalid string in misc partition\n");
610 retval = -1;
611 }
612 else
613 retval = 1;
614 }
615cleanup:
616 if(ffbm_page_buffer)
617 free(ffbm_page_buffer);
618 return retval;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300619}
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700620
621