📄 partition.c
字号:
/****************************************Copyright (c)**************************************************
** 思 蜕 盟 豆 皮 开 发 小 组
** stmfans 论坛
**
** QQ 群: 65081316 StmFans思蜕盟 1组
** QQ 群: 68584951 StmFans思蜕盟 2组
** http://www.stmfans.com/bbs/
**
** This program was produced by the
** IAR Embedded Workbench 4.0 Kickstart 442
** Copyright 2008-2009 stmfans
** Chip type : STM32F103VB
** Program type : Application
** Clock frequency : 8.000000 MHz
** Memory model :
** External SRAM size :
** Data Stack size :
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: partition.c
**创 建 人: 陈海
**最后修改日期: 2008年10月23日
**描 述: 豆皮开发板教程
**
**--------------历史版本信息----------------------------------------------------------------------------
** 创建人: Roland Riegel
** 版 本: v0.01
** 日 期: 2008年06月08日
** 描 述: 原始版本
**
**--------------当前版本信息----------------------------------------------------------------------------
** 创建人: 陈海
** 版 本: v0.02
** 日 期: 2008年10月23日
** 描 述: 当前版本
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "partition.h"
#include <string.h>
#if USE_DYNAMIC_MEMORY
#include <stdlib.h>
#endif
/**
* \addtogroup partition Partition table support
*
* Support for reading partition tables and access to partitions.
*
* @{
*/
/**
* \file
* Partition table implementation (license: GPLv2 or LGPLv2.1)
*
* \author Roland Riegel
*/
/**
* \addtogroup partition_config Configuration of partition table support
* Preprocessor defines to configure the partition support.
*/
#if !USE_DYNAMIC_MEMORY
static struct partition_struct partition_handles[PARTITION_COUNT];
#endif
/**
* Opens a partition.
*
* Opens a partition by its index number and returns a partition
* handle which describes the opened partition.
*
* \note This function does not support extended partitions.
*
* \param[in] device_read A function pointer which is used to read from the disk.
* \param[in] device_read_interval A function pointer which is used to read in constant intervals from the disk.
* \param[in] device_write A function pointer which is used to write to the disk.
* \param[in] device_write_interval A function pointer which is used to write a data stream to disk.
* \param[in] index The index of the partition which should be opened, range 0 to 3.
* A negative value is allowed as well. In this case, the partition opened is
* not checked for existance, begins at offset zero, has a length of zero
* and is of an unknown type.
* \returns 0 on failure, a partition descriptor on success.
* \see partition_close
*/
struct partition_struct* partition_open(device_read_t device_read, device_read_interval_t device_read_interval, device_write_t device_write, device_write_interval_t device_write_interval, s8 index)
{
struct partition_struct* new_partition = 0;
u8 buffer[0x10];
if(!device_read || !device_read_interval || index >= 4)
return 0;
if(index >= 0)
{
/* read specified partition table index */
if(!device_read(0x01be + index * 0x10, buffer, sizeof(buffer)))
return 0;
/* abort on empty partition entry */
if(buffer[4] == 0x00)
return 0;
}
/* allocate partition descriptor */
#if USE_DYNAMIC_MEMORY
new_partition = malloc(sizeof(*new_partition));
if(!new_partition)
return 0;
#else
new_partition = partition_handles;
u8 i;
for(i = 0; i < PARTITION_COUNT; ++i)
{
if(new_partition->type == PARTITION_TYPE_FREE)
break;
++new_partition;
}
if(i >= PARTITION_COUNT)
return 0;
#endif
memset(new_partition, 0, sizeof(*new_partition));
/* fill partition descriptor */
new_partition->device_read = device_read;
new_partition->device_read_interval = device_read_interval;
new_partition->device_write = device_write;
new_partition->device_write_interval = device_write_interval;
if(index >= 0)
{
new_partition->type = buffer[4];
new_partition->offset = ((u32) buffer[8]) |
((u32) buffer[9] << 8) |
((u32) buffer[10] << 16) |
((u32) buffer[11] << 24);
new_partition->length = ((u32) buffer[12]) |
((u32) buffer[13] << 8) |
((u32) buffer[14] << 16) |
((u32) buffer[15] << 24);
}
else
{
new_partition->type = 0xff;
}
return new_partition;
}
/**
* Closes a partition.
*
* This function destroys a partition descriptor which was
* previously obtained from a call to partition_open().
* When this function returns, the given descriptor will be
* invalid.
*
* \param[in] partition The partition descriptor to destroy.
* \returns 0 on failure, 1 on success.
* \see partition_open
*/
u8 partition_close(struct partition_struct* partition)
{
if(!partition)
return 0;
/* destroy partition descriptor */
#if USE_DYNAMIC_MEMORY
free(partition);
#else
partition->type = PARTITION_TYPE_FREE;
#endif
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -