2018-08-28 15:30:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/partition/fs_partition.c
|
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-08-28 15:30:21 +02:00
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-08-28 15:30:21 +02:00
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-08-28 15:30:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
2023-03-10 01:20:48 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2018-08-28 15:30:21 +02:00
|
|
|
#include "partition.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
typedef CODE int
|
|
|
|
(*partition_parser_t)(FAR struct partition_state_s *state,
|
|
|
|
partition_handler_t handler,
|
|
|
|
FAR void *arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int parse_partition(FAR struct partition_state_s *state,
|
|
|
|
partition_handler_t handler,
|
|
|
|
FAR void *arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const partition_parser_t g_parser[] =
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_PTABLE_PARTITION
|
|
|
|
parse_ptable_partition,
|
|
|
|
#endif
|
2021-09-29 16:45:24 +02:00
|
|
|
|
2021-10-11 04:48:44 +02:00
|
|
|
#ifdef CONFIG_GPT_PARTITION
|
|
|
|
parse_gpt_partition,
|
|
|
|
#endif
|
|
|
|
|
2021-09-29 16:45:24 +02:00
|
|
|
#ifdef CONFIG_MBR_PARTITION
|
|
|
|
parse_mbr_partition,
|
|
|
|
#endif
|
|
|
|
|
2018-08-28 15:30:21 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: parse_partition
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* parse the partition table.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* state - The partition table state
|
|
|
|
* handler - The function to be called for each found partition
|
|
|
|
* arg - A caller provided value to return with the handler
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success; A negated errno value is returned on a failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int parse_partition(FAR struct partition_state_s *state,
|
|
|
|
partition_handler_t handler,
|
|
|
|
FAR void *arg)
|
|
|
|
{
|
2019-03-01 22:00:00 +01:00
|
|
|
int i;
|
|
|
|
int ret = 0;
|
2018-08-28 15:30:21 +02:00
|
|
|
|
|
|
|
for (i = 0; g_parser[i] != NULL; i++)
|
|
|
|
{
|
|
|
|
ret = g_parser[i](state, handler, arg);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-09-29 19:02:27 +02:00
|
|
|
int read_partition_block(FAR struct partition_state_s *state,
|
|
|
|
FAR void *buffer, size_t startblock,
|
|
|
|
size_t nblocks)
|
|
|
|
{
|
|
|
|
if (state->blk)
|
|
|
|
{
|
|
|
|
return state->blk->u.i_bops->read(state->blk,
|
|
|
|
buffer, startblock, nblocks);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return state->mtd->bread(state->mtd, startblock, nblocks, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 15:30:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: parse_block_partition
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* parse the partition table on a block device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* path - The block device to be parsed
|
|
|
|
* handler - The function to be called for each found partition
|
|
|
|
* arg - A caller provided value to return with the handler
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success; A negated errno value is returned on a failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int parse_block_partition(FAR const char *path,
|
|
|
|
partition_handler_t handler,
|
|
|
|
FAR void *arg)
|
|
|
|
{
|
|
|
|
struct partition_state_s state;
|
2018-11-08 16:41:54 +01:00
|
|
|
struct mtd_geometry_s mgeo;
|
2018-08-28 15:30:21 +02:00
|
|
|
struct geometry geo;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = open_blockdriver(path, MS_RDONLY, &state.blk);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:41:54 +01:00
|
|
|
state.mtd = NULL;
|
|
|
|
|
2022-10-13 10:47:28 +02:00
|
|
|
if (state.blk->u.i_bops->ioctl != NULL &&
|
|
|
|
state.blk->u.i_bops->ioctl(state.blk, MTDIOC_GEOMETRY,
|
|
|
|
(unsigned long)(uintptr_t)&mgeo) >= 0)
|
2018-08-28 15:30:21 +02:00
|
|
|
{
|
2022-10-13 10:47:28 +02:00
|
|
|
DEBUGASSERT(mgeo.blocksize);
|
|
|
|
|
2018-11-08 16:41:54 +01:00
|
|
|
state.blocksize = mgeo.blocksize;
|
|
|
|
state.erasesize = mgeo.erasesize;
|
|
|
|
state.nblocks = mgeo.neraseblocks;
|
|
|
|
state.nblocks *= mgeo.erasesize / mgeo.blocksize;
|
2018-08-28 15:30:21 +02:00
|
|
|
|
|
|
|
ret = parse_partition(&state, handler, arg);
|
|
|
|
}
|
2018-11-08 16:41:54 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = state.blk->u.i_bops->geometry(state.blk, &geo);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2022-10-13 10:47:28 +02:00
|
|
|
DEBUGASSERT(geo.geo_sectorsize);
|
|
|
|
|
2018-11-08 16:41:54 +01:00
|
|
|
state.blocksize = geo.geo_sectorsize;
|
|
|
|
state.erasesize = geo.geo_sectorsize;
|
|
|
|
state.nblocks = geo.geo_nsectors;
|
|
|
|
|
|
|
|
ret = parse_partition(&state, handler, arg);
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 15:30:21 +02:00
|
|
|
|
|
|
|
close_blockdriver(state.blk);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: parse_mtd_partition
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* parse the partition table on a mtd device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mtd - The MTD device to be parsed
|
|
|
|
* handler - The function to be called for each found partition
|
|
|
|
* arg - A caller provided value to return with the handler
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success; A negated errno value is returned on a failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int parse_mtd_partition(FAR struct mtd_dev_s *mtd,
|
|
|
|
partition_handler_t handler,
|
|
|
|
FAR void *arg)
|
|
|
|
{
|
|
|
|
struct partition_state_s state;
|
2018-11-08 16:41:54 +01:00
|
|
|
struct mtd_geometry_s mgeo;
|
2018-08-28 15:30:21 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-07-19 18:53:22 +02:00
|
|
|
ret = mtd->ioctl(mtd, MTDIOC_GEOMETRY, (unsigned long)(uintptr_t)&mgeo);
|
2018-08-28 15:30:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:47:28 +02:00
|
|
|
DEBUGASSERT(mgeo.blocksize);
|
|
|
|
|
2018-08-28 15:30:21 +02:00
|
|
|
state.blk = NULL;
|
|
|
|
state.mtd = mtd;
|
2018-11-08 16:41:54 +01:00
|
|
|
state.blocksize = mgeo.blocksize;
|
|
|
|
state.erasesize = mgeo.erasesize;
|
|
|
|
state.nblocks = mgeo.neraseblocks;
|
|
|
|
state.nblocks *= mgeo.erasesize / mgeo.blocksize;
|
2018-08-28 15:30:21 +02:00
|
|
|
|
|
|
|
return parse_partition(&state, handler, arg);
|
|
|
|
}
|