|
6 | 6 | * to you under the Apache License, Version 2.0 (the
|
7 | 7 | * "License"); you may not use this file except in compliance
|
8 | 8 | * with the License. You may obtain a copy of the License at
|
9 |
| - * |
| 9 | + * |
10 | 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
11 | 11 | *
|
12 | 12 | * Unless required by applicable law or agreed to in writing,
|
@@ -298,6 +298,66 @@ hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
|
298 | 298 | return 0;
|
299 | 299 | }
|
300 | 300 |
|
| 301 | +static int |
| 302 | +hal_flash_is_setto(const struct hal_flash *hf, uint32_t address, |
| 303 | + uint32_t num_bytes, uint8_t val) |
| 304 | +{ |
| 305 | + uint8_t buf[32]; |
| 306 | + uint32_t blksz; |
| 307 | + int i; |
| 308 | + |
| 309 | + while (num_bytes) { |
| 310 | + blksz = sizeof(buf); |
| 311 | + if (blksz > num_bytes) { |
| 312 | + blksz = num_bytes; |
| 313 | + } |
| 314 | + if (hf->hf_itf->hff_read(hf, address, buf, blksz)) { |
| 315 | + return -1; |
| 316 | + } |
| 317 | + for (i = 0; i < blksz; i++) { |
| 318 | + if (buf[i] != val) { |
| 319 | + return 0; |
| 320 | + } |
| 321 | + } |
| 322 | + num_bytes -= blksz; |
| 323 | + } |
| 324 | + return 1; |
| 325 | +} |
| 326 | + |
| 327 | +int |
| 328 | +hal_flash_is_ones(const struct hal_flash *hf, uint32_t address, |
| 329 | + uint32_t num_bytes) |
| 330 | +{ |
| 331 | + return hal_flash_is_setto(hf, address, num_bytes, 0xff); |
| 332 | +} |
| 333 | + |
| 334 | +int |
| 335 | +hal_flash_is_zeroes(const struct hal_flash *hf, uint32_t address, |
| 336 | + uint32_t num_bytes) |
| 337 | +{ |
| 338 | + return hal_flash_is_setto(hf, address, num_bytes, 0); |
| 339 | +} |
| 340 | + |
| 341 | +int |
| 342 | +hal_flash_isempty(uint8_t id, uint32_t address, uint32_t num_bytes) |
| 343 | +{ |
| 344 | + const struct hal_flash *hf; |
| 345 | + |
| 346 | + hf = hal_bsp_flash_dev(id); |
| 347 | + if (!hf) { |
| 348 | + return -1; |
| 349 | + } |
| 350 | + if (hal_flash_check_addr(hf, address) || |
| 351 | + hal_flash_check_addr(hf, address + num_bytes)) { |
| 352 | + return -1; |
| 353 | + } |
| 354 | + if (hf->hf_itf->hff_is_empty) { |
| 355 | + return hf->hf_itf->hff_is_empty(hf, address, num_bytes); |
| 356 | + } else { |
| 357 | + return hal_flash_is_ones(hf, address, num_bytes); |
| 358 | + } |
| 359 | +} |
| 360 | + |
301 | 361 | int
|
302 | 362 | hal_flash_ioctl(uint8_t id, uint32_t cmd, void *args)
|
303 | 363 | {
|
|
0 commit comments