summaryrefslogtreecommitdiffstats
path: root/env/boot_magic.c
blob: 06f8e4413d30833a37ea5ec9d6591a3b16459167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2000-2010
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * (C) Copyright 2019 Broadcom Corporation
 * Joel Peshkin, Broadcom Corporation, joel.peshkin@broadcom.com
 */

#define DEBUG

#include <common.h>
#include <command.h>
#include <environment.h>
#include <hexdump.h>
#include <linux/stddef.h>
#include <nand.h>
#include <mmc.h>
#include <nand.h>
#include <search.h>
#include <errno.h>
#include <bca_sdk.h>

DECLARE_GLOBAL_DATA_PTR;

__weak uint32_t env_boot_magic_search_size(void)
{
	return 2*1024*1024;
}

__weak int env_override_import(void *ep)
{
	return 0;
}

#ifdef CONFIG_SPI_FLASH
static int env_dev_read_spinor( int addr, size_t * len, char * buffer )
{
	int ret= -1;
	struct mtd_info *mtd;
	size_t retlen;	

	mtd = get_mtd_device_nm(LOADER_PART);
	if (IS_ERR_OR_NULL(mtd)){
		debug("%s:MTD device %s not found, ret %ld\n",__func__, LOADER_PART,
		   PTR_ERR(mtd));
		return ret;
	}
	ret = mtd_read(mtd,addr,*len,&retlen,buffer);
	put_mtd_device(mtd);
	return ret;
}
static int env_dev_write_spinor( int addr, size_t * len, char * buffer )
{
	int ret= -1;
	struct mtd_info *mtd;
	struct erase_info ei;
	size_t retlen;	
	u_char * tempbuff = NULL;
	size_t erase_bytes;
	u32 start_offset;
	u32 start_sector;
	u32 end_sector;
	
	mtd = get_mtd_device_nm(LOADER_PART);
	if (IS_ERR_OR_NULL(mtd)){
		debug("%s:MTD device %s not found, ret %ld\n",__func__, LOADER_PART,
	   		PTR_ERR(mtd));
		return ret;
	}

	start_sector = addr/mtd->erasesize;
	end_sector = ( addr + *len + mtd->erasesize - 1)/mtd->erasesize;
	start_offset = start_sector * mtd->erasesize;
	erase_bytes = (end_sector - start_sector)*mtd->erasesize;
	tempbuff = malloc(erase_bytes);
	if (!tempbuff){
		debug("%s malloc %d failed\n",__func__, erase_bytes);
		goto done;
 	}
	
	ret = mtd_read(mtd,start_offset,erase_bytes,&retlen,tempbuff);
	if (ret){
		debug("%s:read MTD device %s fail, ret %d\n",__func__, LOADER_PART,
	   		ret);
		goto done;
	}

	memcpy(tempbuff + addr%mtd->erasesize, buffer, *len);
	memset(&ei, 0, sizeof(ei));
	ei.mtd = mtd;
	ei.addr = start_offset;
	ei.len = erase_bytes;
	ret = mtd_erase(mtd, &ei);
	if (ret){
		debug("%s:erase MTD device %s fail, ret %d\n",__func__, LOADER_PART,
	   		ret);
		goto done;
	}

	ret = mtd_write(mtd, start_offset, erase_bytes, &retlen, tempbuff);
	if (ret){
		debug("%s:write MTD device %s fail, ret %d\n",__func__, LOADER_PART,
	   		ret);
	}

done:	
	free(tempbuff);
	put_mtd_device(mtd);
	return ret;
	
}
#endif
#ifdef CONFIG_NAND	
static int env_dev_read_nand( int addr, size_t * len, char * buffer )
{
	int ret= -1;
	struct mtd_info *mtd;
	mtd = get_nand_dev_by_index(0);
	if (!mtd)
		return ret;
	ret = nand_read(mtd, addr, len, (void *)buffer);
	return ret;
}
static int env_dev_write_nand( int addr, size_t * len, char * buffer )
{
	int blocknum, off;
	size_t blocksize;
	int ret = -1;

	struct mtd_info *mtd;
	char * block = NULL;
	mtd = get_nand_dev_by_index(0);
	if (!mtd)
		return ret;
	
	block = malloc(mtd->erasesize);
	if (!block)
		return ret;

	blocksize = mtd->erasesize;
	blocknum = addr / mtd->erasesize;
	off = addr % mtd->erasesize;
	ret = nand_read(mtd, (off_t) (blocknum * mtd->erasesize),
		      &blocksize, (void *)block);
	if (ret < 0) {
		debug("read error %d on block %d\n", ret, blocknum);
	}
	memcpy(block + off, buffer, *len);
	nand_erase(mtd, blocknum * blocksize, blocksize);
	ret = nand_write(mtd, blocknum * blocksize, &blocksize,
			       (void *)block);
	if (ret < 0) {
		debug("write error %d on block %d\n", ret, blocknum);
	}
	free(block);
	return ret;
}
#endif

#ifdef CONFIG_MMC

#define ENV_DEV_MMC_READ 		0
#define ENV_DEV_MMC_WRITE		1
#define ENV_DEV_MMC_DEV_NUM 		0
#define ENV_DEV_MMC_BOOT_HWPART 	1
#define ENV_DEV_MMC_USERDATA_PART 	0

static int env_dev_read_write_mmc( int addr, size_t * len, char * buffer, int mode)
{
	uint64_t blocksize, start_blocknum, start_offset, num_blocks;
	int ret = -1;
	char * block_data_ptr = NULL;
	struct mmc *mmc = NULL;
	struct blk_desc * block_dev = NULL;

	/* Initialize MMC */
	mmc = find_mmc_device(ENV_DEV_MMC_DEV_NUM);
	if (!mmc)
		return ret;
	mmc_init(mmc);

	/* Point to BOOT partition */
	blk_select_hwpart_devnum(IF_TYPE_MMC, ENV_DEV_MMC_DEV_NUM, ENV_DEV_MMC_BOOT_HWPART);

	/* Get block device */
	block_dev = mmc_get_blk_desc(mmc);
	if (!block_dev)
		goto mmc_err;
	
	/* Calculate block numbers and offsets */
	blocksize = mmc->read_bl_len;
	start_blocknum = addr/blocksize; 
	start_offset = addr %  blocksize;
	num_blocks = (start_offset+*len)/blocksize + ((start_offset+*len)%blocksize?1:0);
	block_data_ptr = memalign(ARCH_DMA_MINALIGN, num_blocks*blocksize);
	if( !block_data_ptr ) {
		printf("%s: ERROR! Cannot allocate memory for blockdata\n", __FUNCTION__);
		goto mmc_err;
	}

	ret = blk_dread(block_dev, start_blocknum, num_blocks, (void *)block_data_ptr);
	if (ret < 0) {
		printf("%s: read error %d on block %llu, num_blocks %llu\n", __FUNCTION__, ret, start_blocknum, num_blocks);
		goto mmc_err;
	}
		
	if( mode == ENV_DEV_MMC_WRITE ) {
		memcpy(block_data_ptr + start_offset, buffer, *len);
		ret = blk_dwrite(block_dev, start_blocknum, num_blocks, (void *)block_data_ptr);

		if (ret < 0) {
			printf("%s: write error %d on block %llu, num_blocks %llu\n", __FUNCTION__, ret, start_blocknum, num_blocks);
			goto mmc_err;
		}
	} else	{
		memcpy(buffer, block_data_ptr + start_offset, *len);
	}	

	ret = 0;
mmc_err:
	if( block_data_ptr )
		free(block_data_ptr);

	/* Point to USERDATA partition */
	blk_select_hwpart_devnum(IF_TYPE_MMC, ENV_DEV_MMC_DEV_NUM, ENV_DEV_MMC_USERDATA_PART);
	return ret;
}
#endif	

static int get_env_flashtype( char ** flashtype )
{
	*flashtype = get_loader_media();
	return 0;
}

static int env_dev_read( int addr, size_t * len, char * buffer )
{
	char * pflashtype_str = NULL;
	if( get_env_flashtype(&pflashtype_str) == 0 ) {
		if( pflashtype_str != NULL ) {
#ifdef CONFIG_NAND	
			if( strcasecmp(pflashtype_str,FLASH_DEV_STR_NAND) == 0 )  {
				return (env_dev_read_nand(addr, len, buffer));
			}
#endif
		
#ifdef CONFIG_MMC		
			if( strcasecmp(pflashtype_str,FLASH_DEV_STR_EMMC) == 0 ) {
				return (env_dev_read_write_mmc(addr, len, buffer, ENV_DEV_MMC_READ));
			}
#endif		
		
#ifdef CONFIG_SPI_FLASH		
			if( strcasecmp(pflashtype_str,FLASH_DEV_STR_SPINOR) == 0 ) {
				return (env_dev_read_spinor(addr, len, buffer));
			}
#endif		

			printf("%s: Error: Unsupported env storage media %s!\n", __FUNCTION__, pflashtype_str);
		} else {
			printf("%s: Error: Environment storage media invalid!\n", __FUNCTION__);
		}
	} else {
		printf("%s: Error: Cannot find environment storage media!\n", __FUNCTION__);
	}
	return -1;
}

static int env_dev_write( int addr, size_t * len, char * buffer )
{
	char * pflashtype_str = NULL;
	if( get_env_flashtype(&pflashtype_str) == 0 )
	{
		if( pflashtype_str != NULL ) {
#ifdef CONFIG_NAND	
			if( strcasecmp(pflashtype_str,FLASH_DEV_STR_NAND) == 0 ) {
				return (env_dev_write_nand(addr, len, buffer));
			}
#endif
			
#ifdef CONFIG_MMC		
			if( strcasecmp(pflashtype_str,FLASH_DEV_STR_EMMC) == 0 ) {
				return (env_dev_read_write_mmc(addr, len, buffer, ENV_DEV_MMC_WRITE));
			}
#endif		

#ifdef CONFIG_SPI_FLASH		
			if( strcasecmp(pflashtype_str,FLASH_DEV_STR_SPINOR) == 0 ) {
				return (env_dev_write_spinor(addr, len, buffer));
			}
#endif		
		}
	}
	printf("%s: Error: Cannot find environment storage media!\n", __FUNCTION__);
	return -1;
}

static int env_boot_magic_load(void)
{

	uint32_t crc, new;
	int ret;
	loff_t off, copies[10];
	int copies_found = 0;
	int found_size;
	size_t rdlen;
	env_t *ep;
	uint32_t magichdr[3];
	char *ebuff = NULL;

	printf("ENV_BOOT_MAGIC_LOAD\n");

	for (off = 0; off < (loff_t)env_boot_magic_search_size(); off += 4096) {
		rdlen = 12;
		env_dev_read( off, &rdlen, (void *)magichdr);
		if (magichdr[0] != BOOT_MAGIC_MAGIC) {
			continue;
		}
		printf("found magic at %lx\n", (long)off);
		rdlen = magichdr[1] + 12;
		if (ebuff == NULL) {
			ebuff = malloc(max((int)rdlen, CONFIG_ENV_SIZE + 12));
		}
		env_dev_read( off, &rdlen, (void *)ebuff);
		ep = (env_t *) & ebuff[8];
		memcpy(&crc, &ep->crc, sizeof(crc));

		new = crc32(0, ep->data, rdlen - 16);
		if (new != crc) {
			debug("CRC mismatch len = %d\n", (int)rdlen - 16);
			debug("computed %x \n", new);
			debug("expected %x \n", crc);
		} else {
			debug("good crc\n");
			gd->env_valid = ENV_VALID;
			/* FIXME -- may need to grow instead of shrink */
			debug("resize from %ld to %d\n", rdlen - 12,
			      CONFIG_ENV_SIZE);
			new = crc32(0, ep->data, CONFIG_ENV_SIZE - 4);
			memcpy(&ep->crc, &new, sizeof(new));
			copies[copies_found++] = off;
			found_size = rdlen - 12;
			if (!env_override_import((void *)ep))
			{
				ret = env_import((void *)ep, 1);
			}
		}
		free(ebuff);
		ebuff = NULL;
		if (NULL != env_get("env_boot_magic")) {
			break;
		}
	}

	if (copies_found > 0) {
		if (NULL == env_get("env_boot_magic")) {
			char found[256];
			int i;
			sprintf(found, "%d@", found_size);
			for (i = 0; i < copies_found; i++) {
				sprintf(found + strlen(found), "0x%llx,",
					copies[i]);
			}
			found[strlen(found) - 1] = '\0';
			env_set("env_boot_magic", found);
		}
		return (ret);
	} else {
		set_default_env("import not done", 0);
		return (-1);
	}

}

static int env_boot_magic_save(void)
{
	env_t *ep;
	char *envbuf = NULL;
	uint32_t *envintp;
	char *c;
	int ret = -2;
	int elen;
	size_t wr_len;
	char *config;
	uint32_t new;
	int i;
	char *found = NULL;

	config = env_get("env_boot_magic");
	if (NULL == config)
		return (-1);
	elen = simple_strtoul(config, NULL, 0);
	found = malloc(strlen(config) + 1);
	if (!found)
		goto err;
	strcpy(found, config);
	envbuf = malloc(max(elen + 12, CONFIG_ENV_SIZE + 12));
	ep = (env_t *) (envbuf + 8);
	envintp = (uint32_t *) envbuf;
	ret = env_export(ep);
	for (i = CONFIG_ENV_SIZE; i < elen; i++) {
		envbuf[12 + i] = 0xff;
	}
	new = crc32(0, ep->data, elen - 4);
	memcpy(&ep->crc, &new, sizeof(new));
	envintp[0] = BOOT_MAGIC_MAGIC;
	envintp[1] = elen;
	strtok(found, "@");
	while ((c = strtok(NULL, ","))) {
		i = simple_strtoul(c, NULL, 0);
		printf("save to %x\n", i);
		wr_len = elen+12;
                ret = env_dev_write( i, &wr_len, envbuf );
	}

err:	free(envbuf);
	free(found);
	return ret;

}

U_BOOT_ENV_LOCATION(boot_magic) = {
	.location = ENVL_BOOT_MAGIC,
	.load = env_boot_magic_load,
	.save = env_save_ptr(env_boot_magic_save), 
	ENV_NAME("BOOT_MAGIC")
};