android系统移植emmc记录(4)-- fdisk 分区命令分析 .
fdisk -c 0
命令分析:
----------------------------------
- int do_fdisk(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
- {
- if ( argc != 3 )
- {
- printf("Usage: fdisk <-c or -p> <device_num>\n");
- return 0;
- }
- //创建分区
- if ( strcmp(argv[1], "-c") == 0 )
- return create_mmc_fdisk(argc, argv);
- //查看分区
- else if ( strcmp(argv[1], "-p") == 0 )
- return print_mmc_part_info(argc, argv);
- printf("Usage: fdisk <-c or -p> <device_num>\n");
- return 0;
- }
- ----------------------------------------------------------
- int create_mmc_fdisk(int argc, char *argv[])
- {
- int rv;
- int total_block_count;
- unsigned char mbr[512];
- memset(mbr, 0x00, 512);
- //根据指定的0,获取总快数,这里可以知道了 fdisk -c 0 ,指的第一个注册的设备。是0节点,
- total_block_count = get_mmc_block_count(argv[2]);
- ---------------------------
- int get_mmc_block_count(char *device_name)
- {
- int rv;
- struct mmc *mmc;
- int block_count = 0;
- int dev_num;
- //这里把字符 0 转换成了整型 0了
- dev_num = simple_strtoul(device_name, NULL, 0);
- mmc = find_mmc_device(dev_num);
- //再次初始化一次
- rv = mmc_init(mmc);
- if (rv)
- {
- printf("mmc/sd device's initialization is failed.\n");
- return -1;
- }
- //emmc/sd 总容量大小,为块大小的倍数 .
- //修改此处,因整个分区体系就是以一个块大小为512来分区的。
- //BLOCK_SIZE = 512
- //所以对于有的emmc块大小是1024的。应增大它的容量,以免
- //对一个块使用不完全,就跳到下一个块过去了。
- block_count = mmc->capacity *(mmc -> read_bl_len / BLOCK_SIZE)
- return block_count;
- -----------------------------------------
int do_fdisk(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { if ( argc != 3 ) { printf("Usage: fdisk <-c or -p> <device_num>\n"); return 0; } //创建分区 if ( strcmp(argv[1], "-c") == 0 ) return create_mmc_fdisk(argc, argv); //查看分区 else if ( strcmp(argv[1], "-p") == 0 ) return print_mmc_part_info(argc, argv); printf("Usage: fdisk <-c or -p> <device_num>\n"); return 0; } ---------------------------------------------------------- int create_mmc_fdisk(int argc, char *argv[]) { int rv; int total_block_count; unsigned char mbr[512]; memset(mbr, 0x00, 512); //根据指定的0,获取总快数,这里可以知道了 fdisk -c 0 ,指的第一个注册的设备。是0节点, total_block_count = get_mmc_block_count(argv[2]); --------------------------- int get_mmc_block_count(char *device_name) { int rv; struct mmc *mmc; int block_count = 0; int dev_num; //这里把字符 0 转换成了整型 0了 dev_num = simple_strtoul(device_name, NULL, 0); mmc = find_mmc_device(dev_num); //再次初始化一次 rv = mmc_init(mmc); if (rv) { printf("mmc/sd device's initialization is failed.\n"); return -1; } //emmc/sd 总容量大小,为块大小的倍数 . //修改此处,因整个分区体系就是以一个块大小为512来分区的。 //BLOCK_SIZE = 512 //所以对于有的emmc块大小是1024的。应增大它的容量,以免 //对一个块使用不完全,就跳到下一个块过去了。 block_count = mmc->capacity *(mmc -> read_bl_len / BLOCK_SIZE) return block_count; -----------------------------------------
//创建分区列表MBR
make_mmc_partition(total_block_count, mbr);
//吧分区列表MBR写入节点设备中去
rv = put_mmc_mbr(mbr, argv[2]);
------------------
//创建分区列表MBR:
=======================================
- int make_mmc_partition(int total_block_count, unsigned char *mbr)
- {
- int block_start = 0, block_offset;
- SDInfo sdInfo;
- PartitionInfo partInfo[4];
- ///////////////////////////////////////////////////////////
- memset((unsigned char *)&sdInfo, 0x00, sizeof(SDInfo));
- ///////////////////////////////////////////////////////////
- //获取sd柱面,磁头,扇区的起始地址和结束地址
- get_SDInfo(total_block_count, &sdInfo);
- ///////////////////////////////////////////////////////////
- // 跳过前10M,作为系统分区起始地址
- //计算 从第几块开始,10MB/512
- block_start = calc_unit(_10MB, sdInfo);
- //空间大小为256MB,system分区
- block_offset = calc_unit(SYSTEM_PART_SIZE, sdInfo);
- partInfo[0].bootable = 0x00;
- partInfo[0].partitionId = 0x83;
- //开始设置分区,赋值partInfo【0】,会跳过前面10M的空间,用于u-boot,kernel ramdisk
- make_partitionInfo(block_start, block_offset, sdInfo, &partInfo[0]);
- ///////////////////////////////////////////////////////////
- block_start += block_offset;
- //350M datauser 分区
- block_offset = calc_unit(USER_DATA_PART_SIZE, sdInfo);
- partInfo[1].bootable = 0x00;
- partInfo[1].partitionId = 0x83;
- make_partitionInfo(block_start, block_offset, sdInfo, &partInfo[1]);
- ///////////////////////////////////////////////////////////
- block_start += block_offset;
- //cache 分区 100M
- block_offset = calc_unit(CACHE_PART_SIZE, sdInfo);
- partInfo[2].bootable = 0x00;
- partInfo[2].partitionId = 0x83;
- make_partitionInfo(block_start, block_offset, sdInfo, &partInfo[2]);
- ///////////////////////////////////////////////////////////
- //剩余容量 vfat分区,以0c为id。
- block_start += block_offset;
- block_offset = BLOCK_END;
- partInfo[3].bootable = 0x00;
- partInfo[3].partitionId = 0x0C;
- make_partitionInfo(block_start, block_offset, sdInfo, &partInfo[3]);
- ///////////////////////////////////////////////////////////
- memset(mbr, 0x00, sizeof(mbr));
- //初始MBR的标志,默认是55AA
- //[1FE] [1FF]
- mbr[510] = 0x55; mbr[511] = 0xAA;
- //MBR中:1BE-1CD:记录分区表1 ,1CE-1DD :记录分区表2
- // 1DE-1ED : 分区3 1EE-1FD : 分区表4 分区表 5 不能创建,1FE 需加55AA
- //每次 + F
- //system分区:分区表2,所以partInfo[0]表示分区表2,
- //设置DPT结构
- //把每个分区的起始地址和结束地址写入到MBR中。
- encode_partitionInfo(partInfo[0], &mbr[0x1CE]);
- //datause分区,partInfo[1]:表示分区表3
- encode_partitionInfo(partInfo[1], &mbr[0x1DE]);
- //cache分区,partInfo[2]:表示分区表4
- encode_partitionInfo(partInfo[2], &mbr[0x1EE]);
- //剩余空间:partInfo[3] 表示分区表1
- encode_partitionInfo(partInfo[3], &mbr[0x1BE]);
- return 0;
- }
int make_mmc_partition(int total_block_count, unsigned char *mbr) { int block_start = 0, block_offset; SDInfo sdInfo; PartitionInfo partInfo[4]; /////////////////////////////////////////////////////////// memset((unsigned char *)&sdInfo, 0x00, sizeof(SDInfo)); /////////////////////////////////////////////////////////// //获取sd柱面,磁头,扇区的起始地址和结束地址 get_SDInfo(total_block_count, &sdInfo); /////////////////////////////////////////////////////////// // 跳过前10M,作为系统分区起始地址 //计算 从第几块开始,10MB/512 block_start = calc_unit(_10MB, sdInfo); //空间大小为256MB,system分区 block_offset = calc_unit(SYSTEM_PART_SIZE, sdInfo); partInfo[0].bootable = 0x00; partInfo[0].partitionId = 0x83; //开始设置分区,赋值partInfo【0】,会跳过前面10M的空间,用于u-boot,kernel ramdisk make_partitionInfo(block_start, block_offset, sdInfo, &partInfo[0]); /////////////////////////////////////////////////////////// block_start += block_offset; //350M datauser 分区 block_offset = calc_unit(USER_DATA_PART_SIZE, sdInfo); partInfo[1].bootable = 0x00; partInfo[1].partitionId = 0x83; make_partitionInfo(block_start, block_offset, sdInfo, &partInfo[1]); /////////////////////////////////////////////////////////// block_start += block_offset; //cache 分区 100M block_offset = calc_unit(CACHE_PART_SIZE, sdInfo); partInfo[2].bootable = 0x00; partInfo[2].partitionId = 0x83; make_partitionInfo(block_start, block_offset, sdInfo, &partInfo[2]); /////////////////////////////////////////////////////////// //剩余容量 vfat分区,以0c为id。 block_start += block_offset; block_offset = BLOCK_END; partInfo[3].bootable = 0x00; partInfo[3].partitionId = 0x0C; make_partitionInfo(block_start, block_offset, sdInfo, &partInfo[3]); /////////////////////////////////////////////////////////// memset(mbr, 0x00, sizeof(mbr)); //初始MBR的标志,默认是55AA //[1FE] [1FF] mbr[510] = 0x55; mbr[511] = 0xAA; //MBR中:1BE-1CD:记录分区表1 ,1CE-1DD :记录分区表2 // 1DE-1ED : 分区3 1EE-1FD : 分区表4 分区表 5 不能创建,1FE 需加55AA //每次 + F //system分区:分区表2,所以partInfo[0]表示分区表2, //设置DPT结构 //把每个分区的起始地址和结束地址写入到MBR中。 encode_partitionInfo(partInfo[0], &mbr[0x1CE]); //datause分区,partInfo[1]:表示分区表3 encode_partitionInfo(partInfo[1], &mbr[0x1DE]); //cache分区,partInfo[2]:表示分区表4 encode_partitionInfo(partInfo[2], &mbr[0x1EE]); //剩余空间:partInfo[3] 表示分区表1 encode_partitionInfo(partInfo[3], &mbr[0x1BE]); return 0; }
- --------------------
- make_partitionInfo
- ---------------------
- //计算每个分区的起始地址和结束地址
- void make_partitionInfo(int LBA_start, int count, SDInfo sdInfo, PartitionInfo *partInfo)
- {
- int temp = 0;
- int _10MB_unit;
- //第一次跳过前10M
- partInfo->block_start = LBA_start;
- //-----------------------------------------------------
- if (sdInfo.addr_mode == CHS_MODE)
- { //块总数 = c * H * s 而SD卡容量 = 块总数 * 512byte
- //所以,块的起始地址
- partInfo->C_start = partInfo->block_start / (sdInfo.H_end * sdInfo.S_end);
- temp = partInfo->block_start % (sdInfo.H_end * sdInfo.S_end);
- partInfo->H_start = temp / sdInfo.S_end;
- partInfo->S_start = temp % sdInfo.S_end + 1;
- if (count == BLOCK_END)
- { //剩余所有空间
- _10MB_unit = calc_unit(_10MB, sdInfo);
- partInfo->block_end = sdInfo.C_end * sdInfo.H_end * sdInfo.S_end - _10MB_unit - 1;
- partInfo->block_count = partInfo->block_end - partInfo->block_start + 1;
- partInfo->C_end = partInfo->block_end / sdInfo.unit;
- partInfo->H_end = sdInfo.H_end - 1;
- partInfo->S_end = sdInfo.S_end;
- }
- else
- {
- partInfo->block_count = count;
- partInfo->block_end = partInfo->block_start + count - 1;
- partInfo->C_end = partInfo->block_end / sdInfo.unit;
- temp = partInfo->block_end % sdInfo.unit;
- partInfo->H_end = temp / sdInfo.S_end;
- partInfo->S_end = temp % sdInfo.S_end + 1;
- }
- }
- //-----------------------------------------------------
- else
- {
- partInfo->C_start = 0;
- partInfo->H_start = 1;
- partInfo->S_start = 1;
- partInfo->C_end = 1023;
- partInfo->H_end = 254;
- partInfo->S_end = 63;
- if (count == BLOCK_END)
- {
- _10MB_unit = calc_unit(_10MB, sdInfo);
- partInfo->block_end = sdInfo.total_block_count - _10MB_unit - 1;
- partInfo->block_count = partInfo->block_end - partInfo->block_start + 1;
- }
- else
- {
- partInfo->block_count = count;
- partInfo->block_end = partInfo->block_start + count - 1;
- }
- }
- }
- ======================================================================
-------------------- make_partitionInfo --------------------- //计算每个分区的起始地址和结束地址 void make_partitionInfo(int LBA_start, int count, SDInfo sdInfo, PartitionInfo *partInfo) { int temp = 0; int _10MB_unit; //第一次跳过前10M partInfo->block_start = LBA_start; //----------------------------------------------------- if (sdInfo.addr_mode == CHS_MODE) { //块总数 = c * H * s 而SD卡容量 = 块总数 * 512byte //所以,块的起始地址 partInfo->C_start = partInfo->block_start / (sdInfo.H_end * sdInfo.S_end); temp = partInfo->block_start % (sdInfo.H_end * sdInfo.S_end); partInfo->H_start = temp / sdInfo.S_end; partInfo->S_start = temp % sdInfo.S_end + 1; if (count == BLOCK_END) { //剩余所有空间 _10MB_unit = calc_unit(_10MB, sdInfo); partInfo->block_end = sdInfo.C_end * sdInfo.H_end * sdInfo.S_end - _10MB_unit - 1; partInfo->block_count = partInfo->block_end - partInfo->block_start + 1; partInfo->C_end = partInfo->block_end / sdInfo.unit; partInfo->H_end = sdInfo.H_end - 1; partInfo->S_end = sdInfo.S_end; } else { partInfo->block_count = count; partInfo->block_end = partInfo->block_start + count - 1; partInfo->C_end = partInfo->block_end / sdInfo.unit; temp = partInfo->block_end % sdInfo.unit; partInfo->H_end = temp / sdInfo.S_end; partInfo->S_end = temp % sdInfo.S_end + 1; } } //----------------------------------------------------- else { partInfo->C_start = 0; partInfo->H_start = 1; partInfo->S_start = 1; partInfo->C_end = 1023; partInfo->H_end = 254; partInfo->S_end = 63; if (count == BLOCK_END) { _10MB_unit = calc_unit(_10MB, sdInfo); partInfo->block_end = sdInfo.total_block_count - _10MB_unit - 1; partInfo->block_count = partInfo->block_end - partInfo->block_start + 1; } else { partInfo->block_count = count; partInfo->block_end = partInfo->block_start + count - 1; } } } ======================================================================