Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 2 | |
| 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. |
| 12 | * * Neither the name of The Linux Foundation. nor the names of its |
| 13 | * 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 <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <dev/flash-ubi.h> |
| 32 | #include <dev/flash.h> |
| 33 | #include <qpic_nand.h> |
| 34 | #include <rand.h> |
| 35 | |
| 36 | static |
| 37 | const uint32_t crc32_table[256] = { |
| 38 | 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, |
| 39 | 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, |
| 40 | 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, |
| 41 | 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, |
| 42 | 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, |
| 43 | 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, |
| 44 | 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, |
| 45 | 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, |
| 46 | 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, |
| 47 | 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, |
| 48 | 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, |
| 49 | 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, |
| 50 | 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L, |
| 51 | 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, |
| 52 | 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, |
| 53 | 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, |
| 54 | 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, |
| 55 | 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, |
| 56 | 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, |
| 57 | 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, |
| 58 | 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, |
| 59 | 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, |
| 60 | 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, |
| 61 | 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, |
| 62 | 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, |
| 63 | 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L, |
| 64 | 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, |
| 65 | 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, |
| 66 | 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L, |
| 67 | 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, |
| 68 | 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, |
| 69 | 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, |
| 70 | 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, |
| 71 | 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, |
| 72 | 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, |
| 73 | 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, |
| 74 | 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL, |
| 75 | 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, |
| 76 | 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, |
| 77 | 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, |
| 78 | 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, |
| 79 | 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, |
| 80 | 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, |
| 81 | 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, |
| 82 | 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, |
| 83 | 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, |
| 84 | 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, |
| 85 | 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, |
| 86 | 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, |
| 87 | 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L, |
| 88 | 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, |
| 89 | 0x2d02ef8dL |
| 90 | }; |
| 91 | |
| 92 | static uint32_t mtd_crc32(uint32_t crc, const void *buf, size_t size) |
| 93 | { |
| 94 | const uint8_t *p = buf; |
| 95 | |
| 96 | while (size--) |
| 97 | crc = crc32_table[(crc ^ *p++) & 0xff] ^ (crc >> 8); |
| 98 | return crc; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * check_pattern - check if buffer contains only a certain byte pattern. |
| 103 | * @buf: buffer to check |
| 104 | * @patt: the pattern to check |
| 105 | * @size: buffer size in bytes |
| 106 | * |
| 107 | * This function returns %1 if there are only @patt bytes in @buf, and %0 if |
| 108 | * something else was also found. |
| 109 | */ |
| 110 | int check_pattern(const void *buf, uint8_t patt, int size) |
| 111 | { |
| 112 | int i; |
| 113 | |
| 114 | for (i = 0; i < size; i++) |
| 115 | if (((const uint8_t *)buf)[i] != patt) |
| 116 | return 0; |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * read_ec_hdr - read and check an erase counter header. |
| 122 | * @peb: number of the physical erase block to read the header for |
| 123 | * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter |
| 124 | * header |
| 125 | * |
| 126 | * This function reads erase counter header from physical eraseblock @peb and |
| 127 | * stores it in @ec_hdr. This function also checks the validity of the read |
| 128 | * header. |
| 129 | * |
| 130 | * Return codes: |
| 131 | * -1 - in case of error |
| 132 | * 0 - if PEB was found valid |
| 133 | * 1 - if PEB is empty |
| 134 | */ |
| 135 | static int read_ec_hdr(uint32_t peb, struct ubi_ec_hdr *ec_hdr) |
| 136 | { |
| 137 | unsigned char *spare, *tmp_buf; |
| 138 | int ret = -1; |
| 139 | uint32_t crc; |
| 140 | int page_size = flash_page_size(); |
| 141 | int num_pages_per_blk = flash_block_size()/page_size; |
| 142 | |
| 143 | spare = (unsigned char *)malloc(flash_spare_size()); |
| 144 | if (!spare) |
| 145 | { |
| 146 | dprintf(CRITICAL, "read_ec_hdr: Mem allocation failed\n"); |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | tmp_buf = (unsigned char *)malloc(page_size); |
| 151 | if (!tmp_buf) |
| 152 | { |
| 153 | dprintf(CRITICAL, "read_ec_hdr: Mem allocation failed\n"); |
| 154 | goto out_tmp_buf; |
| 155 | } |
| 156 | |
| 157 | if (qpic_nand_block_isbad(peb * num_pages_per_blk)) { |
| 158 | dprintf(CRITICAL, "read_ec_hdr: Bad block @ %d\n", peb); |
| 159 | goto out; |
| 160 | } |
| 161 | |
| 162 | if (qpic_nand_read(peb * num_pages_per_blk, 1, tmp_buf, spare)) { |
| 163 | dprintf(CRITICAL, "read_ec_hdr: Read %d failed \n", peb); |
| 164 | goto out; |
| 165 | } |
| 166 | memcpy(ec_hdr, tmp_buf, UBI_EC_HDR_SIZE); |
| 167 | |
| 168 | if (check_pattern((void *)ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { |
| 169 | ret = 1; |
| 170 | goto out; |
| 171 | } |
| 172 | |
| 173 | /* Make sure we read a valid UBI EC_HEADER */ |
| 174 | if (BE32(ec_hdr->magic) != (uint32_t)UBI_EC_HDR_MAGIC) { |
| 175 | dprintf(CRITICAL, |
| 176 | "read_ec_hdr: Wrong magic at peb-%d Expected: %d, received %d\n", |
| 177 | peb, UBI_EC_HDR_MAGIC, BE32(ec_hdr->magic)); |
| 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | if (ec_hdr->version != UBI_VERSION) { |
| 182 | dprintf(CRITICAL, |
| 183 | "read_ec_hdr: Wrong version at peb-%d Expected: %d, received %d\n", |
| 184 | peb, UBI_VERSION, ec_hdr->version); |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | if (BE64(ec_hdr->ec) > UBI_MAX_ERASECOUNTER) { |
| 189 | dprintf(CRITICAL, |
| 190 | "read_ec_hdr: Wrong ec at peb-%d: %lld \n", |
| 191 | peb, BE64(ec_hdr->ec)); |
| 192 | goto out; |
| 193 | } |
| 194 | |
| 195 | crc = mtd_crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); |
| 196 | if (BE32(ec_hdr->hdr_crc) != crc) { |
| 197 | dprintf(CRITICAL, |
| 198 | "read_ec_hdr: Wrong crc at peb-%d: calculated %d, recived %d\n", |
| 199 | peb,crc, BE32(ec_hdr->hdr_crc)); |
| 200 | goto out; |
| 201 | } |
| 202 | |
| 203 | ret = 0; |
| 204 | out: |
| 205 | free(tmp_buf); |
| 206 | out_tmp_buf: |
| 207 | free(spare); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * write_ec_header() - Write provided ec_header for given PEB |
| 213 | * @peb: number of the physical erase block to write the header to |
| 214 | * @new_ech: the ec_header to write |
| 215 | * |
| 216 | * Return codes: |
| 217 | * -1 - in case of error |
| 218 | * 0 - on success |
| 219 | */ |
| 220 | static int write_ec_header(uint32_t peb, struct ubi_ec_hdr *new_ech) |
| 221 | { |
| 222 | unsigned page_size = flash_page_size(); |
| 223 | int num_pages_per_blk = flash_block_size()/page_size; |
| 224 | unsigned char *buf; |
| 225 | int ret = 0; |
| 226 | |
| 227 | buf = malloc(sizeof(uint8_t) * page_size); |
| 228 | if (!buf) { |
| 229 | dprintf(CRITICAL, "write_ec_header: Mem allocation failed\n"); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | memset(buf, 0, page_size); |
| 234 | ASSERT(page_size > sizeof(*new_ech)); |
| 235 | memcpy(buf, new_ech, UBI_EC_HDR_SIZE); |
| 236 | ret = qpic_nand_write(peb * num_pages_per_blk, 1, buf, 0); |
| 237 | if (ret) { |
| 238 | dprintf(CRITICAL, |
| 239 | "write_ec_header: qpic_nand_write failed with %d\n", ret); |
| 240 | ret = -1; |
| 241 | goto out; |
| 242 | } |
| 243 | |
| 244 | out: |
| 245 | free(buf); |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * scan_partition() - Collect the ec_headers info of a given partition |
| 251 | * @ptn: partition to read the headers of |
| 252 | * |
| 253 | * Returns allocated and filled struct ubi_scan_info (si). |
| 254 | * Note: si should be released by caller. |
| 255 | */ |
| 256 | static struct ubi_scan_info *scan_partition(struct ptentry *ptn) |
| 257 | { |
| 258 | struct ubi_scan_info *si; |
| 259 | struct ubi_ec_hdr *ec_hdr; |
| 260 | unsigned i, curr_peb; |
| 261 | unsigned long long sum = 0; |
| 262 | int page_size = flash_page_size(); |
| 263 | int ret; |
| 264 | |
| 265 | si = malloc(sizeof(*si)); |
| 266 | if (!si) { |
| 267 | dprintf(CRITICAL,"scan_partition: (%s) Memory allocation failed\n", |
| 268 | ptn->name); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | memset((void *)si, 0, sizeof(*si)); |
| 273 | si->ec = malloc(ptn->length * sizeof(uint64_t)); |
| 274 | if (!si->ec) { |
| 275 | dprintf(CRITICAL,"scan_partition: (%s) Memory allocation failed\n", |
| 276 | ptn->name); |
| 277 | goto out_failed_ec; |
| 278 | } |
| 279 | memset((void *)si->ec, 0, ptn->length * sizeof(uint64_t)); |
| 280 | |
| 281 | ec_hdr = malloc(UBI_EC_HDR_SIZE); |
| 282 | if (!ec_hdr) { |
| 283 | dprintf(CRITICAL,"scan_partition: (%s) Memory allocation failed\n", |
| 284 | ptn->name); |
| 285 | goto out_failed; |
| 286 | } |
| 287 | |
| 288 | curr_peb = ptn->start; |
| 289 | si->vid_hdr_offs = 0; |
| 290 | si->image_seq = rand() & UBI_IMAGE_SEQ_BASE; |
| 291 | |
| 292 | for (i = 0; i < ptn->length; i++){ |
| 293 | ret = read_ec_hdr(curr_peb + i, ec_hdr); |
| 294 | switch (ret) { |
| 295 | case 1: |
| 296 | si->empty_cnt++; |
| 297 | si->ec[i] = UBI_MAX_ERASECOUNTER; |
| 298 | break; |
| 299 | case 0: |
| 300 | if (!si->vid_hdr_offs) { |
| 301 | si->vid_hdr_offs = BE32(ec_hdr->vid_hdr_offset); |
| 302 | si->data_offs = BE32(ec_hdr->data_offset); |
| 303 | if (!si->vid_hdr_offs || !si->data_offs || |
| 304 | si->vid_hdr_offs % page_size || |
| 305 | si->data_offs % page_size) { |
| 306 | si->bad_cnt++; |
| 307 | si->ec[i] = UBI_MAX_ERASECOUNTER; |
| 308 | si->vid_hdr_offs = 0; |
| 309 | continue; |
| 310 | } |
| 311 | if (BE32(ec_hdr->vid_hdr_offset) != si->vid_hdr_offs) { |
| 312 | si->bad_cnt++; |
| 313 | si->ec[i] = UBI_MAX_ERASECOUNTER; |
| 314 | continue; |
| 315 | } |
| 316 | if (BE32(ec_hdr->data_offset) != si->data_offs) { |
| 317 | si->bad_cnt++; |
| 318 | si->ec[i] = UBI_MAX_ERASECOUNTER; |
| 319 | continue; |
| 320 | } |
| 321 | } |
| 322 | si->good_cnt++; |
| 323 | si->ec[i] = BE64(ec_hdr->ec); |
| 324 | break; |
| 325 | case -1: |
| 326 | default: |
| 327 | si->bad_cnt++; |
| 328 | si->ec[i] = UBI_MAX_ERASECOUNTER; |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * If less then 95% of the PEBs were "bad" (didn't have valid |
| 335 | * ec header), then set mean_ec = UBI_DEF_ERACE_COUNTER. |
| 336 | */ |
| 337 | sum = 0; |
| 338 | if (si->good_cnt && (double)(si->good_cnt / ptn->length) * 100 > 95) { |
| 339 | for (i = 0; i < ptn->length; i++) { |
| 340 | if (si->ec[i] == UBI_MAX_ERASECOUNTER) |
| 341 | continue; |
| 342 | sum += si->ec[i]; |
| 343 | } |
| 344 | si->mean_ec = sum / si->good_cnt; |
| 345 | } else { |
| 346 | si->mean_ec = UBI_DEF_ERACE_COUNTER; |
| 347 | } |
| 348 | free(ec_hdr); |
| 349 | return si; |
| 350 | |
| 351 | out_failed: |
| 352 | free(si->ec); |
| 353 | out_failed_ec: |
| 354 | free(si); |
| 355 | return NULL; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * update_ec_header() - Update provided ec_header |
| 360 | * @si: pointer to struct ubi_scan_info, holding the collected |
| 361 | * ec_headers information of the partition |
| 362 | * @index: index in si->ec[] of this peb. Relative to ptn->start |
| 363 | * @new_header: False if this is an update of an existing header. |
| 364 | * True if new header needs to be filled in |
| 365 | */ |
| 366 | static void update_ec_header(struct ubi_ec_hdr *old_ech, |
| 367 | const struct ubi_scan_info *si, |
| 368 | int index, bool new_header) |
| 369 | { |
| 370 | uint32_t crc; |
| 371 | |
| 372 | if (si->ec[index] < UBI_MAX_ERASECOUNTER) |
| 373 | old_ech->ec = BE64(si->ec[index] + 1); |
| 374 | else |
| 375 | old_ech->ec = BE64(si->mean_ec); |
| 376 | |
| 377 | if (new_header) { |
| 378 | old_ech->vid_hdr_offset = BE32(si->vid_hdr_offs); |
| 379 | old_ech->data_offset = BE32(si->data_offs); |
| 380 | old_ech->magic = BE32(UBI_EC_HDR_MAGIC); |
| 381 | old_ech->version = UBI_VERSION; |
| 382 | } |
| 383 | old_ech->image_seq = BE32(si->image_seq); |
| 384 | crc = mtd_crc32(UBI_CRC32_INIT, |
| 385 | (const void *)old_ech, UBI_EC_HDR_SIZE_CRC); |
| 386 | old_ech->hdr_crc = BE32(crc); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * calc_data_len - calculate how much real data is stored in the buffer |
| 391 | * @page_size: min I/O of the device |
| 392 | * @buf: a buffer with the contents of the physical eraseblock |
| 393 | * @len: the buffer length |
| 394 | * |
| 395 | * This function calculates how much "real data" is stored in @buf and |
| 396 | * returns the length (in number of pages). Continuous 0xFF bytes at the end |
| 397 | * of the buffer are not considered as "real data". |
| 398 | */ |
| 399 | static int calc_data_len(int page_size, const void *buf, int len) |
| 400 | { |
| 401 | int i; |
| 402 | |
| 403 | for (i = len - 1; i >= 0; i--) |
| 404 | if (((const uint8_t *)buf)[i] != 0xFF) |
| 405 | break; |
| 406 | |
| 407 | /* The resulting length must be aligned to the minimum flash I/O size */ |
| 408 | len = i + 1; |
| 409 | len = (len + page_size - 1) / page_size; |
| 410 | return len; |
| 411 | } |
| 412 | |
| 413 | /** |
Tanya Brokhman | c52aeb8 | 2015-01-27 11:08:50 +0200 | [diff] [blame] | 414 | * fastmap_present - returns true if Fastmap superblock is found |
| 415 | * @data: raw data to test |
| 416 | * |
| 417 | * This function returns 1 if the provided PEB data contains |
| 418 | * Fastmap superblock, 0 otherwise |
| 419 | */ |
| 420 | static int fastmap_present(const void *data){ |
| 421 | struct ubi_ec_hdr *ec_hdr = (struct ubi_ec_hdr *)(data); |
| 422 | struct ubi_vid_hdr *vid_hdr; |
| 423 | |
| 424 | vid_hdr = (struct ubi_vid_hdr *)(data + BE32(ec_hdr->vid_hdr_offset)); |
| 425 | if (BE32(vid_hdr->vol_id) == UBI_FM_SB_VOLUME_ID) |
| 426 | return 1; |
| 427 | else |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * ubi_erase_peb - Erase PEB and update EC header |
| 433 | * @peb_num: number of the PEB to erase |
| 434 | * @need_erase: if true PEB will be erased |
| 435 | * @si: UBI scan information |
| 436 | * @ptn_start: first PEB of the flashed partition |
| 437 | * |
| 438 | * This function erases the given PEB (if required) and writes a new EC |
| 439 | * header for it. |
| 440 | * |
| 441 | * Returns: -1 on error |
| 442 | * 0 on success |
| 443 | */ |
| 444 | static int ubi_erase_peb(int peb_num, int need_erase, |
| 445 | struct ubi_scan_info *si, int ptn_start) |
| 446 | { |
| 447 | struct ubi_ec_hdr new_ech; |
| 448 | int page_size = flash_page_size(); |
| 449 | int num_pages_per_blk = flash_block_size() / page_size; |
| 450 | int ret; |
| 451 | |
| 452 | if (need_erase && qpic_nand_blk_erase(peb_num * num_pages_per_blk)) { |
| 453 | dprintf(INFO, "flash_ubi_img: erase of %d failed\n", peb_num); |
| 454 | return -1; |
| 455 | } |
| 456 | memset(&new_ech, 0xff, sizeof(new_ech)); |
| 457 | update_ec_header(&new_ech, si, peb_num - ptn_start, true); |
| 458 | |
| 459 | /* Write new ec_header */ |
| 460 | ret = write_ec_header(peb_num, &new_ech); |
| 461 | if (ret) { |
| 462 | dprintf(CRITICAL, "flash_ubi_img: write ec_header to %d failed\n", |
| 463 | peb_num); |
| 464 | return -1; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
Tanya Brokhman | f934316 | 2015-01-27 13:37:41 +0200 | [diff] [blame^] | 468 | |
| 469 | /** |
| 470 | * remove_F_flag() - Turn off space-fixup flag in the ubifs superblock |
| 471 | * @data: pointer to the peb to check in the flashed image |
| 472 | * |
| 473 | * The UBIFS Superblock will be located at LEB 0 of the image. LEB 0 will be |
| 474 | * mapped as follows: |
| 475 | * If the image contains Fastmap superblock: |
| 476 | * - LEB 0 will be at PEB3 |
| 477 | * else: |
| 478 | * - LEB 0 will be at PEB2 |
| 479 | */ |
| 480 | static void remove_F_flag(const void *leb_data) |
| 481 | { |
| 482 | struct ubifs_ch *ch; |
| 483 | struct ubifs_sb_node *ubifs_sb; |
| 484 | struct ubi_ec_hdr *ech; |
| 485 | struct ubi_vid_hdr *vidh; |
| 486 | int vol_id; |
| 487 | |
| 488 | ech = (struct ubi_ec_hdr *)leb_data; |
| 489 | vidh = (struct ubi_vid_hdr *)(leb_data + BE32(ech->vid_hdr_offset)); |
| 490 | vol_id = BE32(vidh->vol_id); |
| 491 | |
| 492 | if (vol_id > UBI_MAX_VOLUMES && |
| 493 | vol_id != UBI_LAYOUT_VOLUME_ID && |
| 494 | vol_id != UBI_FM_SB_VOLUME_ID) |
| 495 | return; |
| 496 | |
| 497 | ubifs_sb = (struct ubifs_sb_node *)(leb_data + BE32(ech->data_offset)); |
| 498 | ch = (struct ubifs_ch *)ubifs_sb; |
| 499 | if (ch->node_type != UBIFS_SB_NODE) |
| 500 | return; |
| 501 | if (ubifs_sb->flags & UBIFS_FLG_SPACE_FIXUP) { |
| 502 | ubifs_sb->flags &= (~UBIFS_FLG_SPACE_FIXUP); |
| 503 | ch->crc = mtd_crc32(UBIFS_CRC32_INIT, (void *)ubifs_sb + 8, |
| 504 | sizeof(struct ubifs_sb_node) - 8); |
| 505 | } |
| 506 | } |
| 507 | |
Tanya Brokhman | c52aeb8 | 2015-01-27 11:08:50 +0200 | [diff] [blame] | 508 | /** |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 509 | * flash_ubi_img() - Write the provided (UBI) image to given partition |
| 510 | * @ptn: partition to write the image to |
| 511 | * @data: the image to write |
| 512 | * @size: size of the image to write |
| 513 | * |
| 514 | * Return codes: |
| 515 | * -1 - in case of error |
| 516 | * 0 - on success |
| 517 | */ |
| 518 | int flash_ubi_img(struct ptentry *ptn, void *data, unsigned size) |
| 519 | { |
| 520 | struct ubi_scan_info *si; |
| 521 | struct ubi_ec_hdr *old_ech; |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 522 | uint32_t curr_peb = ptn->start; |
| 523 | void *img_peb; |
| 524 | unsigned page_size = flash_page_size(); |
| 525 | unsigned block_size = flash_block_size(); |
| 526 | int num_pages_per_blk = block_size / page_size; |
| 527 | int num_pages; |
| 528 | int ret, need_erase; |
Tanya Brokhman | c52aeb8 | 2015-01-27 11:08:50 +0200 | [diff] [blame] | 529 | int bad_blocks_cnt = 0; |
| 530 | int fmsb_peb = 0; |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 531 | |
| 532 | si = scan_partition(ptn); |
| 533 | if (!si) { |
| 534 | dprintf(CRITICAL, "flash_ubi_img: scan_partition failed\n"); |
| 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * In case si->vid_hdr_offs is still -1 (non UBI image was |
| 540 | * flashed on device, get the value from the image to flush |
| 541 | */ |
| 542 | if (!si->vid_hdr_offs){ |
| 543 | struct ubi_ec_hdr *echd = (struct ubi_ec_hdr *)data; |
| 544 | si->vid_hdr_offs = BE32(echd->vid_hdr_offset); |
| 545 | si->data_offs = BE32(echd->data_offset); |
| 546 | } |
| 547 | |
| 548 | need_erase = (si->empty_cnt == (int)ptn->length ? 0 : 1); |
| 549 | /* Update the "to be" flashed image and flash it */ |
| 550 | img_peb = data; |
| 551 | while (size && curr_peb < ptn->start + ptn->length) { |
| 552 | if (need_erase && qpic_nand_blk_erase(curr_peb * num_pages_per_blk)) { |
| 553 | dprintf(CRITICAL, "flash_ubi_img: erase of %d failed\n", |
| 554 | curr_peb); |
Tanya Brokhman | c52aeb8 | 2015-01-27 11:08:50 +0200 | [diff] [blame] | 555 | bad_blocks_cnt++; |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 556 | curr_peb++; |
| 557 | continue; |
| 558 | } |
Tanya Brokhman | f934316 | 2015-01-27 13:37:41 +0200 | [diff] [blame^] | 559 | remove_F_flag(img_peb); |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 560 | /* Update the ec_header in the image */ |
| 561 | old_ech = (struct ubi_ec_hdr *)img_peb; |
| 562 | update_ec_header(old_ech, si, curr_peb - ptn->start, false); |
| 563 | if (size < block_size) |
| 564 | num_pages = size / page_size; |
| 565 | else |
| 566 | num_pages = calc_data_len(page_size, img_peb, block_size); |
| 567 | /* Write one block from image */ |
| 568 | ret = qpic_nand_write(curr_peb * num_pages_per_blk, |
| 569 | num_pages, img_peb, 0); |
| 570 | if (ret) { |
| 571 | dprintf(CRITICAL, "flash_ubi_img: writing to peb-%d failed\n", |
| 572 | curr_peb); |
Tanya Brokhman | c52aeb8 | 2015-01-27 11:08:50 +0200 | [diff] [blame] | 573 | bad_blocks_cnt++; |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 574 | curr_peb++; |
| 575 | continue; |
| 576 | } |
| 577 | if (size < block_size) |
| 578 | size = 0; |
| 579 | else |
| 580 | size -= block_size; |
Tanya Brokhman | c52aeb8 | 2015-01-27 11:08:50 +0200 | [diff] [blame] | 581 | |
| 582 | if (fastmap_present(img_peb)) |
| 583 | fmsb_peb = curr_peb; |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 584 | img_peb += flash_block_size(); |
| 585 | curr_peb++; |
| 586 | } |
| 587 | |
| 588 | if (size) { |
| 589 | dprintf(CRITICAL, |
| 590 | "flash_ubi_img: Not enough good blocks to flash image!"); |
| 591 | ret = -1; |
| 592 | goto out; |
| 593 | } |
| 594 | |
| 595 | /* Erase and write ec_header for the rest of the blocks */ |
Tanya Brokhman | c52aeb8 | 2015-01-27 11:08:50 +0200 | [diff] [blame] | 596 | for (; curr_peb < ptn->start + ptn->length; curr_peb++) |
| 597 | if (ubi_erase_peb(curr_peb, need_erase, si, ptn->start)) |
| 598 | bad_blocks_cnt++; |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 599 | |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 600 | ret = 0; |
Tanya Brokhman | c52aeb8 | 2015-01-27 11:08:50 +0200 | [diff] [blame] | 601 | /* |
| 602 | * If flashed image contains fastmap data and bad blocks were found |
| 603 | * we need to invalidate the flashed fastmap since it isn't accurate |
| 604 | * anymore. |
| 605 | */ |
| 606 | if (bad_blocks_cnt && fmsb_peb) { |
| 607 | dprintf(CRITICAL, "flash_ubi_img: invalidate fmsb\n"); |
| 608 | ret = ubi_erase_peb(ptn->start + 2, 1, si, ptn->start); |
| 609 | } |
| 610 | |
Tanya Brokhman | ea981a3 | 2015-02-12 16:32:15 +0200 | [diff] [blame] | 611 | out: |
| 612 | free(si->ec); |
| 613 | free(si); |
| 614 | return ret; |
| 615 | } |