summaryrefslogtreecommitdiffstats
path: root/erasure_layout.c
blob: 108fea4c0fc278f1f18af0d71df65f1ddb92e3d1 (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
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2022 Aarya Chaumal
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#include "include/flash.h"
#include "include/layout.h"
#include "include/erasure_layout.h"

static size_t calculate_block_count(const struct flashchip *chip, size_t eraser_idx)
{
	size_t block_count = 0;

	chipoff_t addr = 0;
	for (size_t i = 0; addr < chip->total_size * 1024; i++) {
		const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
		block_count += block->count;
		addr += block->size * block->count;
	}

	return block_count;
}

static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
		chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
{
	struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
	edata->start_addr = start_addr;
	edata->end_addr = end_addr;
	edata->selected = false;
	edata->block_num = block_num;

	if (!idx)
		return;

	edata->first_sub_block_index = *sub_block_index;
	struct eraseblock_data *subedata = &layout[idx - 1].layout_list[*sub_block_index];
	while (subedata->start_addr >= start_addr && subedata->end_addr <= end_addr &&
		*sub_block_index < layout[idx-1].block_count) {
		(*sub_block_index)++;
		subedata++;
	}
	edata->last_sub_block_index = *sub_block_index - 1;
}

/*
 * @brief Function to free the created erase_layout
 *
 * @param layout pointer to allocated layout
 * @param erasefn_count number of erase functions for which the layout was created
 *
 */
void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
{
	if (!layout)
		return;
	for (size_t i = 0; i < erasefn_count; i++) {
		free(layout[i].layout_list);
	}
	free(layout);
}

/*
 * @brief Function to create an erase layout
 *
 * @param	flashctx	flash context
 * @param	e_layout	address to the pointer to store the layout
 * @return	0 on success,
 *		-1 if layout creation fails
 *
 * This function creates a layout of which erase functions erase which regions
 * of the flash chip. This helps to optimally select the erase functions for
 * erase/write operations.
 */
int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
{
	const struct flashchip *chip = flashctx->chip;
	const size_t erasefn_count = count_usable_erasers(flashctx);
	if (!erasefn_count) {
		msg_gerr("No erase functions supported\n");
		return 0;
	}

	struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
	if (!layout) {
		msg_gerr("Out of memory!\n");
		return -1;
	}

	size_t layout_idx = 0;
	for (size_t eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
		if (check_block_eraser(flashctx, eraser_idx, 0))
			continue;

		layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
		const size_t block_count = calculate_block_count(flashctx->chip, eraser_idx);
		size_t sub_block_index = 0;

		layout[layout_idx].block_count = block_count;
		layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
									sizeof(struct eraseblock_data));

		if (!layout[layout_idx].layout_list) {
			free_erase_layout(layout, layout_idx);
			return -1;
		}

		size_t block_num = 0;
		chipoff_t start_addr = 0;

		for (int i = 0; block_num < block_count;  i++) {
			const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];

			for (size_t num = 0; num < block->count; num++) {
				chipoff_t end_addr = start_addr + block->size - 1;
				init_eraseblock(layout, layout_idx, block_num,
						start_addr, end_addr, &sub_block_index);
				block_num += 1;
				start_addr = end_addr + 1;
			}
		}
		layout_idx++;
	}

	*e_layout = layout;
	return layout_idx;
}

/*
 * @brief	Function to align start and address of the region boundaries
 *
 * @param	layout	erase layout
 * @param	flashctx	flash context
 * @param	region_start	pointer to start address of the region to align
 * @param	region_end	pointer to end address of the region to align
 *
 * This function aligns start and end address of the region (in struct walk_info)
 * to some erase sector boundaries and modify the region start and end addresses
 * to match nearest erase sector boundaries. This function will be used in the
 * new algorithm for erase function selection.
 */
static void align_region(const struct erase_layout *layout, struct flashctx *const flashctx,
			chipoff_t *region_start, chipoff_t *region_end)
{
	chipoff_t start_diff = UINT_MAX, end_diff = UINT_MAX;
	const size_t erasefn_count = count_usable_erasers(flashctx);
	for (size_t i = 0; i < erasefn_count; i++) {
		for (size_t j = 0; j < layout[i].block_count; j++) {
			const struct eraseblock_data *ll = &layout[i].layout_list[j];
			if (ll->start_addr <= *region_start)
				start_diff = (*region_start - ll->start_addr) > start_diff ?
						start_diff : (*region_start - ll->start_addr);
			if (ll->end_addr >= *region_end)
				end_diff = (ll->end_addr - *region_end) > end_diff ?
						end_diff : (ll->end_addr - *region_end);
		}
	}

	if (start_diff) {
		msg_cinfo("Region start not sector aligned! Extending start boundaries...\n");
		*region_start = *region_start - start_diff;
	}
	if (end_diff) {
		msg_cinfo("Region end not sector aligned! Extending end boundaries...\n");
		*region_end = *region_end + end_diff;
	}
}

/*
 * @brief	Function to select the list of sectors that need erasing
 *
 * @param	flashctx	flash context
 * @param	layout		erase layout
 * @param	findex		index of the erase function
 * @param	block_num	index of the block to erase according to the erase function index
 * @param	curcontents	buffer containg the current contents of the flash
 * @param	newcontents	buffer containg the new contents of the flash
 * @param	rstart		start address of the region
 * @rend	rend		end address of the region
 */
static void select_erase_functions(struct flashctx *flashctx, const struct erase_layout *layout,
				size_t findex, size_t block_num, uint8_t *curcontents, uint8_t *newcontents,
				chipoff_t rstart, chipoff_t rend)
{
	struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
	if (!findex) {
		if (ll->start_addr >= rstart && ll->end_addr <= rend) {
			chipoff_t start_addr = ll->start_addr;
			chipoff_t end_addr = ll->end_addr;
			const chipsize_t erase_len = end_addr - start_addr + 1;
			const uint8_t erased_value = ERASED_VALUE(flashctx);
			ll->selected = need_erase(curcontents + start_addr, newcontents + start_addr, erase_len,
						flashctx->chip->gran, erased_value);
		}
	} else {
		int count = 0;
		const int sub_block_start = ll->first_sub_block_index;
		const int sub_block_end = ll->last_sub_block_index;

		for (int j = sub_block_start; j <= sub_block_end; j++) {
			select_erase_functions(flashctx, layout, findex - 1, j, curcontents, newcontents,
						rstart, rend);
			if (layout[findex - 1].layout_list[j].selected)
				count++;
		}

		const int total_blocks = sub_block_end - sub_block_start + 1;
		if (count && count > total_blocks/2) {
			if (ll->start_addr >= rstart && ll->end_addr <= rend) {
				for (int j = sub_block_start; j <= sub_block_end; j++)
					layout[findex - 1].layout_list[j].selected = false;
				ll->selected = true;
			}
		}
	}
}

/*
 * @brief	wrapper to use the erase algorithm
 *
 * @param	flashctx	flash context
 * @param	region_start	start address of the region
 * @param	region_end	end address of the region
 * @param       curcontents     buffer containg the current contents of the flash
 * @param       newcontents     buffer containg the new contents of the flash
 * @param	erase_layout	erase layout
 * @param	all_skipped	pointer to the flag to chec if any block was erased
 */
int erase_write(struct flashctx *const flashctx, chipoff_t region_start, chipoff_t region_end,
		uint8_t *curcontents, uint8_t *newcontents,
		struct erase_layout *erase_layout, bool *all_skipped)
{
	const size_t erasefn_count = count_usable_erasers(flashctx);
	int ret = 0;
	size_t i;
	chipoff_t old_start = region_start, old_end = region_end;
	align_region(erase_layout, flashctx, &region_start, &region_end);

	uint8_t *old_start_buf = NULL, *old_end_buf = NULL;
	old_start_buf = (uint8_t *)malloc(old_start - region_start);
	if (!old_start_buf) {
		msg_cerr("Not enough memory!\n");
		ret = -1;
		goto _end;
	}
	old_end_buf = (uint8_t *)malloc(region_end - old_end);
	if (!old_end_buf) {
		msg_cerr("Not enough memory!\n");
		ret = -1;
		goto _end;
	}

	if (old_start - region_start) {
		read_flash(flashctx, curcontents + region_start, region_start, old_start - region_start);
		memcpy(old_start_buf, newcontents + region_start, old_start - region_start);
		memcpy(newcontents + region_start, curcontents + region_start, old_start - region_start);
	}
	if (region_end - old_end) {
		read_flash(flashctx, curcontents + old_end, old_end, region_end - old_end);
		memcpy(old_end_buf, newcontents + old_end, region_end - old_end);
		memcpy(newcontents + old_end, curcontents + old_end, region_end - old_end);
	}

	// select erase functions
	for (i = 0; i < erase_layout[erasefn_count - 1].block_count; i++) {
		if (erase_layout[erasefn_count - 1].layout_list[i].start_addr <= region_end &&
			region_start <= erase_layout[erasefn_count - 1].layout_list[i].end_addr)
			select_erase_functions(flashctx, erase_layout,
						erasefn_count - 1, i,
						curcontents, newcontents,
						region_start, region_end);
	}

	for (i = 0; i < erasefn_count; i++) {
		for (size_t j = 0; j < erase_layout[i].block_count; j++) {
			if (!erase_layout[i].layout_list[j].selected)	continue;

			// erase
			chipoff_t start_addr = erase_layout[i].layout_list[j].start_addr;
			unsigned int block_len = erase_layout[i].layout_list[j].end_addr - start_addr + 1;
			const uint8_t erased_value = ERASED_VALUE(flashctx);
			// execute erase
			erasefunc_t *erasefn = lookup_erase_func_ptr(erase_layout[i].eraser);

			if (!flashctx->flags.skip_unwritable_regions) {
				if (check_for_unwritable_regions(flashctx, start_addr, block_len))
					goto _end;
			}

			unsigned int len;
			for (unsigned int addr = start_addr; addr < start_addr + block_len; addr += len) {
				struct flash_region region;
				get_flash_region(flashctx, addr, &region);

				len = min(start_addr + block_len, region.end) - addr;

				if (region.write_prot) {
					msg_gdbg("%s: cannot erase inside %s "
						"region (%#08"PRIx32"..%#08"PRIx32"), skipping range (%#08x..%#08x).\n",
						 __func__, region.name,
						 region.start, region.end - 1,
						 addr, addr + len - 1);
					free(region.name);
					continue;
				}

				msg_gdbg("%s: %s region (%#08"PRIx32"..%#08"PRIx32") is "
					"writable, erasing range (%#08x..%#08x).\n",
					 __func__, region.name,
					 region.start, region.end - 1,
					 addr, addr + len - 1);
				free(region.name);

				if (erasefn(flashctx, addr, len)) {
					ret = -1;
					goto _end;
				}
				if (check_erased_range(flashctx, addr, len)) {
					ret = - 1;
					msg_cerr("ERASE FAILED!\n");
					goto _end;
				}
			}

			ret = erasefn(flashctx, start_addr, block_len);
			if (ret) {
				msg_cerr("Failed to execute erase command "
					"for offset %#"PRIx32" to %#"PRIx32".\n",
					start_addr, start_addr + block_len);
				ret = -1;
				goto _end;
			}

			// adjust curcontents
			memset(curcontents+start_addr, erased_value, block_len);
			// after erase make it unselected again
			erase_layout[i].layout_list[j].selected = false;
			msg_cdbg("E(%"PRIx32":%"PRIx32")", start_addr, start_addr + block_len - 1);
			// verify erase
			ret = check_erased_range(flashctx, start_addr, block_len);
			if (ret) {
				msg_cerr("Verifying flash. Erase failed for range %#"PRIx32" : %#"PRIx32", Abort.\n",
					start_addr, start_addr + block_len - 1);
				goto _end;
			}

			*all_skipped = false;
		}
	}

	// write
	unsigned int start_here = 0, len_here = 0, erase_len = region_end - region_start + 1;
	while ((len_here = get_next_write(curcontents + region_start + start_here,
					newcontents + region_start + start_here,
					erase_len - start_here, &start_here,
					flashctx->chip->gran))) {
		// execute write
		ret = write_flash(flashctx,
				newcontents + region_start + start_here,
				region_start + start_here, len_here);
		if (ret) {
			msg_cerr("Write failed at %#zx, Abort.\n", i);
			ret = -1;
			goto _end;
		}

		// adjust curcontents
		memcpy(curcontents + region_start + start_here,
			newcontents + region_start + start_here, len_here);
		msg_cdbg("W(%"PRIx32":%"PRIx32")", region_start + start_here, region_start + start_here + len_here - 1);

		*all_skipped = false;
	}
	// verify write
	ret = verify_range(flashctx, newcontents + region_start, region_start, region_end - region_start);
	if (ret) {
		msg_cerr("Verifying flash. Write failed for range %#"PRIx32" : %#"PRIx32", Abort.\n",
			region_start, region_end);
		goto _end;
	}

_end:
	memcpy(newcontents + region_start, old_start_buf, old_start - region_start);
	memcpy(newcontents + old_end, old_end_buf, region_end - old_end);

	free(old_start_buf);
	free(old_end_buf);

	msg_cinfo("Erase/write done from %"PRIx32" to %"PRIx32"\n", region_start, region_end);
	return ret;
}