Skip to content

Commit add710e

Browse files
Johan Rudholmcjb
authored andcommitted
mmc: boot partition ro lock support
Enable boot partitions to be read-only locked until next power on via a sysfs entry. There will be one sysfs entry for each boot partition: /sys/block/mmcblkXbootY/ro_lock_until_next_power_on Each boot partition is locked by writing 1 to its file. Signed-off-by: Johan Rudholm <johan.rudholm@stericsson.com> Signed-off-by: John Beckett <john.beckett@stericsson.com> Signed-off-by: Chris Ball <cjb@laptop.org>
1 parent 92df954 commit add710e

5 files changed

Lines changed: 153 additions & 11 deletions

File tree

‎Documentation/mmc/mmc-dev-parts.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,16 @@ echo 0 > /sys/block/mmcblkXbootY/force_ro
2525
To re-enable read-only access:
2626

2727
echo 1 > /sys/block/mmcblkXbootY/force_ro
28+
29+
The boot partitions can also be locked read only until the next power on,
30+
with:
31+
32+
echo 1 > /sys/block/mmcblkXbootY/ro_lock_until_next_power_on
33+
34+
This is a feature of the card and not of the kernel. If the card does
35+
not support boot partition locking, the file will not exist. If the
36+
feature has been disabled on the card, the file will be read-only.
37+
38+
The boot partitions can also be locked permanently, but this feature is
39+
not accessible through sysfs in order to avoid accidental or malicious
40+
bricking.

‎drivers/mmc/card/block.c‎

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ struct mmc_blk_data {
107107
*/
108108
unsigned int part_curr;
109109
struct device_attribute force_ro;
110+
struct device_attribute power_ro_lock;
111+
int area_type;
110112
};
111113

112114
static DEFINE_MUTEX(open_lock);
@@ -165,6 +167,70 @@ static void mmc_blk_put(struct mmc_blk_data *md)
165167
mutex_unlock(&open_lock);
166168
}
167169

170+
static ssize_t power_ro_lock_show(struct device *dev,
171+
struct device_attribute *attr, char *buf)
172+
{
173+
int ret;
174+
struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
175+
struct mmc_card *card = md->queue.card;
176+
int locked = 0;
177+
178+
if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
179+
locked = 2;
180+
else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
181+
locked = 1;
182+
183+
ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
184+
185+
return ret;
186+
}
187+
188+
static ssize_t power_ro_lock_store(struct device *dev,
189+
struct device_attribute *attr, const char *buf, size_t count)
190+
{
191+
int ret;
192+
struct mmc_blk_data *md, *part_md;
193+
struct mmc_card *card;
194+
unsigned long set;
195+
196+
if (kstrtoul(buf, 0, &set))
197+
return -EINVAL;
198+
199+
if (set != 1)
200+
return count;
201+
202+
md = mmc_blk_get(dev_to_disk(dev));
203+
card = md->queue.card;
204+
205+
mmc_claim_host(card->host);
206+
207+
ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
208+
card->ext_csd.boot_ro_lock |
209+
EXT_CSD_BOOT_WP_B_PWR_WP_EN,
210+
card->ext_csd.part_time);
211+
if (ret)
212+
pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
213+
else
214+
card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
215+
216+
mmc_release_host(card->host);
217+
218+
if (!ret) {
219+
pr_info("%s: Locking boot partition ro until next power on\n",
220+
md->disk->disk_name);
221+
set_disk_ro(md->disk, 1);
222+
223+
list_for_each_entry(part_md, &md->part, part)
224+
if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
225+
pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
226+
set_disk_ro(part_md->disk, 1);
227+
}
228+
}
229+
230+
mmc_blk_put(md);
231+
return count;
232+
}
233+
168234
static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
169235
char *buf)
170236
{
@@ -1347,7 +1413,8 @@ static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
13471413
struct device *parent,
13481414
sector_t size,
13491415
bool default_ro,
1350-
const char *subname)
1416+
const char *subname,
1417+
int area_type)
13511418
{
13521419
struct mmc_blk_data *md;
13531420
int devidx, ret;
@@ -1372,11 +1439,12 @@ static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
13721439
if (!subname) {
13731440
md->name_idx = find_first_zero_bit(name_use, max_devices);
13741441
__set_bit(md->name_idx, name_use);
1375-
}
1376-
else
1442+
} else
13771443
md->name_idx = ((struct mmc_blk_data *)
13781444
dev_to_disk(parent)->private_data)->name_idx;
13791445

1446+
md->area_type = area_type;
1447+
13801448
/*
13811449
* Set the read-only status based on the supported commands
13821450
* and the write protect switch.
@@ -1470,7 +1538,8 @@ static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
14701538
size = card->csd.capacity << (card->csd.read_blkbits - 9);
14711539
}
14721540

1473-
md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1541+
md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1542+
MMC_BLK_DATA_AREA_MAIN);
14741543
return md;
14751544
}
14761545

@@ -1479,13 +1548,14 @@ static int mmc_blk_alloc_part(struct mmc_card *card,
14791548
unsigned int part_type,
14801549
sector_t size,
14811550
bool default_ro,
1482-
const char *subname)
1551+
const char *subname,
1552+
int area_type)
14831553
{
14841554
char cap_str[10];
14851555
struct mmc_blk_data *part_md;
14861556

14871557
part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1488-
subname);
1558+
subname, area_type);
14891559
if (IS_ERR(part_md))
14901560
return PTR_ERR(part_md);
14911561
part_md->part_type = part_type;
@@ -1518,7 +1588,8 @@ static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
15181588
card->part[idx].part_cfg,
15191589
card->part[idx].size >> 9,
15201590
card->part[idx].force_ro,
1521-
card->part[idx].name);
1591+
card->part[idx].name,
1592+
card->part[idx].area_type);
15221593
if (ret)
15231594
return ret;
15241595
}
@@ -1547,9 +1618,16 @@ mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
15471618

15481619
static void mmc_blk_remove_req(struct mmc_blk_data *md)
15491620
{
1621+
struct mmc_card *card;
1622+
15501623
if (md) {
1624+
card = md->queue.card;
15511625
if (md->disk->flags & GENHD_FL_UP) {
15521626
device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1627+
if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1628+
card->ext_csd.boot_ro_lockable)
1629+
device_remove_file(disk_to_dev(md->disk),
1630+
&md->power_ro_lock);
15531631

15541632
/* Stop new requests from getting into the queue */
15551633
del_gendisk(md->disk);
@@ -1578,6 +1656,7 @@ static void mmc_blk_remove_parts(struct mmc_card *card,
15781656
static int mmc_add_disk(struct mmc_blk_data *md)
15791657
{
15801658
int ret;
1659+
struct mmc_card *card = md->queue.card;
15811660

15821661
add_disk(md->disk);
15831662
md->force_ro.show = force_ro_show;
@@ -1587,7 +1666,33 @@ static int mmc_add_disk(struct mmc_blk_data *md)
15871666
md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
15881667
ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
15891668
if (ret)
1590-
del_gendisk(md->disk);
1669+
goto force_ro_fail;
1670+
1671+
if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1672+
card->ext_csd.boot_ro_lockable) {
1673+
mode_t mode;
1674+
1675+
if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
1676+
mode = S_IRUGO;
1677+
else
1678+
mode = S_IRUGO | S_IWUSR;
1679+
1680+
md->power_ro_lock.show = power_ro_lock_show;
1681+
md->power_ro_lock.store = power_ro_lock_store;
1682+
md->power_ro_lock.attr.mode = mode;
1683+
md->power_ro_lock.attr.name =
1684+
"ro_lock_until_next_power_on";
1685+
ret = device_create_file(disk_to_dev(md->disk),
1686+
&md->power_ro_lock);
1687+
if (ret)
1688+
goto power_ro_lock_fail;
1689+
}
1690+
return ret;
1691+
1692+
power_ro_lock_fail:
1693+
device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1694+
force_ro_fail:
1695+
del_gendisk(md->disk);
15911696

15921697
return ret;
15931698
}

‎drivers/mmc/core/mmc.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
348348
part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
349349
mmc_part_add(card, part_size,
350350
EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
351-
"boot%d", idx, true);
351+
"boot%d", idx, true,
352+
MMC_BLK_DATA_AREA_BOOT);
352353
}
353354
}
354355
}
@@ -435,7 +436,8 @@ static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
435436
hc_wp_grp_sz);
436437
mmc_part_add(card, part_size << 19,
437438
EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
438-
"gp%d", idx, false);
439+
"gp%d", idx, false,
440+
MMC_BLK_DATA_AREA_GP);
439441
}
440442
}
441443
card->ext_csd.sec_trim_mult =
@@ -446,6 +448,14 @@ static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
446448
ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
447449
card->ext_csd.trim_timeout = 300 *
448450
ext_csd[EXT_CSD_TRIM_MULT];
451+
452+
/*
453+
* Note that the call to mmc_part_add above defaults to read
454+
* only. If this default assumption is changed, the call must
455+
* take into account the value of boot_locked below.
456+
*/
457+
card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
458+
card->ext_csd.boot_ro_lockable = true;
449459
}
450460

451461
if (card->ext_csd.rev >= 5) {

‎include/linux/mmc/card.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ struct mmc_ext_csd {
7171
bool hpi_en; /* HPI enablebit */
7272
bool hpi; /* HPI support bit */
7373
unsigned int hpi_cmd; /* cmd used as HPI */
74+
unsigned int boot_ro_lock; /* ro lock support */
75+
bool boot_ro_lockable;
7476
u8 raw_partition_support; /* 160 */
7577
u8 raw_erased_mem_count; /* 181 */
7678
u8 raw_ext_csd_structure; /* 194 */
@@ -187,6 +189,10 @@ struct mmc_part {
187189
unsigned int part_cfg; /* partition type */
188190
char name[MAX_MMC_PART_NAME_LEN];
189191
bool force_ro; /* to make boot parts RO by default */
192+
unsigned int area_type;
193+
#define MMC_BLK_DATA_AREA_MAIN (1<<0)
194+
#define MMC_BLK_DATA_AREA_BOOT (1<<1)
195+
#define MMC_BLK_DATA_AREA_GP (1<<2)
190196
};
191197

192198
/*
@@ -265,12 +271,14 @@ struct mmc_card {
265271
* This function fill contents in mmc_part.
266272
*/
267273
static inline void mmc_part_add(struct mmc_card *card, unsigned int size,
268-
unsigned int part_cfg, char *name, int idx, bool ro)
274+
unsigned int part_cfg, char *name, int idx, bool ro,
275+
int area_type)
269276
{
270277
card->part[card->nr_parts].size = size;
271278
card->part[card->nr_parts].part_cfg = part_cfg;
272279
sprintf(card->part[card->nr_parts].name, name, idx);
273280
card->part[card->nr_parts].force_ro = ro;
281+
card->part[card->nr_parts].area_type = area_type;
274282
card->nr_parts++;
275283
}
276284

‎include/linux/mmc/mmc.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ struct _mmc_csd {
280280
#define EXT_CSD_RST_N_FUNCTION 162 /* R/W */
281281
#define EXT_CSD_SANITIZE_START 165 /* W */
282282
#define EXT_CSD_WR_REL_PARAM 166 /* RO */
283+
#define EXT_CSD_BOOT_WP 173 /* R/W */
283284
#define EXT_CSD_ERASE_GROUP_DEF 175 /* R/W */
284285
#define EXT_CSD_PART_CONFIG 179 /* R/W */
285286
#define EXT_CSD_ERASED_MEM_CONT 181 /* RO */
@@ -321,6 +322,11 @@ struct _mmc_csd {
321322

322323
#define EXT_CSD_WR_REL_PARAM_EN (1<<2)
323324

325+
#define EXT_CSD_BOOT_WP_B_PWR_WP_DIS (0x40)
326+
#define EXT_CSD_BOOT_WP_B_PERM_WP_DIS (0x10)
327+
#define EXT_CSD_BOOT_WP_B_PERM_WP_EN (0x04)
328+
#define EXT_CSD_BOOT_WP_B_PWR_WP_EN (0x01)
329+
324330
#define EXT_CSD_PART_CONFIG_ACC_MASK (0x7)
325331
#define EXT_CSD_PART_CONFIG_ACC_BOOT0 (0x1)
326332
#define EXT_CSD_PART_CONFIG_ACC_GP0 (0x4)

0 commit comments

Comments
 (0)