@@ -366,25 +366,20 @@ static int exfat_read_root(struct inode *inode)
366
366
return 0 ;
367
367
}
368
368
369
- static struct boot_sector * exfat_read_boot_with_logical_sector (
370
- struct super_block * sb )
369
+ static int exfat_calibrate_blocksize (struct super_block * sb , int logical_sect )
371
370
{
372
371
struct exfat_sb_info * sbi = EXFAT_SB (sb );
373
- struct boot_sector * p_boot = (struct boot_sector * )sbi -> boot_bh -> b_data ;
374
- unsigned short logical_sect = 0 ;
375
-
376
- logical_sect = 1 << p_boot -> sect_size_bits ;
377
372
378
373
if (!is_power_of_2 (logical_sect ) ||
379
374
logical_sect < 512 || logical_sect > 4096 ) {
380
375
exfat_err (sb , "bogus logical sector size %u" , logical_sect );
381
- return NULL ;
376
+ return - EIO ;
382
377
}
383
378
384
379
if (logical_sect < sb -> s_blocksize ) {
385
380
exfat_err (sb , "logical sector size too small for device (logical sector size = %u)" ,
386
381
logical_sect );
387
- return NULL ;
382
+ return - EIO ;
388
383
}
389
384
390
385
if (logical_sect > sb -> s_blocksize ) {
@@ -394,24 +389,20 @@ static struct boot_sector *exfat_read_boot_with_logical_sector(
394
389
if (!sb_set_blocksize (sb , logical_sect )) {
395
390
exfat_err (sb , "unable to set blocksize %u" ,
396
391
logical_sect );
397
- return NULL ;
392
+ return - EIO ;
398
393
}
399
394
sbi -> boot_bh = sb_bread (sb , 0 );
400
395
if (!sbi -> boot_bh ) {
401
396
exfat_err (sb , "unable to read boot sector (logical sector size = %lu)" ,
402
397
sb -> s_blocksize );
403
- return NULL ;
398
+ return - EIO ;
404
399
}
405
-
406
- p_boot = (struct boot_sector * )sbi -> boot_bh -> b_data ;
407
400
}
408
- return p_boot ;
401
+ return 0 ;
409
402
}
410
403
411
- /* mount the file system volume */
412
- static int __exfat_fill_super (struct super_block * sb )
404
+ static int exfat_read_boot_sector (struct super_block * sb )
413
405
{
414
- int ret ;
415
406
struct boot_sector * p_boot ;
416
407
struct exfat_sb_info * sbi = EXFAT_SB (sb );
417
408
@@ -424,51 +415,41 @@ static int __exfat_fill_super(struct super_block *sb)
424
415
exfat_err (sb , "unable to read boot sector" );
425
416
return - EIO ;
426
417
}
427
-
428
- /* PRB is read */
429
418
p_boot = (struct boot_sector * )sbi -> boot_bh -> b_data ;
430
419
431
420
/* check the validity of BOOT */
432
421
if (le16_to_cpu ((p_boot -> signature )) != BOOT_SIGNATURE ) {
433
422
exfat_err (sb , "invalid boot record signature" );
434
- ret = - EINVAL ;
435
- goto free_bh ;
423
+ return - EINVAL ;
436
424
}
437
425
438
-
439
- /* check logical sector size */
440
- p_boot = exfat_read_boot_with_logical_sector (sb );
441
- if (!p_boot ) {
442
- ret = - EIO ;
443
- goto free_bh ;
426
+ if (memcmp (p_boot -> fs_name , STR_EXFAT , BOOTSEC_FS_NAME_LEN )) {
427
+ exfat_err (sb , "invalid fs_name" ); /* fs_name may unprintable */
428
+ return - EINVAL ;
444
429
}
445
430
446
431
/*
447
- * res_zero field must be filled with zero to prevent mounting
432
+ * must_be_zero field must be filled with zero to prevent mounting
448
433
* from FAT volume.
449
434
*/
450
- if (memchr_inv (p_boot -> must_be_zero , 0 ,
451
- sizeof (p_boot -> must_be_zero ))) {
452
- ret = - EINVAL ;
453
- goto free_bh ;
454
- }
435
+ if (memchr_inv (p_boot -> must_be_zero , 0 , sizeof (p_boot -> must_be_zero )))
436
+ return - EINVAL ;
455
437
456
- p_boot = (struct boot_sector * )p_boot ;
457
- if (!p_boot -> num_fats ) {
438
+ if (p_boot -> num_fats != 1 && p_boot -> num_fats != 2 ) {
458
439
exfat_err (sb , "bogus number of FAT structure" );
459
- ret = - EINVAL ;
460
- goto free_bh ;
440
+ return - EINVAL ;
461
441
}
462
442
463
443
sbi -> sect_per_clus = 1 << p_boot -> sect_per_clus_bits ;
464
444
sbi -> sect_per_clus_bits = p_boot -> sect_per_clus_bits ;
465
- sbi -> cluster_size_bits = sbi -> sect_per_clus_bits + sb -> s_blocksize_bits ;
445
+ sbi -> cluster_size_bits = p_boot -> sect_per_clus_bits +
446
+ p_boot -> sect_size_bits ;
466
447
sbi -> cluster_size = 1 << sbi -> cluster_size_bits ;
467
448
sbi -> num_FAT_sectors = le32_to_cpu (p_boot -> fat_length );
468
449
sbi -> FAT1_start_sector = le32_to_cpu (p_boot -> fat_offset );
469
- sbi -> FAT2_start_sector = p_boot -> num_fats == 1 ?
470
- sbi -> FAT1_start_sector :
471
- sbi -> FAT1_start_sector + sbi -> num_FAT_sectors ;
450
+ sbi -> FAT2_start_sector = le32_to_cpu ( p_boot -> fat_offset );
451
+ if ( p_boot -> num_fats == 2 )
452
+ sbi -> FAT2_start_sector += sbi -> num_FAT_sectors ;
472
453
sbi -> data_start_sector = le32_to_cpu (p_boot -> clu_offset );
473
454
sbi -> num_sectors = le64_to_cpu (p_boot -> vol_length );
474
455
/* because the cluster index starts with 2 */
@@ -483,15 +464,45 @@ static int __exfat_fill_super(struct super_block *sb)
483
464
sbi -> clu_srch_ptr = EXFAT_FIRST_CLUSTER ;
484
465
sbi -> used_clusters = EXFAT_CLUSTERS_UNTRACKED ;
485
466
486
- if (le16_to_cpu (p_boot -> vol_flags ) & VOL_DIRTY ) {
487
- sbi -> vol_flag |= VOL_DIRTY ;
488
- exfat_warn (sb , "Volume was not properly unmounted. Some data may be corrupt. Please run fsck." );
467
+ /* check consistencies */
468
+ if (sbi -> num_FAT_sectors << p_boot -> sect_size_bits <
469
+ sbi -> num_clusters * 4 ) {
470
+ exfat_err (sb , "bogus fat length" );
471
+ return - EINVAL ;
472
+ }
473
+ if (sbi -> data_start_sector <
474
+ sbi -> FAT1_start_sector + sbi -> num_FAT_sectors * p_boot -> num_fats ) {
475
+ exfat_err (sb , "bogus data start sector" );
476
+ return - EINVAL ;
489
477
}
478
+ if (sbi -> vol_flag & VOL_DIRTY )
479
+ exfat_warn (sb , "Volume was not properly unmounted. Some data may be corrupt. Please run fsck." );
480
+ if (sbi -> vol_flag & ERR_MEDIUM )
481
+ exfat_warn (sb , "Medium has reported failures. Some data may be lost." );
490
482
491
483
/* exFAT file size is limited by a disk volume size */
492
484
sb -> s_maxbytes = (u64 )(sbi -> num_clusters - EXFAT_RESERVED_CLUSTERS ) <<
493
485
sbi -> cluster_size_bits ;
494
486
487
+ /* check logical sector size */
488
+ if (exfat_calibrate_blocksize (sb , 1 << p_boot -> sect_size_bits ))
489
+ return - EIO ;
490
+
491
+ return 0 ;
492
+ }
493
+
494
+ /* mount the file system volume */
495
+ static int __exfat_fill_super (struct super_block * sb )
496
+ {
497
+ int ret ;
498
+ struct exfat_sb_info * sbi = EXFAT_SB (sb );
499
+
500
+ ret = exfat_read_boot_sector (sb );
501
+ if (ret ) {
502
+ exfat_err (sb , "failed to read boot sector" );
503
+ goto free_bh ;
504
+ }
505
+
495
506
ret = exfat_create_upcase_table (sb );
496
507
if (ret ) {
497
508
exfat_err (sb , "failed to load upcase table" );
0 commit comments