Skip to content

Commit 62593cf

Browse files
mtd: spi-nor: refactor block protection functions
This code was a bit sloppy, would produce a lot of copy-and-paste, and did not always provide a sensible interface: * It didn't validate the length for LOCK and the offset for UNLOCK, so we were essentially discarding half of the user-supplied data and assuming what they wanted to lock/unlock * It didn't do very good error checking * It didn't make use of the fact that this operation works on power-of-two dimensions So, rewrite this to do proper bit arithmetic rather than a bunch of hard-coded condition tables. Now we have: * More comments on how this was derived * Notes on what is (and isn't) supported * A more exendible function, so we could add support for other protection ranges * More accurate locking - e.g., suppose the top quadrant is locked (75% to 100%); then in the following cases, case (a) will succeed but (b) will not (return -EINVAL): (a) user requests lock 3rd quadrant (50% to 75%) (b) user requests lock 3rd quadrant, minus a few blocks (e.g., 50% to 73%) Case (b) *should* fail, since we'd have to lock blocks that weren't requested. But the old implementation didn't know the difference and would lock the entire second half (50% to 100%) This refactoring work will also help enable the addition of mtd_is_locked() support and potentially the support of bottom boot protection (TB=1). Signed-off-by: Brian Norris <computersforpeace@gmail.com>
1 parent f890025 commit 62593cf

1 file changed

Lines changed: 126 additions & 45 deletions

File tree

‎drivers/mtd/spi-nor/spi-nor.c‎

Lines changed: 126 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -400,72 +400,153 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
400400
return ret;
401401
}
402402

403+
static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
404+
uint64_t *len)
405+
{
406+
struct mtd_info *mtd = &nor->mtd;
407+
u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
408+
int shift = ffs(mask) - 1;
409+
int pow;
410+
411+
if (!(sr & mask)) {
412+
/* No protection */
413+
*ofs = 0;
414+
*len = 0;
415+
} else {
416+
pow = ((sr & mask) ^ mask) >> shift;
417+
*len = mtd->size >> pow;
418+
*ofs = mtd->size - *len;
419+
}
420+
}
421+
422+
/*
423+
* Return 1 if the entire region is locked, 0 otherwise
424+
*/
425+
static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
426+
u8 sr)
427+
{
428+
loff_t lock_offs;
429+
uint64_t lock_len;
430+
431+
stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
432+
433+
return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
434+
}
435+
436+
/*
437+
* Lock a region of the flash. Compatible with ST Micro and similar flash.
438+
* Supports only the block protection bits BP{0,1,2} in the status register
439+
* (SR). Does not support these features found in newer SR bitfields:
440+
* - TB: top/bottom protect - only handle TB=0 (top protect)
441+
* - SEC: sector/block protect - only handle SEC=0 (block protect)
442+
* - CMP: complement protect - only support CMP=0 (range is not complemented)
443+
*
444+
* Sample table portion for 8MB flash (Winbond w25q64fw):
445+
*
446+
* SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
447+
* --------------------------------------------------------------------------
448+
* X | X | 0 | 0 | 0 | NONE | NONE
449+
* 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
450+
* 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
451+
* 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
452+
* 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
453+
* 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
454+
* 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
455+
* X | X | 1 | 1 | 1 | 8 MB | ALL
456+
*
457+
* Returns negative on errors, 0 on success.
458+
*/
403459
static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
404460
{
405461
struct mtd_info *mtd = &nor->mtd;
406-
uint32_t offset = ofs;
407-
uint8_t status_old, status_new;
408-
int ret = 0;
462+
u8 status_old, status_new;
463+
u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
464+
u8 shift = ffs(mask) - 1, pow, val;
409465

410466
status_old = read_sr(nor);
411467

412-
if (offset < mtd->size - (mtd->size / 2))
413-
status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
414-
else if (offset < mtd->size - (mtd->size / 4))
415-
status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
416-
else if (offset < mtd->size - (mtd->size / 8))
417-
status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
418-
else if (offset < mtd->size - (mtd->size / 16))
419-
status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
420-
else if (offset < mtd->size - (mtd->size / 32))
421-
status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
422-
else if (offset < mtd->size - (mtd->size / 64))
423-
status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
424-
else
425-
status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
468+
/* SPI NOR always locks to the end */
469+
if (ofs + len != mtd->size) {
470+
/* Does combined region extend to end? */
471+
if (!stm_is_locked_sr(nor, ofs + len, mtd->size - ofs - len,
472+
status_old))
473+
return -EINVAL;
474+
len = mtd->size - ofs;
475+
}
476+
477+
/*
478+
* Need smallest pow such that:
479+
*
480+
* 1 / (2^pow) <= (len / size)
481+
*
482+
* so (assuming power-of-2 size) we do:
483+
*
484+
* pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
485+
*/
486+
pow = ilog2(mtd->size) - ilog2(len);
487+
val = mask - (pow << shift);
488+
if (val & ~mask)
489+
return -EINVAL;
490+
/* Don't "lock" with no region! */
491+
if (!(val & mask))
492+
return -EINVAL;
493+
494+
status_new = (status_old & ~mask) | val;
426495

427496
/* Only modify protection if it will not unlock other areas */
428-
if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) >
429-
(status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
430-
write_enable(nor);
431-
ret = write_sr(nor, status_new);
432-
}
497+
if ((status_new & mask) <= (status_old & mask))
498+
return -EINVAL;
433499

434-
return ret;
500+
write_enable(nor);
501+
return write_sr(nor, status_new);
435502
}
436503

504+
/*
505+
* Unlock a region of the flash. See stm_lock() for more info
506+
*
507+
* Returns negative on errors, 0 on success.
508+
*/
437509
static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
438510
{
439511
struct mtd_info *mtd = &nor->mtd;
440-
uint32_t offset = ofs;
441512
uint8_t status_old, status_new;
442-
int ret = 0;
513+
u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
514+
u8 shift = ffs(mask) - 1, pow, val;
443515

444516
status_old = read_sr(nor);
445517

446-
if (offset+len > mtd->size - (mtd->size / 64))
447-
status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0);
448-
else if (offset+len > mtd->size - (mtd->size / 32))
449-
status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
450-
else if (offset+len > mtd->size - (mtd->size / 16))
451-
status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
452-
else if (offset+len > mtd->size - (mtd->size / 8))
453-
status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
454-
else if (offset+len > mtd->size - (mtd->size / 4))
455-
status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
456-
else if (offset+len > mtd->size - (mtd->size / 2))
457-
status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
458-
else
459-
status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
518+
/* Cannot unlock; would unlock larger region than requested */
519+
if (stm_is_locked_sr(nor, status_old, ofs - mtd->erasesize,
520+
mtd->erasesize))
521+
return -EINVAL;
460522

461-
/* Only modify protection if it will not lock other areas */
462-
if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) <
463-
(status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
464-
write_enable(nor);
465-
ret = write_sr(nor, status_new);
523+
/*
524+
* Need largest pow such that:
525+
*
526+
* 1 / (2^pow) >= (len / size)
527+
*
528+
* so (assuming power-of-2 size) we do:
529+
*
530+
* pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
531+
*/
532+
pow = ilog2(mtd->size) - order_base_2(mtd->size - (ofs + len));
533+
if (ofs + len == mtd->size) {
534+
val = 0; /* fully unlocked */
535+
} else {
536+
val = mask - (pow << shift);
537+
/* Some power-of-two sizes are not supported */
538+
if (val & ~mask)
539+
return -EINVAL;
466540
}
467541

468-
return ret;
542+
status_new = (status_old & ~mask) | val;
543+
544+
/* Only modify protection if it will not lock other areas */
545+
if ((status_new & mask) >= (status_old & mask))
546+
return -EINVAL;
547+
548+
write_enable(nor);
549+
return write_sr(nor, status_new);
469550
}
470551

471552
static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)

0 commit comments

Comments
 (0)