1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2007-2010 Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
5 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
6 * Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.com> *
7 * Copyright (C) 2017-2018 Tomas Vanek <vanekt@fbl.cz> *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
21 ***************************************************************************/
26 #include <flash/common.h>
27 #include <flash/nor/core.h>
28 #include <flash/nor/imp.h>
29 #include <target/image.h>
33 * Upper level of NOR flash framework.
34 * The lower level interfaces are to drivers. These upper level ones
35 * primarily support access from Tcl scripts or from GDB.
38 static struct flash_bank *flash_banks;
40 int flash_driver_erase(struct flash_bank *bank, int first, int last)
44 retval = bank->driver->erase(bank, first, last);
45 if (retval != ERROR_OK)
46 LOG_ERROR("failed erasing sectors %d to %d", first, last);
51 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
56 if (bank->num_prot_blocks)
57 num_blocks = bank->num_prot_blocks;
59 num_blocks = bank->num_sectors;
62 /* callers may not supply illegal parameters ... */
63 if (first < 0 || first > last || last >= num_blocks) {
64 LOG_ERROR("illegal protection block range");
68 /* force "set" to 0/1 */
71 if (bank->driver->protect == NULL) {
72 LOG_ERROR("Flash protection is not supported.");
73 return ERROR_FLASH_OPER_UNSUPPORTED;
78 * We must not use any cached information about protection state!!!!
80 * There are a million things that could change the protect state:
82 * the target could have reset, power cycled, been hot plugged,
83 * the application could have run, etc.
85 * Drivers only receive valid protection block range.
87 retval = bank->driver->protect(bank, set, first, last);
88 if (retval != ERROR_OK)
89 LOG_ERROR("failed setting protection for blocks %d to %d", first, last);
94 int flash_driver_write(struct flash_bank *bank,
95 uint8_t *buffer, uint32_t offset, uint32_t count)
99 retval = bank->driver->write(bank, buffer, offset, count);
100 if (retval != ERROR_OK) {
102 "error writing to flash at address " TARGET_ADDR_FMT
103 " at offset 0x%8.8" PRIx32,
111 int flash_driver_read(struct flash_bank *bank,
112 uint8_t *buffer, uint32_t offset, uint32_t count)
116 LOG_DEBUG("call flash_driver_read()");
118 retval = bank->driver->read(bank, buffer, offset, count);
119 if (retval != ERROR_OK) {
121 "error reading to flash at address " TARGET_ADDR_FMT
122 " at offset 0x%8.8" PRIx32,
130 int default_flash_read(struct flash_bank *bank,
131 uint8_t *buffer, uint32_t offset, uint32_t count)
133 return target_read_buffer(bank->target, offset + bank->base, count, buffer);
136 void flash_bank_add(struct flash_bank *bank)
138 /* put flash bank in linked list */
139 unsigned bank_num = 0;
141 /* find last flash bank */
142 struct flash_bank *p = flash_banks;
143 while (NULL != p->next) {
152 bank->bank_number = bank_num;
155 struct flash_bank *flash_bank_list(void)
160 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
162 struct flash_bank *p;
165 for (p = flash_banks; p; p = p->next) {
169 LOG_ERROR("flash bank %d does not exist", num);
173 int flash_get_bank_count(void)
175 struct flash_bank *p;
177 for (p = flash_banks; p; p = p->next)
182 void default_flash_free_driver_priv(struct flash_bank *bank)
184 free(bank->driver_priv);
185 bank->driver_priv = NULL;
188 void flash_free_all_banks(void)
190 struct flash_bank *bank = flash_banks;
192 struct flash_bank *next = bank->next;
193 if (bank->driver->free_driver_priv)
194 bank->driver->free_driver_priv(bank);
196 LOG_WARNING("Flash driver of %s does not support free_driver_priv()", bank->name);
198 /* For 'virtual' flash driver bank->sectors and bank->prot_blocks pointers are copied from
199 * master flash_bank structure. They point to memory locations allocated by master flash driver
200 * so master driver is responsible for releasing them.
201 * Avoid UB caused by double-free memory corruption if flash bank is 'virtual'. */
203 if (strcmp(bank->driver->name, "virtual") != 0) {
205 free(bank->prot_blocks);
215 struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
217 unsigned requested = get_flash_name_index(name);
220 struct flash_bank *bank;
221 for (bank = flash_banks; NULL != bank; bank = bank->next) {
222 if (strcmp(bank->name, name) == 0)
224 if (!flash_driver_name_matches(bank->driver->name, name))
226 if (++found < requested)
233 int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
235 struct flash_bank *bank;
238 bank = get_flash_bank_by_name_noprobe(name);
240 retval = bank->driver->auto_probe(bank);
242 if (retval != ERROR_OK) {
243 LOG_ERROR("auto_probe failed");
252 int get_flash_bank_by_num(int num, struct flash_bank **bank)
254 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
260 retval = p->driver->auto_probe(p);
262 if (retval != ERROR_OK) {
263 LOG_ERROR("auto_probe failed");
270 /* lookup flash bank by address, bank not found is success, but
271 * result_bank is set to NULL. */
272 int get_flash_bank_by_addr(struct target *target,
275 struct flash_bank **result_bank)
277 struct flash_bank *c;
279 /* cycle through bank list */
280 for (c = flash_banks; c; c = c->next) {
281 if (c->target != target)
285 retval = c->driver->auto_probe(c);
287 if (retval != ERROR_OK) {
288 LOG_ERROR("auto_probe failed");
291 /* check whether address belongs to this flash bank */
292 if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
299 LOG_ERROR("No flash at address " TARGET_ADDR_FMT, addr);
305 static int default_flash_mem_blank_check(struct flash_bank *bank)
307 struct target *target = bank->target;
308 const int buffer_size = 1024;
311 int retval = ERROR_OK;
313 if (bank->target->state != TARGET_HALTED) {
314 LOG_ERROR("Target not halted");
315 return ERROR_TARGET_NOT_HALTED;
318 uint8_t *buffer = malloc(buffer_size);
320 for (i = 0; i < bank->num_sectors; i++) {
322 bank->sectors[i].is_erased = 1;
324 for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
327 if (chunk > (bank->sectors[i].size - j))
328 chunk = (bank->sectors[i].size - j);
330 retval = target_read_memory(target,
331 bank->base + bank->sectors[i].offset + j,
335 if (retval != ERROR_OK)
338 for (nBytes = 0; nBytes < chunk; nBytes++) {
339 if (buffer[nBytes] != bank->erased_value) {
340 bank->sectors[i].is_erased = 0;
353 int default_flash_blank_check(struct flash_bank *bank)
355 struct target *target = bank->target;
359 if (bank->target->state != TARGET_HALTED) {
360 LOG_ERROR("Target not halted");
361 return ERROR_TARGET_NOT_HALTED;
364 struct target_memory_check_block *block_array;
365 block_array = malloc(bank->num_sectors * sizeof(struct target_memory_check_block));
366 if (block_array == NULL)
367 return default_flash_mem_blank_check(bank);
369 for (i = 0; i < bank->num_sectors; i++) {
370 block_array[i].address = bank->base + bank->sectors[i].offset;
371 block_array[i].size = bank->sectors[i].size;
372 block_array[i].result = UINT32_MAX; /* erase state unknown */
375 bool fast_check = true;
376 for (i = 0; i < bank->num_sectors; ) {
377 retval = target_blank_check_memory(target,
378 block_array + i, bank->num_sectors - i,
381 /* Run slow fallback if the first run gives no result
382 * otherwise use possibly incomplete results */
387 i += retval; /* add number of blocks done this round */
391 for (i = 0; i < bank->num_sectors; i++)
392 bank->sectors[i].is_erased = block_array[i].result;
395 LOG_USER("Running slow fallback erase check - add working memory");
396 retval = default_flash_mem_blank_check(bank);
403 /* Manipulate given flash region, selecting the bank according to target
404 * and address. Maps an address range to a set of sectors, and issues
405 * the callback() on that set ... e.g. to erase or unprotect its members.
407 * Parameter iterate_protect_blocks switches iteration of protect block
408 * instead of erase sectors. If there is no protect blocks array, sectors
409 * are used in iteration, so compatibility for old flash drivers is retained.
411 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
412 * range must fit those sectors exactly. This is clearly safe; it can't
413 * erase data which the caller said to leave alone, for example. If it's
414 * non-NULL, rather than failing, extra data in the first and/or last
415 * sectors will be added to the range, and that reason string is used when
416 * warning about those additions.
418 static int flash_iterate_address_range_inner(struct target *target,
419 char *pad_reason, target_addr_t addr, uint32_t length,
420 bool iterate_protect_blocks,
421 int (*callback)(struct flash_bank *bank, int first, int last))
423 struct flash_bank *c;
424 struct flash_sector *block_array;
425 target_addr_t last_addr = addr + length; /* first address AFTER end */
431 int retval = get_flash_bank_by_addr(target, addr, true, &c);
432 if (retval != ERROR_OK)
435 if (c->size == 0 || c->num_sectors == 0) {
436 LOG_ERROR("Bank is invalid");
437 return ERROR_FLASH_BANK_INVALID;
441 /* special case, erase whole bank when length is zero */
442 if (addr != c->base) {
443 LOG_ERROR("Whole bank access must start at beginning of bank.");
444 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
447 return callback(c, 0, c->num_sectors - 1);
450 /* check whether it all fits in this bank */
451 if (addr + length - 1 > c->base + c->size - 1) {
452 LOG_ERROR("Flash access does not fit into bank.");
453 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
456 if (c->prot_blocks == NULL || c->num_prot_blocks == 0) {
457 /* flash driver does not define protect blocks, use sectors instead */
458 iterate_protect_blocks = false;
461 if (iterate_protect_blocks) {
462 block_array = c->prot_blocks;
463 num_blocks = c->num_prot_blocks;
465 block_array = c->sectors;
466 num_blocks = c->num_sectors;
470 last_addr -= c->base;
472 for (i = 0; i < num_blocks; i++) {
473 struct flash_sector *f = &block_array[i];
474 uint32_t end = f->offset + f->size;
476 /* start only on a sector boundary */
478 /* scanned past the first sector? */
479 if (addr < f->offset)
482 /* is this the first sector? */
483 if (addr == f->offset)
486 /* Does this need head-padding? If so, pad and warn;
487 * or else force an error.
489 * Such padding can make trouble, since *WE* can't
490 * ever know if that data was in use. The warning
491 * should help users sort out messes later.
493 else if (addr < end && pad_reason) {
494 /* FIXME say how many bytes (e.g. 80 KB) */
495 LOG_WARNING("Adding extra %s range, "
496 "%#8.8x to " TARGET_ADDR_FMT,
498 (unsigned) f->offset,
505 /* is this (also?) the last sector? */
506 if (last_addr == end) {
511 /* Does this need tail-padding? If so, pad and warn;
512 * or else force an error.
514 if (last_addr < end && pad_reason) {
515 /* FIXME say how many bytes (e.g. 80 KB) */
516 LOG_WARNING("Adding extra %s range, "
519 (unsigned) last_addr,
525 /* MUST finish on a sector boundary */
526 if (last_addr <= f->offset)
530 /* invalid start or end address? */
531 if (first == -1 || last == -1) {
532 LOG_ERROR("address range " TARGET_ADDR_FMT " .. " TARGET_ADDR_FMT
533 " is not sector-aligned",
535 c->base + last_addr - 1);
536 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
539 /* The NOR driver may trim this range down, based on what
540 * sectors are already erased/unprotected. GDB currently
541 * blocks such optimizations.
543 return callback(c, first, last);
546 /* The inner fn only handles a single bank, we could be spanning
549 static int flash_iterate_address_range(struct target *target,
550 char *pad_reason, target_addr_t addr, uint32_t length,
551 bool iterate_protect_blocks,
552 int (*callback)(struct flash_bank *bank, int first, int last))
554 struct flash_bank *c;
555 int retval = ERROR_OK;
557 /* Danger! zero-length iterations means entire bank! */
559 retval = get_flash_bank_by_addr(target, addr, true, &c);
560 if (retval != ERROR_OK)
563 uint32_t cur_length = length;
564 /* check whether it all fits in this bank */
565 if (addr + length - 1 > c->base + c->size - 1) {
566 LOG_DEBUG("iterating over more than one flash bank.");
567 cur_length = c->base + c->size - addr;
569 retval = flash_iterate_address_range_inner(target,
570 pad_reason, addr, cur_length,
571 iterate_protect_blocks,
573 if (retval != ERROR_OK)
576 length -= cur_length;
578 } while (length > 0);
583 int flash_erase_address_range(struct target *target,
584 bool pad, target_addr_t addr, uint32_t length)
586 return flash_iterate_address_range(target, pad ? "erase" : NULL,
587 addr, length, false, &flash_driver_erase);
590 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
592 return flash_driver_protect(bank, 0, first, last);
595 int flash_unlock_address_range(struct target *target, target_addr_t addr,
598 /* By default, pad to sector boundaries ... the real issue here
599 * is that our (only) caller *permanently* removes protection,
600 * and doesn't restore it.
602 return flash_iterate_address_range(target, "unprotect",
603 addr, length, true, &flash_driver_unprotect);
606 static int compare_section(const void *a, const void *b)
608 struct imagesection *b1, *b2;
609 b1 = *((struct imagesection **)a);
610 b2 = *((struct imagesection **)b);
612 if (b1->base_address == b2->base_address)
614 else if (b1->base_address > b2->base_address)
621 * Get aligned start address of a flash write region
623 target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr)
625 if (addr < bank->base || addr >= bank->base + bank->size
626 || bank->write_start_alignment <= 1)
629 if (bank->write_start_alignment == FLASH_WRITE_ALIGN_SECTOR) {
630 uint32_t offset = addr - bank->base;
631 uint32_t aligned = 0;
633 for (sect = 0; sect < bank->num_sectors; sect++) {
634 if (bank->sectors[sect].offset > offset)
637 aligned = bank->sectors[sect].offset;
639 return bank->base + aligned;
642 return addr & ~(bank->write_start_alignment - 1);
646 * Get aligned end address of a flash write region
648 target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr)
650 if (addr < bank->base || addr >= bank->base + bank->size
651 || bank->write_end_alignment <= 1)
654 if (bank->write_end_alignment == FLASH_WRITE_ALIGN_SECTOR) {
655 uint32_t offset = addr - bank->base;
656 uint32_t aligned = 0;
658 for (sect = 0; sect < bank->num_sectors; sect++) {
659 aligned = bank->sectors[sect].offset + bank->sectors[sect].size - 1;
660 if (aligned >= offset)
663 return bank->base + aligned;
666 return addr | (bank->write_end_alignment - 1);
670 * Check if gap between sections is bigger than minimum required to discontinue flash write
672 static bool flash_write_check_gap(struct flash_bank *bank,
673 target_addr_t addr1, target_addr_t addr2)
675 if (bank->minimal_write_gap == FLASH_WRITE_CONTINUOUS
676 || addr1 < bank->base || addr1 >= bank->base + bank->size
677 || addr2 < bank->base || addr2 >= bank->base + bank->size)
680 if (bank->minimal_write_gap == FLASH_WRITE_GAP_SECTOR) {
682 uint32_t offset1 = addr1 - bank->base;
683 /* find the sector following the one containing addr1 */
684 for (sect = 0; sect < bank->num_sectors; sect++) {
685 if (bank->sectors[sect].offset > offset1)
688 if (sect >= bank->num_sectors)
691 uint32_t offset2 = addr2 - bank->base;
692 return bank->sectors[sect].offset + bank->sectors[sect].size <= offset2;
695 target_addr_t aligned1 = flash_write_align_end(bank, addr1);
696 target_addr_t aligned2 = flash_write_align_start(bank, addr2);
697 return aligned1 + bank->minimal_write_gap < aligned2;
701 int flash_write_unlock(struct target *target, struct image *image,
702 uint32_t *written, int erase, bool unlock)
704 int retval = ERROR_OK;
707 uint32_t section_offset;
708 struct flash_bank *c;
718 /* assume all sectors need erasing - stops any problems
719 * when flash_write is called multiple times */
724 /* allocate padding array */
725 padding = calloc(image->num_sections, sizeof(*padding));
727 /* This fn requires all sections to be in ascending order of addresses,
728 * whereas an image can have sections out of order. */
729 struct imagesection **sections = malloc(sizeof(struct imagesection *) *
730 image->num_sections);
732 for (i = 0; i < image->num_sections; i++)
733 sections[i] = &image->sections[i];
735 qsort(sections, image->num_sections, sizeof(struct imagesection *),
738 /* loop until we reach end of the image */
739 while (section < image->num_sections) {
743 target_addr_t run_address = sections[section]->base_address + section_offset;
744 uint32_t run_size = sections[section]->size - section_offset;
747 if (sections[section]->size == 0) {
748 LOG_WARNING("empty section %d", section);
754 /* find the corresponding flash bank */
755 retval = get_flash_bank_by_addr(target, run_address, false, &c);
756 if (retval != ERROR_OK)
759 LOG_WARNING("no flash bank found for address " TARGET_ADDR_FMT, run_address);
760 section++; /* and skip it */
765 /* collect consecutive sections which fall into the same bank */
766 section_last = section;
767 padding[section] = 0;
768 while ((run_address + run_size - 1 < c->base + c->size - 1) &&
769 (section_last + 1 < image->num_sections)) {
770 /* sections are sorted */
771 assert(sections[section_last + 1]->base_address >= c->base);
772 if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
773 /* Done with this bank */
777 /* if we have multiple sections within our image,
778 * flash programming could fail due to alignment issues
779 * attempt to rebuild a consecutive buffer for the flash loader */
780 target_addr_t run_next_addr = run_address + run_size;
781 target_addr_t next_section_base = sections[section_last + 1]->base_address;
782 if (next_section_base < run_next_addr) {
783 LOG_ERROR("Section at " TARGET_ADDR_FMT
784 " overlaps section ending at " TARGET_ADDR_FMT,
785 next_section_base, run_next_addr);
786 LOG_ERROR("Flash write aborted.");
791 pad_bytes = next_section_base - run_next_addr;
793 if (flash_write_check_gap(c, run_next_addr - 1, next_section_base)) {
794 LOG_INFO("Flash write discontinued at " TARGET_ADDR_FMT
795 ", next section at " TARGET_ADDR_FMT,
796 run_next_addr, next_section_base);
801 LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
803 section_last, run_next_addr, pad_bytes);
805 padding[section_last] = pad_bytes;
806 run_size += pad_bytes;
807 run_size += sections[++section_last]->size;
810 if (run_address + run_size - 1 > c->base + c->size - 1) {
811 /* If we have more than one flash chip back to back, then we limit
812 * the current write operation to the current chip.
814 LOG_DEBUG("Truncate flash run size to the current flash chip.");
816 run_size = c->base + c->size - run_address;
817 assert(run_size > 0);
820 uint32_t padding_at_start = 0;
821 if (c->write_start_alignment || c->write_end_alignment) {
822 /* align write region according to bank requirements */
823 target_addr_t aligned_start = flash_write_align_start(c, run_address);
824 padding_at_start = run_address - aligned_start;
825 if (padding_at_start > 0) {
826 LOG_WARNING("Section start address " TARGET_ADDR_FMT
827 " breaks the required alignment of flash bank %s",
828 run_address, c->name);
829 LOG_WARNING("Padding %d bytes from " TARGET_ADDR_FMT,
830 padding_at_start, aligned_start);
832 run_address -= padding_at_start;
833 run_size += padding_at_start;
836 target_addr_t run_end = run_address + run_size - 1;
837 target_addr_t aligned_end = flash_write_align_end(c, run_end);
838 pad_bytes = aligned_end - run_end;
840 LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
841 " with %d bytes (bank write end alignment)",
842 section_last, run_end + 1, pad_bytes);
844 padding[section_last] += pad_bytes;
845 run_size += pad_bytes;
848 } else if (unlock || erase) {
849 /* If we're applying any sector automagic, then pad this
850 * (maybe-combined) segment to the end of its last sector.
853 uint32_t offset_start = run_address - c->base;
854 uint32_t offset_end = offset_start + run_size;
855 uint32_t end = offset_end, delta;
857 for (sector = 0; sector < c->num_sectors; sector++) {
858 end = c->sectors[sector].offset
859 + c->sectors[sector].size;
860 if (offset_end <= end)
864 delta = end - offset_end;
865 padding[section_last] += delta;
869 /* allocate buffer */
870 buffer = malloc(run_size);
871 if (buffer == NULL) {
872 LOG_ERROR("Out of memory for flash bank buffer");
877 if (padding_at_start)
878 memset(buffer, c->default_padded_value, padding_at_start);
880 buffer_idx = padding_at_start;
882 /* read sections to the buffer */
883 while (buffer_idx < run_size) {
886 size_read = run_size - buffer_idx;
887 if (size_read > sections[section]->size - section_offset)
888 size_read = sections[section]->size - section_offset;
892 * #¤%#"%¤% we have to figure out the section # from the sorted
893 * list of pointers to sections to invoke image_read_section()...
895 intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
896 int t_section_num = diff / sizeof(struct imagesection);
898 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
899 "section_offset = %"PRIu32", buffer_idx = %"PRIu32", size_read = %zu",
900 section, t_section_num, section_offset,
901 buffer_idx, size_read);
902 retval = image_read_section(image, t_section_num, section_offset,
903 size_read, buffer + buffer_idx, &size_read);
904 if (retval != ERROR_OK || size_read == 0) {
909 buffer_idx += size_read;
910 section_offset += size_read;
912 /* see if we need to pad the section */
913 if (padding[section]) {
914 memset(buffer + buffer_idx, c->default_padded_value, padding[section]);
915 buffer_idx += padding[section];
918 if (section_offset >= sections[section]->size) {
927 retval = flash_unlock_address_range(target, run_address, run_size);
928 if (retval == ERROR_OK) {
930 /* calculate and erase sectors */
931 retval = flash_erase_address_range(target,
932 true, run_address, run_size);
936 if (retval == ERROR_OK) {
937 /* write flash sectors */
938 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
943 if (retval != ERROR_OK) {
944 /* abort operation */
949 *written += run_size; /* add run size to total written counter */
959 int flash_write(struct target *target, struct image *image,
960 uint32_t *written, int erase)
962 return flash_write_unlock(target, image, written, erase, false);
965 struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size, int num_blocks)
969 struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
973 for (i = 0; i < num_blocks; i++) {
974 array[i].offset = offset;
975 array[i].size = size;
976 array[i].is_erased = -1;
977 array[i].is_protected = -1;