uboot 对 FAT 分区的解析

uboot 对 FAT 分区的解析

改写 UBOOT 从 U 盘读入固件,然后刷机。发现有的 U 盘无法正确读到分区,跟踪了一下发现自己写的代码有漏洞,只尝试解析分区表里的第一个分区。跟踪的过程中重温了一下 MBR 的格式,这里记录一下 UBOOT 对其中分区表的解析。

MBR

主引导记录(Master Boot Record),位于存储介质的第一个扇区。存储介质可能是硬盘、U 盘或 SD 卡等。

MBR 存在第一个扇区里,总共有 512 字节,其包括三个部分:

  1. 启动代码,前 446 个字节 (0,0x1bd]。一般 U 盘或 SD 卡不作启动用,这段空间的内容无意义。
  2. 分区表 DPT(Disk Partition Table),64 字节 [0x1be, 0x1fd]
  3. 结束标志,2 字节 [0x1fe, 0x1ff],0x55aa,用于标志 MBR 是否有效

分区表

每个分区信息占 16 字节,64 字节的分区表最多可以描述四个分区。如果想划分更多的分区,则要使用到扩展分区。

16 字节的分区信息意义为:

typedef struct dos_partition {
	unsigned char boot_ind;		/* 0x80 - active			*/
	unsigned char head;		/* starting head			*/
	unsigned char sector;		/* starting sector			*/
	unsigned char cyl;		/* starting cylinder			*/
	unsigned char sys_ind;		/* What partition type			*/
	unsigned char end_head;		/* end head				*/
	unsigned char end_sector;	/* end sector				*/
	unsigned char end_cyl;		/* end cylinder				*/
	unsigned char start4[4];	/* starting sector counting from 0	*/
	unsigned char size4[4];		/* nr of sectors in partition		*/
} dos_partition_t;
  • boot_ind, 0x80 表示活动分区,0x00 为非活动分区。
  • head,分区起始磁头号
  • sector,分区起始扇区号
  • cyl,分区起始柱面号
  • sys_ind,分区格式,各值的意义见下图
  • end_head,结束磁头号
  • end_sector,结束扇区号
  • end_cyl,结束柱面号
  • start4,分区起始相对扇区号,从磁盘开始到该分区的第一个扇区的偏移量,以扇区数为单位
  • size4,分区的总扇区数

如果分区表项指定的分区一般称为主分区。另外,分区表项还可以指定分区类型为扩展分区 0x7,这样该表项描述的那个分区的起始扇区中还包括一个分区表。

FAT 分区

UBOOT 中按以下条件判断一个分区为 FAT 分区:

  • 分区结束标志 0x55aa
  • 分区的第一个扇区中有 FAT 标志,0x36 字节处有 ”FAT“ 字符,或 0x52 字节处有 “FAT32” 字符
原文地址:https://www.cnblogs.com/sammei/p/4261404.html