Skip to content

Commit 181a9e8

Browse files
Tetsuhiro Kohadanamjaejeon
authored andcommitted
exfat: redefine PBR as boot_sector
Aggregate PBR related definitions and redefine as "boot_sector" to comply with the exFAT specification. And, rename variable names including 'pbr'. Signed-off-by: Tetsuhiro Kohada <[email protected]> Reviewed-by: Sungjong Seo <[email protected]> Signed-off-by: Namjae Jeon <[email protected]>
1 parent 943af1f commit 181a9e8

File tree

3 files changed

+72
-93
lines changed

3 files changed

+72
-93
lines changed

fs/exfat/exfat_fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ struct exfat_sb_info {
227227
unsigned int root_dir; /* root dir cluster */
228228
unsigned int dentries_per_clu; /* num of dentries per cluster */
229229
unsigned int vol_flag; /* volume dirty flag */
230-
struct buffer_head *pbr_bh; /* buffer_head of PBR sector */
230+
struct buffer_head *boot_bh; /* buffer_head of BOOT sector */
231231

232232
unsigned int map_clu; /* allocation bitmap start cluster */
233233
unsigned int map_sectors; /* num of allocation bitmap sectors */

fs/exfat/exfat_raw.h

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
#include <linux/types.h>
1010

11-
#define PBR_SIGNATURE 0xAA55
11+
#define BOOT_SIGNATURE 0xAA55
12+
#define EXBOOT_SIGNATURE 0xAA550000
1213

1314
#define EXFAT_MAX_FILE_LEN 255
1415

@@ -55,7 +56,7 @@
5556

5657
/* checksum types */
5758
#define CS_DIR_ENTRY 0
58-
#define CS_PBR_SECTOR 1
59+
#define CS_BOOT_SECTOR 1
5960
#define CS_DEFAULT 2
6061

6162
/* file attributes */
@@ -69,57 +70,35 @@
6970
#define ATTR_RWMASK (ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME | \
7071
ATTR_SUBDIR | ATTR_ARCHIVE)
7172

72-
#define PBR64_JUMP_BOOT_LEN 3
73-
#define PBR64_OEM_NAME_LEN 8
74-
#define PBR64_RESERVED_LEN 53
73+
#define BOOTSEC_JUMP_BOOT_LEN 3
74+
#define BOOTSEC_FS_NAME_LEN 8
75+
#define BOOTSEC_OLDBPB_LEN 53
7576

7677
#define EXFAT_FILE_NAME_LEN 15
7778

78-
/* EXFAT BIOS parameter block (64 bytes) */
79-
struct bpb64 {
80-
__u8 jmp_boot[PBR64_JUMP_BOOT_LEN];
81-
__u8 oem_name[PBR64_OEM_NAME_LEN];
82-
__u8 res_zero[PBR64_RESERVED_LEN];
83-
} __packed;
84-
85-
/* EXFAT EXTEND BIOS parameter block (56 bytes) */
86-
struct bsx64 {
87-
__le64 vol_offset;
88-
__le64 vol_length;
89-
__le32 fat_offset;
90-
__le32 fat_length;
91-
__le32 clu_offset;
92-
__le32 clu_count;
93-
__le32 root_cluster;
94-
__le32 vol_serial;
95-
__u8 fs_version[2];
96-
__le16 vol_flags;
97-
__u8 sect_size_bits;
98-
__u8 sect_per_clus_bits;
99-
__u8 num_fats;
100-
__u8 phy_drv_no;
101-
__u8 perc_in_use;
102-
__u8 reserved2[7];
103-
} __packed;
104-
105-
/* EXFAT PBR[BPB+BSX] (120 bytes) */
106-
struct pbr64 {
107-
struct bpb64 bpb;
108-
struct bsx64 bsx;
109-
} __packed;
110-
111-
/* Common PBR[Partition Boot Record] (512 bytes) */
112-
struct pbr {
113-
union {
114-
__u8 raw[64];
115-
struct bpb64 f64;
116-
} bpb;
117-
union {
118-
__u8 raw[56];
119-
struct bsx64 f64;
120-
} bsx;
121-
__u8 boot_code[390];
122-
__le16 signature;
79+
/* EXFAT: Main and Backup Boot Sector (512 bytes) */
80+
struct boot_sector {
81+
__u8 jmp_boot[BOOTSEC_JUMP_BOOT_LEN];
82+
__u8 fs_name[BOOTSEC_FS_NAME_LEN];
83+
__u8 must_be_zero[BOOTSEC_OLDBPB_LEN];
84+
__le64 partition_offset;
85+
__le64 vol_length;
86+
__le32 fat_offset;
87+
__le32 fat_length;
88+
__le32 clu_offset;
89+
__le32 clu_count;
90+
__le32 root_cluster;
91+
__le32 vol_serial;
92+
__u8 fs_revision[2];
93+
__le16 vol_flags;
94+
__u8 sect_size_bits;
95+
__u8 sect_per_clus_bits;
96+
__u8 num_fats;
97+
__u8 drv_sel;
98+
__u8 percent_in_use;
99+
__u8 reserved[7];
100+
__u8 boot_code[390];
101+
__le16 signature;
123102
} __packed;
124103

125104
struct exfat_dentry {

fs/exfat/super.c

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void exfat_put_super(struct super_block *sb)
4949
sync_blockdev(sb->s_bdev);
5050
exfat_set_vol_flags(sb, VOL_CLEAN);
5151
exfat_free_bitmap(sbi);
52-
brelse(sbi->pbr_bh);
52+
brelse(sbi->boot_bh);
5353
mutex_unlock(&sbi->s_lock);
5454

5555
call_rcu(&sbi->rcu, exfat_delayed_free);
@@ -101,7 +101,7 @@ static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
101101
int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag)
102102
{
103103
struct exfat_sb_info *sbi = EXFAT_SB(sb);
104-
struct pbr64 *bpb = (struct pbr64 *)sbi->pbr_bh->b_data;
104+
struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
105105
bool sync;
106106

107107
/* flags are not changed */
@@ -116,18 +116,18 @@ int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag)
116116
if (sb_rdonly(sb))
117117
return 0;
118118

119-
bpb->bsx.vol_flags = cpu_to_le16(new_flag);
119+
p_boot->vol_flags = cpu_to_le16(new_flag);
120120

121-
if (new_flag == VOL_DIRTY && !buffer_dirty(sbi->pbr_bh))
121+
if (new_flag == VOL_DIRTY && !buffer_dirty(sbi->boot_bh))
122122
sync = true;
123123
else
124124
sync = false;
125125

126-
set_buffer_uptodate(sbi->pbr_bh);
127-
mark_buffer_dirty(sbi->pbr_bh);
126+
set_buffer_uptodate(sbi->boot_bh);
127+
mark_buffer_dirty(sbi->boot_bh);
128128

129129
if (sync)
130-
sync_dirty_buffer(sbi->pbr_bh);
130+
sync_dirty_buffer(sbi->boot_bh);
131131
return 0;
132132
}
133133

@@ -366,13 +366,14 @@ static int exfat_read_root(struct inode *inode)
366366
return 0;
367367
}
368368

369-
static struct pbr *exfat_read_pbr_with_logical_sector(struct super_block *sb)
369+
static struct boot_sector *exfat_read_boot_with_logical_sector(
370+
struct super_block *sb)
370371
{
371372
struct exfat_sb_info *sbi = EXFAT_SB(sb);
372-
struct pbr *p_pbr = (struct pbr *) (sbi->pbr_bh)->b_data;
373+
struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
373374
unsigned short logical_sect = 0;
374375

375-
logical_sect = 1 << p_pbr->bsx.f64.sect_size_bits;
376+
logical_sect = 1 << p_boot->sect_size_bits;
376377

377378
if (!is_power_of_2(logical_sect) ||
378379
logical_sect < 512 || logical_sect > 4096) {
@@ -387,58 +388,57 @@ static struct pbr *exfat_read_pbr_with_logical_sector(struct super_block *sb)
387388
}
388389

389390
if (logical_sect > sb->s_blocksize) {
390-
brelse(sbi->pbr_bh);
391-
sbi->pbr_bh = NULL;
391+
brelse(sbi->boot_bh);
392+
sbi->boot_bh = NULL;
392393

393394
if (!sb_set_blocksize(sb, logical_sect)) {
394395
exfat_err(sb, "unable to set blocksize %u",
395396
logical_sect);
396397
return NULL;
397398
}
398-
sbi->pbr_bh = sb_bread(sb, 0);
399-
if (!sbi->pbr_bh) {
399+
sbi->boot_bh = sb_bread(sb, 0);
400+
if (!sbi->boot_bh) {
400401
exfat_err(sb, "unable to read boot sector (logical sector size = %lu)",
401402
sb->s_blocksize);
402403
return NULL;
403404
}
404405

405-
p_pbr = (struct pbr *)sbi->pbr_bh->b_data;
406+
p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
406407
}
407-
return p_pbr;
408+
return p_boot;
408409
}
409410

410411
/* mount the file system volume */
411412
static int __exfat_fill_super(struct super_block *sb)
412413
{
413414
int ret;
414-
struct pbr *p_pbr;
415-
struct pbr64 *p_bpb;
415+
struct boot_sector *p_boot;
416416
struct exfat_sb_info *sbi = EXFAT_SB(sb);
417417

418418
/* set block size to read super block */
419419
sb_min_blocksize(sb, 512);
420420

421421
/* read boot sector */
422-
sbi->pbr_bh = sb_bread(sb, 0);
423-
if (!sbi->pbr_bh) {
422+
sbi->boot_bh = sb_bread(sb, 0);
423+
if (!sbi->boot_bh) {
424424
exfat_err(sb, "unable to read boot sector");
425425
return -EIO;
426426
}
427427

428428
/* PRB is read */
429-
p_pbr = (struct pbr *)sbi->pbr_bh->b_data;
429+
p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
430430

431-
/* check the validity of PBR */
432-
if (le16_to_cpu((p_pbr->signature)) != PBR_SIGNATURE) {
431+
/* check the validity of BOOT */
432+
if (le16_to_cpu((p_boot->signature)) != BOOT_SIGNATURE) {
433433
exfat_err(sb, "invalid boot record signature");
434434
ret = -EINVAL;
435435
goto free_bh;
436436
}
437437

438438

439439
/* check logical sector size */
440-
p_pbr = exfat_read_pbr_with_logical_sector(sb);
441-
if (!p_pbr) {
440+
p_boot = exfat_read_boot_with_logical_sector(sb);
441+
if (!p_boot) {
442442
ret = -EIO;
443443
goto free_bh;
444444
}
@@ -447,43 +447,43 @@ static int __exfat_fill_super(struct super_block *sb)
447447
* res_zero field must be filled with zero to prevent mounting
448448
* from FAT volume.
449449
*/
450-
if (memchr_inv(p_pbr->bpb.f64.res_zero, 0,
451-
sizeof(p_pbr->bpb.f64.res_zero))) {
450+
if (memchr_inv(p_boot->must_be_zero, 0,
451+
sizeof(p_boot->must_be_zero))) {
452452
ret = -EINVAL;
453453
goto free_bh;
454454
}
455455

456-
p_bpb = (struct pbr64 *)p_pbr;
457-
if (!p_bpb->bsx.num_fats) {
456+
p_boot = (struct boot_sector *)p_boot;
457+
if (!p_boot->num_fats) {
458458
exfat_err(sb, "bogus number of FAT structure");
459459
ret = -EINVAL;
460460
goto free_bh;
461461
}
462462

463-
sbi->sect_per_clus = 1 << p_bpb->bsx.sect_per_clus_bits;
464-
sbi->sect_per_clus_bits = p_bpb->bsx.sect_per_clus_bits;
463+
sbi->sect_per_clus = 1 << p_boot->sect_per_clus_bits;
464+
sbi->sect_per_clus_bits = p_boot->sect_per_clus_bits;
465465
sbi->cluster_size_bits = sbi->sect_per_clus_bits + sb->s_blocksize_bits;
466466
sbi->cluster_size = 1 << sbi->cluster_size_bits;
467-
sbi->num_FAT_sectors = le32_to_cpu(p_bpb->bsx.fat_length);
468-
sbi->FAT1_start_sector = le32_to_cpu(p_bpb->bsx.fat_offset);
469-
sbi->FAT2_start_sector = p_bpb->bsx.num_fats == 1 ?
467+
sbi->num_FAT_sectors = le32_to_cpu(p_boot->fat_length);
468+
sbi->FAT1_start_sector = le32_to_cpu(p_boot->fat_offset);
469+
sbi->FAT2_start_sector = p_boot->num_fats == 1 ?
470470
sbi->FAT1_start_sector :
471471
sbi->FAT1_start_sector + sbi->num_FAT_sectors;
472-
sbi->data_start_sector = le32_to_cpu(p_bpb->bsx.clu_offset);
473-
sbi->num_sectors = le64_to_cpu(p_bpb->bsx.vol_length);
472+
sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
473+
sbi->num_sectors = le64_to_cpu(p_boot->vol_length);
474474
/* because the cluster index starts with 2 */
475-
sbi->num_clusters = le32_to_cpu(p_bpb->bsx.clu_count) +
475+
sbi->num_clusters = le32_to_cpu(p_boot->clu_count) +
476476
EXFAT_RESERVED_CLUSTERS;
477477

478-
sbi->root_dir = le32_to_cpu(p_bpb->bsx.root_cluster);
478+
sbi->root_dir = le32_to_cpu(p_boot->root_cluster);
479479
sbi->dentries_per_clu = 1 <<
480480
(sbi->cluster_size_bits - DENTRY_SIZE_BITS);
481481

482-
sbi->vol_flag = le16_to_cpu(p_bpb->bsx.vol_flags);
482+
sbi->vol_flag = le16_to_cpu(p_boot->vol_flags);
483483
sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
484484
sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED;
485485

486-
if (le16_to_cpu(p_bpb->bsx.vol_flags) & VOL_DIRTY) {
486+
if (le16_to_cpu(p_boot->vol_flags) & VOL_DIRTY) {
487487
sbi->vol_flag |= VOL_DIRTY;
488488
exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
489489
}
@@ -517,7 +517,7 @@ static int __exfat_fill_super(struct super_block *sb)
517517
free_upcase_table:
518518
exfat_free_upcase_table(sbi);
519519
free_bh:
520-
brelse(sbi->pbr_bh);
520+
brelse(sbi->boot_bh);
521521
return ret;
522522
}
523523

@@ -608,7 +608,7 @@ static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
608608
free_table:
609609
exfat_free_upcase_table(sbi);
610610
exfat_free_bitmap(sbi);
611-
brelse(sbi->pbr_bh);
611+
brelse(sbi->boot_bh);
612612

613613
check_nls_io:
614614
unload_nls(sbi->nls_io);

0 commit comments

Comments
 (0)