shaoshengli
驱动小牛
驱动小牛
  • 注册日期2002-06-24
  • 最后登录2022-02-14
  • 粉丝0
  • 关注0
  • 积分14分
  • 威望77点
  • 贡献值0点
  • 好评度2点
  • 原创分0分
  • 专家分0分
  • 社区居民
阅读:3261回复:6

mp3_____Format命令的代码(C语言)

楼主#
更多 发布于:2004-01-16 14:04
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Create single FAT12 or FAT16 partition and format the selected memory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   - Single partition
*   - Cluster size is 4 or 8 Kbytes
*   - Sector size is 512 bytes
*   - 2 fats management
*   - 512 entries in the root directory
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_format (void)
{
#define FORMAT_NB_CYLINDER            (*tab).nb_cylinder
#define FORMAT_NB_HEAD                (*tab).nb_head
#define FORMAT_NB_SECTOR              (*tab).nb_sector
#define FORMAT_NB_HIDDEN_SECTOR       (*tab).nb_hidden_sector
#define FORMAT_NB_SECTOR_PER_CLUSTER  (*tab).nb_sector_per_cluster

Byte i, j;
Uint32 nb_total_sectors;
Uint16 nb_sector_fat;
s_format *tab;

  tab = Hard_format();

  fat_cluster_size = FORMAT_NB_SECTOR_PER_CLUSTER;
  nb_total_sectors = (Uint32)FORMAT_NB_CYLINDER * FORMAT_NB_HEAD * FORMAT_NB_SECTOR;

  /* FAT type caculation */
  fat_is_fat16 = ((nb_total_sectors  / fat_cluster_size) > MAX_CLUSTERS12);

#if FAT_PARTITIONNED == TRUE
  /* -- MASTER BOOT RECORD -- */
  Hard_write_open(MBR_ADDRESS);
  for (i = 446/2; i != 0; i--)              /* Boot Code */
  {
    Hard_write_byte(0x00);
    Hard_write_byte(0x00);
  }

  /* First Partition entry */
  Hard_write_byte(0x80);                    /* Default Boot Partition */
  Hard_write_byte((Byte)(FORMAT_NB_HIDDEN_SECTOR / FORMAT_NB_SECTOR));       /* Start head */
  Hard_write_byte((Byte)((FORMAT_NB_HIDDEN_SECTOR % FORMAT_NB_SECTOR) + 1)); /* Start Sector */
  Hard_write_byte(0x00);                    /* Start Cylinder */
  if (fat_is_fat16)
  {
    if (nb_total_sectors > 0xFFFF)          /* Total Sectors */
    {
      Hard_write_byte(FAT16_SUP32M);        /* FAT16 > 32 Mbytes */
    }
    else
    {
      Hard_write_byte(FAT16_INF32M);        /* FAT16 < 32 Mbytes */
    }
  }
  else
  {
    Hard_write_byte(FAT12);                 /* FAT12 */
  }
  Hard_write_byte((Byte)(FORMAT_NB_HEAD - 1)); /* Endhead-Zero-based(0) head number */
  Hard_write_byte((Byte)((((FORMAT_NB_CYLINDER - 1) / 0x04) & 0xC0) +  FORMAT_NB_SECTOR));  
                                                            /* EndSector-Zero-based(1) sector number */
  Hard_write_byte((Byte)((FORMAT_NB_CYLINDER - 1) & 0xFF)); /* EndCylinder */
  Hard_write_byte((Byte)(FORMAT_NB_HIDDEN_SECTOR));         /* Start sector */
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  
  Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR)));
  Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR)>>8));
  Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR)>>16));
  Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR)>>24));

  /* Second Partition entry */
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);

  /* Third Partition entry */
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);

  /* Fourth Partition entry */
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  
  Hard_write_byte(0x55);                    /* Signature Word */
  Hard_write_byte(0xAA);

  /* -- HIDDEN SECTORS -- */
  for (j = FORMAT_NB_HIDDEN_SECTOR - 1; j != 0; j--)
  {
    for (i = (SECTOR_SIZE / 4); i != 0; i--)
    {
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
    }
  }
#else   /* fat not partitionned */
  Hard_write_open(MBR_ADDRESS);
#endif  /* FAT_PARTITIONNED == TRUE */

  /* -- PARTITION BOOT RECORD -- */
  Hard_write_byte(0xEB);                    /* JMP inst to PBR boot code */
  Hard_write_byte(0x3C);
  Hard_write_byte(0x90);
  Hard_write_byte(\'O\');                     /* OEM name */
  Hard_write_byte(\'E\');
  Hard_write_byte(\'M\');
  Hard_write_byte(\' \');
  Hard_write_byte(\'N\');
  Hard_write_byte(\'A\');
  Hard_write_byte(\'M\');
  Hard_write_byte(\'E\');
  Hard_write_byte(SECTOR_SIZE);             /* number of bytes per sector */
  Hard_write_byte(SECTOR_SIZE >> 8);
  Hard_write_byte(fat_cluster_size);        /* Number of sector per cluster */
  Hard_write_byte(NB_RESERVED);             /* number of reserved sector */
  Hard_write_byte(NB_RESERVED >> 8);
  Hard_write_byte(NB_FATS);                 /* Number of FAT */
  Hard_write_byte(NB_ROOT_ENTRY);           /* number of root directory entries */
  Hard_write_byte(NB_ROOT_ENTRY >> 8);
                                                            
  if (nb_total_sectors > 0xFFFF)            /* Total Sectors */
  {
    Hard_write_byte(0x00);
    Hard_write_byte(0x00);
  }
  else
  {
    Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR)));
    Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR) >> 8));
  }

  Hard_write_byte(HARD_DISK);               /* Media Byte */
  /* Number of sector in each FAT */
  if (fat_is_fat16)
    nb_sector_fat = (((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR) / fat_cluster_size) * 2 / SECTOR_SIZE) + 1;
  else
    nb_sector_fat = (((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR) / fat_cluster_size) * 3 / 1024) + 1;
  Hard_write_byte((Byte)(nb_sector_fat));
  Hard_write_byte((Byte)(nb_sector_fat >> 8));
  Hard_write_byte((Byte)(FORMAT_NB_SECTOR));              /* Number of sectors on a track */
  Hard_write_byte((Byte)(FORMAT_NB_SECTOR >> 8));
  Hard_write_byte((Byte)(FORMAT_NB_HEAD));                /* Number of heads */
  Hard_write_byte((Byte)(FORMAT_NB_HEAD >> 8));
  Hard_write_byte((Byte)(FORMAT_NB_HIDDEN_SECTOR));       /* Number of hidden sectors */
  Hard_write_byte((Byte)(FORMAT_NB_HIDDEN_SECTOR >> 8));
  Hard_write_byte((Byte)(FORMAT_NB_HIDDEN_SECTOR >> 16));
  Hard_write_byte((Byte)(FORMAT_NB_HIDDEN_SECTOR >> 24));
  if (nb_total_sectors > 0xFFFF)
  { /* number of sectors > 65535 */
    Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR)));
    Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR) >> 8));
    Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR) >> 16));
    Hard_write_byte((Byte)((nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR) >> 24));
  }
  else
  { /* number of sectors < 65535 */
    Hard_write_byte(0x00);
    Hard_write_byte(0x00);
    Hard_write_byte(0x00);
    Hard_write_byte(0x00);
  }
  Hard_write_byte(FAT_DRIVE_NUMBER);        /* Driver number */
  Hard_write_byte(0x00);                    /* not used */
  Hard_write_byte(FAT_EXT_SIGN);            /* extended boot signature */
  Hard_write_byte(0x00);                    /* volume ID */
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(0x00);
  Hard_write_byte(\'N\');                     /* Volume Label */
  Hard_write_byte(\'O\');
  Hard_write_byte(\' \');
  Hard_write_byte(\'N\');
  Hard_write_byte(\'A\');
  Hard_write_byte(\'M\');
  Hard_write_byte(\'E\');
  Hard_write_byte(\' \');
  Hard_write_byte(\' \');
  Hard_write_byte(\' \');
  Hard_write_byte(\' \');
  Hard_write_byte(\'F\');                     /* File System Type in ASCII */
  Hard_write_byte(\'A\');
  Hard_write_byte(\'T\');
  Hard_write_byte(\'1\');
  if (fat_is_fat16)
  {
    Hard_write_byte(\'6\');
  }
  else
  {
    Hard_write_byte(\'2\');
  }
  Hard_write_byte(\' \');
  Hard_write_byte(\' \');
  Hard_write_byte(\' \');
  for (i = 448/2; i != 0; i--)              /* Boot Code */
  {
    Hard_write_byte(0x00);
    Hard_write_byte(0x00);
  }
  Hard_write_byte(0x55);                    /* Signature word */
  Hard_write_byte(0xAA);
  
  /* -- FATS -- */
  /* -- FAT 1 -- */
  Hard_write_byte(0xF8);                    /* reserved clusters 0 & 1 */
  Hard_write_byte(0xFF);
  Hard_write_byte(0xFF);
  if (fat_is_fat16)
  { /* FAT16 */
    Hard_write_byte(0xFF);
  }
  else
  { /* FAT12 */
    Hard_write_byte(0x00);
  }
  /* free clusters in first FAT sector */
  for (i = (SECTOR_SIZE - 4) / 2; i != 0; i--)
  {
    Hard_write_byte(0x00);
    Hard_write_byte(0x00);
  }
  /* free clusters in other FAT sectors */
  for (j = nb_sector_fat - 1; j != 0; j--)
  {
    for (i = SECTOR_SIZE / 8; i != 0; i--)
    {
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
    }
  }

  /* -- FAT 2 -- */
  Hard_write_byte(0xF8);                    /* reserved clusters 0 & 1 */
  Hard_write_byte(0xFF);
  Hard_write_byte(0xFF);
  if (fat_is_fat16)
  { /* FAT16 */
    Hard_write_byte(0xFF);
  }
  else
  { /* FAT12 */
    Hard_write_byte(0x00);
  }
  /* free clusters in first FAT sector */
  for (i = (SECTOR_SIZE - 4) / 2; i != 0; i--)
  {
    Hard_write_byte(0x00);
    Hard_write_byte(0x00);
  }
  /* free clusters in other FAT sectors */
  for (j = nb_sector_fat - 1; j != 0; j--)
  {
    for (i = SECTOR_SIZE / 8; i != 0; i--)
    {
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
    }
  }


  /* -- ROOT DIRECTORY ENTRIES -- */
  for (j = NB_ROOT_ENTRY / 4; j != 0 ; j--)
  {
    for (i = DIR_SIZE; i != 0; i--)
    {
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
      Hard_write_byte(0x00);
    }
  }
  Hard_write_close();
}

最新喜欢:

lixinli81lixinl... chinaleichinal...
rayyang2000
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2012-09-13
  • 粉丝3
  • 关注0
  • 积分1036分
  • 威望925点
  • 贡献值3点
  • 好评度823点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-01-28 13:04
是实际使用的代码吗?
我先置顶,让兄弟们参阅参阅。
天天coding-debugging中----超稀饭memory dump file ======================================================== [b]Windows Device Driver Development and Consulting Service[/b] [color=blue][url]http://www.ybwork.com[/url][/color] ========================================================
rkt_001
驱动牛犊
驱动牛犊
  • 注册日期2002-11-19
  • 最后登录2004-03-27
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-01-28 16:47
受益非浅,
zhanghb
驱动牛犊
驱动牛犊
  • 注册日期2004-02-11
  • 最后登录2009-10-11
  • 粉丝0
  • 关注0
  • 积分8分
  • 威望78点
  • 贡献值0点
  • 好评度7点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-02-14 10:09
楼主,我也实现了部分功能,我们能否相互交流一下
zhanghuanlin@freemail.soim.com
shaoshengli
驱动小牛
驱动小牛
  • 注册日期2002-06-24
  • 最后登录2022-02-14
  • 粉丝0
  • 关注0
  • 积分14分
  • 威望77点
  • 贡献值0点
  • 好评度2点
  • 原创分0分
  • 专家分0分
  • 社区居民
地下室#
发布于:2004-02-14 11:00
以上代码是从Atmel范例里剪切下来的,大家放心使用,绝对OK!!!
lujunql
驱动小牛
驱动小牛
  • 注册日期2004-06-09
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分21分
  • 威望3点
  • 贡献值0点
  • 好评度2点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2004-06-28 15:43
好东西啊!
flyhye
驱动小牛
驱动小牛
  • 注册日期2003-07-01
  • 最后登录2012-01-11
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望2点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
6楼#
发布于:2004-06-28 18:49
 丁页!
我一般不会这样处理,通常是将一些信息直接放在某数组中,:)
千载奇逢,无如好书良友 一生清福,只在碗茗炉烟
游客

返回顶部