From 6ccd7242c4c1404dafb64cd937adc3c65ce02385 Mon Sep 17 00:00:00 2001 From: Frank Haverkamp Date: Wed, 21 Jun 2006 14:26:02 +0200 Subject: [MTD] UBI: Removed automake, autoconf, added ubi userspace headers. Signed-off-by: Frank Haverkamp --- include/mtd/ubi-header.h | 390 +++++++++++++++++++++++++++++++++++++++++++++++ include/mtd/ubi-user.h | 128 ++++++++++++++++ 2 files changed, 518 insertions(+) create mode 100644 include/mtd/ubi-header.h create mode 100644 include/mtd/ubi-user.h (limited to 'include/mtd') diff --git a/include/mtd/ubi-header.h b/include/mtd/ubi-header.h new file mode 100644 index 0000000..1b325a1 --- /dev/null +++ b/include/mtd/ubi-header.h @@ -0,0 +1,390 @@ +/* + * Copyright (c) International Business Machines Corp., 2006 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See + * the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Authors: Artem B. Bityutskiy + * Thomas Gleixner + * Frank Haverkamp + * Oliver Lohmann + * Andreas Arnez + */ + +/* + * This file defines the layout of UBI headers and all the other UBI on-flash + * data structures. + */ + +#ifndef __UBI_HEADER_H__ +#define __UBI_HEADER_H__ + +#include + +/* The version of this UBI implementation */ +#define UBI_VERSION 1 + +/* The highest erase counter value supported by this implementation of UBI */ +#define UBI_MAX_ERASECOUNTER 0x7FFFFFFF + +/* The initial CRC32 value used when calculating CRC checksums */ +#define UBI_CRC32_INIT 0xFFFFFFFFU + +/** + * Magic numbers of the UBI headers. + * + * @UBI_EC_HDR_MAGIC: erase counter header magic number (ASCII "UBI#") + * @UBI_VID_HDR_MAGIC: volume identifier header magic number (ASCII "UBI!") + */ +enum { + UBI_EC_HDR_MAGIC = 0x55424923, + UBI_VID_HDR_MAGIC = 0x55424921 +}; + +/** + * Molume type constants used in volume identifier headers. + * + * @UBI_VID_DYNAMIC: dynamic volume + * @UBI_VID_STATIC: static volume + */ +enum { + UBI_VID_DYNAMIC = 1, + UBI_VID_STATIC = 2 +}; + +/** + * Compatibility constants used by internal volumes. + * + * @UBI_COMPAT_DELETE: delete this internal volume before anything is written + * to the flash + * @UBI_COMPAT_RO: attach this device in read-only mode + * @UBI_COMPAT_IGNORE: ignore this internal volume, but the UBI wear-leveling + * unit may still move these logical eraseblocks to ensure wear-leveling + * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its + * physical eraseblocks, don't even allow the wear-leveling unit to move + * them + * @UBI_COMPAT_REJECT: reject this UBI image + */ +enum { + UBI_COMPAT_DELETE = 1, + UBI_COMPAT_RO = 2, + UBI_COMPAT_IGNORE = 3, + UBI_COMPAT_PRESERVE = 4, + UBI_COMPAT_REJECT = 5 +}; + +/* + * ubi16_t/ubi32_t/ubi64_t - 16, 32, and 64-bit integers used in UBI on-flash + * data structures. + */ +typedef struct { + uint16_t int16; +} __attribute__ ((packed)) ubi16_t; + +typedef struct { + uint32_t int32; +} __attribute__ ((packed)) ubi32_t; + +typedef struct { + uint64_t int64; +} __attribute__ ((packed)) ubi64_t; + +/* + * In this implementation UBI uses the big-endian format for on-flash integers. + * The below are the corresponding endianess conversion macros. + */ +#define cpu_to_ubi16(x) ((ubi16_t){__cpu_to_be16(x)}) +#define ubi16_to_cpu(x) ((uint16_t)__be16_to_cpu((x).int16)) + +#define cpu_to_ubi32(x) ((ubi32_t){__cpu_to_be32(x)}) +#define ubi32_to_cpu(x) ((uint32_t)__be32_to_cpu((x).int32)) + +#define cpu_to_ubi64(x) ((ubi64_t){__cpu_to_be64(x)}) +#define ubi64_to_cpu(x) ((uint64_t)__be64_to_cpu((x).int64)) + +/* + * Sizes of UBI headers. + */ +#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr) +#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr) + +/* + * Sizes of UBI headers without the ending CRC. + */ +#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(ubi32_t)) +#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(ubi32_t)) + +/* + * How much private data may internal volumes store in the VID header. + */ +#define UBI_VID_HDR_IVOL_DATA_SIZE 12 + +/** + * struct ubi_ec_hdr - UBI erase counter header. + * + * @magic: the erase counter header magic number (%UBI_EC_HDR_MAGIC) + * @version: the version of UBI implementation which is supposed to accept this + * UBI image (%UBI_VERSION) + * @padding1: reserved for future, zeroes + * @ec: the erase counter + * @vid_hdr_offset: where the VID header begins + * @data_offset: where the user data begins + * @padding2: reserved for future, zeroes + * @hdr_crc: the erase counter header CRC checksum + * + * The erase counter header takes 64 bytes and has a plenty of unused space for + * future usage. The unused fields are zeroed. The @version field is used to + * indicate the version of UBI implementation which is supposed to be able to + * work with this UBI image. If @version is greater then the current UBI + * version, the image is rejecter. This may be useful in future if something + * is changed radically. This field is duplicated in the volume identifier + * header. + * + * The @vid_hdr_offset and @data_offset fields contain the offset of the the + * volume identifier header and user data, relative to the beginning of the + * eraseblock. These values have to be the same for all eraseblocks. + */ +struct ubi_ec_hdr { + ubi32_t magic; + uint8_t version; + uint8_t padding1[3]; + ubi64_t ec; /* Warning: the current limit is 31-bit anyway! */ + ubi32_t vid_hdr_offset; + ubi32_t data_offset; + uint8_t padding2[36]; + ubi32_t hdr_crc; +} __attribute__ ((packed)); + +/** + * struct ubi_vid_hdr - on-flash UBI volume identifier header. + * + * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC) + * @version: UBI implementation version which is supposed to accept this UBI + * image (%UBI_VERSION) + * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC) + * @copy_flag: a flag indicating if this physical eraseblock was created by + * means of copying an original physical eraseblock to ensure wear-leveling. + * @compat: compatibility of this volume (%UBI_COMPAT_DELETE, + * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT) + * @vol_id: volume ID + * @lnum: logical eraseblock number + * @leb_ver: eraseblock copy number + * @data_size: how many bytes of data this eraseblock contains. + * @used_ebs: total number of used logical eraseblocks in this volume + * @data_pad: how many bytes at the end of this eraseblock are not used + * @data_crc: CRC checksum of data containing in this eraseblock + * @padding1: reserved for future, zeroes + * @ivol_data: private data of internal volumes + * @hdr_crc: volume identifier header CRC checksum + * + * The @leb_ver and the @copy_flag fields are used to distinguish between older + * and newer copies of logical eraseblocks, as well as to guarantee robustness + * to unclean reboots. As UBI erases logical eraseblocks asynchronously, it has + * to distinguish between older and newer copies of eraseblocks. This is done + * using the @version field. On the other hand, when UBI moves an eraseblock, + * its version is also increased and the @copy_flag is set to 1. Additionally, + * when moving eraseblocks, UBI calculates data CRC and stores it in the + * @data_crc field, even for dynamic volumes. + * + * Thus, if there are 2 eraseblocks of the same volume and logical number, UBI + * uses the following algorithm to pick one of them. It first picks the one + * with larger version (say, A). If @copy_flag is not set, then A is picked. If + * @copy_flag is set, UBI checks the CRC of the eraseblock (@data_crc). This is + * needed to ensure that copying was finished. If the CRC is all right, A is + * picked. If not, the older eraseblock is picked. + * + * Note, the @leb_ver field may overflow. Thus, if you have 2 versions A and B, + * then A > B if abs(A-B) < 0x7FFFFFFF, and A < B otherwise. + * + * There are 2 sorts of volumes in UBI: user volumes and internal volumes. + * Internal volumes are not seen from outside and are used for different + * internal UBI purposes. In this implementation there are only two internal + * volumes: the layout volume and the update volume. Internal volumes are the + * main mechanism of UBI extensions. For example, in future one may introduce a + * journal internal volume. + * + * The @compat field is only used for internal volumes and contains the degree + * of their compatibility. This field is always zero for user volumes. This + * field provides a mechanism to introduce UBI extensions and to be still + * compatible with older UBI binaries. For example, if someone introduced an + * journal internal volume in future, he would probably use %UBI_COMPAT_DELETE + * compatibility. And in this case, older UBI binaries, which know nothing + * about the journal volume, would just delete this and work perfectly fine. + * This is somewhat similar to what Ext2fs does when it is fed by an Ext3fs + * image - it just ignores the Ext3fs journal. + * + * The @data_crc field contains the CRC checksum of the contents of the logical + * eraseblock if this is a static volume. In case of dynamic volumes, it does + * not contain the CRC checksum as a rule. The only exception is when the + * logical eraseblock was moved by the wear-leveling unit, then the + * wear-leveling unit calculates the eraseblocks' CRC and stores it at + * @data_crc. + * + * The @data_size field is always used for static volumes because we want to + * know about how many bytes of data are stored in this eraseblock. For + * dynamic eraseblocks, this field usually contains zero. The only exception is + * when the logical eraseblock is moved to another physical eraseblock due to + * wear-leveling reasons. In this case, UBI calculates CRC checksum of the + * contents and uses both @data_crc and @data_size fields. In this case, the + * @data_size field contains the size of logical eraseblock of this volume + * (which may vary owing to @alignment). + * + * The @used_ebs field is used only for static volumes and indicates how many + * eraseblocks the data of the volume takes. For dynamic volumes this field is + * not used and always contains zero. + * + * The @data_pad is calculated when volumes are created using the alignment + * parameter. So, effectively, the @data_pad field reduces the size of logical + * eraseblocks of this volume. This is very handy when one uses block-oriented + * software (say, cramfs) on top of the UBI volume. + * + * The @ivol_data contains private data of internal volumes. This might be very + * handy to store data in the VID header, not in the eraseblock's contents. For + * example it may make life of simple boot-loaders easier. The @ivol_data field + * contains zeroes for user volumes. + */ +struct ubi_vid_hdr { + ubi32_t magic; + uint8_t version; + uint8_t vol_type; + uint8_t copy_flag; + uint8_t compat; + ubi32_t vol_id; + ubi32_t lnum; + ubi32_t leb_ver; + ubi32_t data_size; + ubi32_t used_ebs; + ubi32_t data_pad; + ubi32_t data_crc; + uint8_t padding1[12]; + uint8_t ivol_data[UBI_VID_HDR_IVOL_DATA_SIZE]; + ubi32_t hdr_crc; +} __attribute__ ((packed)); + +/** + * struct ubi_vid_hdr_upd_vol - private data of the update internal volume + * stored in volume identifier headers. + * + * @vol_id: volume ID of the volume under update + * @padding: zeroes + */ +struct ubi_vid_hdr_upd_vol { + ubi32_t vol_id; + uint8_t padding[UBI_VID_HDR_IVOL_DATA_SIZE - 4]; +} __attribute__ ((packed)); + +/* + * Count of internal UBI volumes. + */ +#define UBI_INT_VOL_COUNT 2 + +/* + * Internal volume IDs start from this digit. There is a reserved room for 4096 + * internal volumes. + */ +#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096) + +/** + * enum ubi_internal_volume_numbers - volume IDs of internal UBI volumes. + * + * %UBI_LAYOUT_VOL_ID: volume ID of the layout volume + * %UBI_UPDATE_VOL_ID: volume ID of the update volume + */ +enum ubi_internal_volume_ids { + UBI_LAYOUT_VOL_ID = UBI_INTERNAL_VOL_START, + UBI_UPDATE_VOL_ID = UBI_INTERNAL_VOL_START + 1 +}; + +/* + * Number of logical eraseblocks reserved for internal volumes. + */ +#define UBI_LAYOUT_VOLUME_EBS 2 +#define UBI_UPDATE_VOLUME_EBS 1 + +/* + * Names of internal volumes + */ +#define UBI_LAYOUT_VOLUME_NAME "The layout volume" +#define UBI_UPDATE_VOLUME_NAME "The update volume" + +/* + * Compatibility flags of internal volumes. + */ +#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT +#define UBI_UPDATE_VOLUME_COMPAT UBI_COMPAT_REJECT + +/* + * The maximum number of volumes per one UBI device. + */ +#define UBI_MAX_VOLUMES 128 + +/* + * The maximum volume name length. + */ +#define UBI_VOL_NAME_MAX 127 + +/* + * Size of volume table records. + */ +#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vol_tbl_record) + +/* + * Size of volume table records without the ending CRC. + */ +#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(ubi32_t)) + +/** + * struct ubi_vol_tbl_record - a record in the volume table. + * + * @reserved_pebs: how many physical eraseblocks are reserved for this volume + * @alignment: volume alignment + * @data_pad: how many bytes are not used at the end of the eraseblocks to + * satisfy the requested alignment + * @padding1: reserved, zeroes + * @name_len: the volume name length + * @name: the volume name + * @padding2: reserved, zeroes + * @crc: a CRC32 checksum of the record + * + * The layout volume consists of 2 logical eraseblock, each of which contains + * the volume table (i.e., the volume table is duplicated). The volume table is + * an array of &struct ubi_vol_tbl_record objects indexed by the volume ID. + * + * If the size of the logical eraseblock is large enough to fit + * %UBI_MAX_VOLUMES, the volume table contains %UBI_MAX_VOLUMES records. + * Otherwise, it contains as much records as can be fit (i.e., size of logical + * eraseblock divided by sizeof(struct ubi_vol_tbl_record)). + * + * The @alignment field is specified when the volume is created and cannot be + * later changed. It may be useful, for example, when a block-oriented file + * system works on top of UBI. The @data_pad field is calculated using the + * logical eraseblock size and @alignment. The alignment must be multiple to the + * minimal flash I/O unit. If @alignment is 1, all the available space of + * eraseblocks is used. + * + * Empty records contain all zeroes and the CRC checksum of those zeroes. + */ +struct ubi_vol_tbl_record { + ubi32_t reserved_pebs; + ubi32_t alignment; + ubi32_t data_pad; + uint8_t vol_type; + uint8_t padding1; + ubi16_t name_len; + uint8_t name[UBI_VOL_NAME_MAX + 1]; + uint8_t padding2[24]; + ubi32_t crc; +} __attribute__ ((packed)); + +#endif /* !__UBI_HEADER_H__ */ diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h new file mode 100644 index 0000000..161f674 --- /dev/null +++ b/include/mtd/ubi-user.h @@ -0,0 +1,128 @@ +/* + * Copyright (c) International Business Machines Corp., 2006 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See + * the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Artem B. Bityutskiy + */ + +#ifndef __UBI_USER_H__ +#define __UBI_USER_H__ + +#ifndef __KERNEL__ +#define __user +#endif + +/* + * When a new volume is created, users may either specify the volume number they + * want to create or to let UBI automatically assign a volume number using this + * constant. + */ +#define UBI_VOL_NUM_AUTO (-1) + +/* + * IOCTL commands of UBI character devices + */ + +#define UBI_IOC_MAGIC 'o' + +/* Create an UBI volume */ +#define UBI_IOCMKVOL _IOW(UBI_IOC_MAGIC, 0, struct ubi_mkvol_req) +/* Remove an UBI volume */ +#define UBI_IOCRMVOL _IOW(UBI_IOC_MAGIC, 1, int32_t) +/* Re-size an UBI volume */ +#define UBI_IOCRSVOL _IOW(UBI_IOC_MAGIC, 2, struct ubi_rsvol_req) + +/* + * IOCTL commands of UBI volume character devices. + */ + +#define UBI_VOL_IOC_MAGIC 'O' + +/* Start UBI volume update */ +#define UBI_IOCVOLUP _IOW(UBI_VOL_IOC_MAGIC, 0, int64_t) +/* An eraseblock erasure command, used for debugging, disabled by dafault */ +#define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 0, int32_t) + +/** + * UBI volume type constants. + * + * @UBI_DYNAMIC_VOLUME: dynamic volume + * @UBI_STATIC_VOLUME: static volume + */ +enum { + UBI_DYNAMIC_VOLUME = 3, + UBI_STATIC_VOLUME = 4 +}; + +/** + * struct ubi_mkvol_req - volume description data structure used in + * volume creation requests. + * + * @vol_id: volume number + * @alignment: volume alignment + * @bytes: volume size in bytes + * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) + * @padding: reserved for future, not used + * @name_len: volume name length + * @name: volume name + * + * This structure is used by userspace programs when creating new volumes. The + * @used_bytes field is only necessary when creating static volumes. + * + * The @alignment field specifies the required alignment of the volume logical + * eraseblock. This means, that the size of logical eraseblocks will be aligned + * to this number, i.e., + * (UBI device logical eraseblock size) mod (@alignment) = 0. + * + * To put it differently, the logical eraseblock of this volume may be slightly + * shortened in order to make it properly aligned. The alignment has to be + * multiple of the flash minimal input/output unit, or %1 to utilize the entire + * available space of logical eraseblocks. + * + * The @alignment field may be useful, for example, when one wants to maintain + * a block device on top of an UBI volume. In this case, it is desirable to fit + * an integer number of blocks in logical eraseblocks of this UBI volume. With + * alignment it is possible to update this volume using plane UBI volume image + * BLOBs, without caring about how to properly write them. + */ +struct ubi_mkvol_req { + int32_t vol_id; + int32_t alignment; + int64_t bytes; + int8_t vol_type; + int8_t padding[9]; + int16_t name_len; + __user const char *name; +} __attribute__ ((packed)); + +/** + * struct ubi_rsvol_req - a data structure used in volume re-size requests. + * + * @vol_id: ID of the volume to re-size + * @bytes: new size of the volume in bytes + * + * Re-sizing is possible for both dynamic and static volumes. But while dynamic + * volumes may be re-sized arbitrarily, static volumes cannot be made to be + * smaller then the number of bytes they bear. To arbitrarily shrink a static + * volume, it must be wiped out first (by means of volume update operation with + * zero number of bytes). + */ +struct ubi_rsvol_req { + int64_t bytes; + int32_t vol_id; +} __attribute__ ((packed)); + +#endif /* __UBI_USER_H__ */ -- cgit v1.2.3 From d8f5f9f8cfc5a780f7e95b178e919ac2b20fda93 Mon Sep 17 00:00:00 2001 From: Josh Boyer Date: Wed, 21 Jun 2006 08:37:06 -0500 Subject: Fix ubi-header.h to use userspace swab functions Signed-off-by: Josh Boyer --- include/mtd/ubi-header.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'include/mtd') diff --git a/include/mtd/ubi-header.h b/include/mtd/ubi-header.h index 1b325a1..12ce1c9 100644 --- a/include/mtd/ubi-header.h +++ b/include/mtd/ubi-header.h @@ -30,7 +30,7 @@ #ifndef __UBI_HEADER_H__ #define __UBI_HEADER_H__ -#include +#include /* The version of this UBI implementation */ #define UBI_VERSION 1 @@ -104,14 +104,14 @@ typedef struct { * In this implementation UBI uses the big-endian format for on-flash integers. * The below are the corresponding endianess conversion macros. */ -#define cpu_to_ubi16(x) ((ubi16_t){__cpu_to_be16(x)}) -#define ubi16_to_cpu(x) ((uint16_t)__be16_to_cpu((x).int16)) +#define cpu_to_ubi16(x) ((ubi16_t){cpu_to_be16(x)}) +#define ubi16_to_cpu(x) ((uint16_t)be16_to_cpu((x).int16)) -#define cpu_to_ubi32(x) ((ubi32_t){__cpu_to_be32(x)}) -#define ubi32_to_cpu(x) ((uint32_t)__be32_to_cpu((x).int32)) +#define cpu_to_ubi32(x) ((ubi32_t){cpu_to_be32(x)}) +#define ubi32_to_cpu(x) ((uint32_t)be32_to_cpu((x).int32)) -#define cpu_to_ubi64(x) ((ubi64_t){__cpu_to_be64(x)}) -#define ubi64_to_cpu(x) ((uint64_t)__be64_to_cpu((x).int64)) +#define cpu_to_ubi64(x) ((ubi64_t){cpu_to_be64(x)}) +#define ubi64_to_cpu(x) ((uint64_t)be64_to_cpu((x).int64)) /* * Sizes of UBI headers. -- cgit v1.2.3 From faa7699bf15b9a08b1b2658745314ae8f63878a2 Mon Sep 17 00:00:00 2001 From: "dedekind@linutronix.de" Date: Fri, 30 Jun 2006 14:05:25 +0200 Subject: [MTD] UBI: Adaptations to new driver, reworked frontend --- include/mtd/ubi-header.h | 21 +-- include/mtd/ubi-user.h | 7 +- ubi-utils/src/config.h | 4 +- ubi-utils/src/libubi.c | 31 ++-- ubi-utils/src/libubi_int.h | 8 +- ubi-utils/src/ubimkvol.c | 405 ++++++++++++++++++++++++++----------------- ubi-utils/src/ubirmvol.c | 267 +++++++++++++++++----------- ubi-utils/src/ubiupdatevol.c | 50 +++--- 8 files changed, 470 insertions(+), 323 deletions(-) (limited to 'include/mtd') diff --git a/include/mtd/ubi-header.h b/include/mtd/ubi-header.h index 12ce1c9..ca96fc9 100644 --- a/include/mtd/ubi-header.h +++ b/include/mtd/ubi-header.h @@ -41,7 +41,7 @@ /* The initial CRC32 value used when calculating CRC checksums */ #define UBI_CRC32_INIT 0xFFFFFFFFU -/** +/* * Magic numbers of the UBI headers. * * @UBI_EC_HDR_MAGIC: erase counter header magic number (ASCII "UBI#") @@ -52,7 +52,7 @@ enum { UBI_VID_HDR_MAGIC = 0x55424921 }; -/** +/* * Molume type constants used in volume identifier headers. * * @UBI_VID_DYNAMIC: dynamic volume @@ -63,23 +63,19 @@ enum { UBI_VID_STATIC = 2 }; -/** +/* * Compatibility constants used by internal volumes. * * @UBI_COMPAT_DELETE: delete this internal volume before anything is written * to the flash * @UBI_COMPAT_RO: attach this device in read-only mode - * @UBI_COMPAT_IGNORE: ignore this internal volume, but the UBI wear-leveling - * unit may still move these logical eraseblocks to ensure wear-leveling * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its - * physical eraseblocks, don't even allow the wear-leveling unit to move - * them + * physical eraseblocks, don't allow the wear-leveling unit to move them * @UBI_COMPAT_REJECT: reject this UBI image */ enum { UBI_COMPAT_DELETE = 1, UBI_COMPAT_RO = 2, - UBI_COMPAT_IGNORE = 3, UBI_COMPAT_PRESERVE = 4, UBI_COMPAT_REJECT = 5 }; @@ -281,7 +277,7 @@ struct ubi_vid_hdr { */ struct ubi_vid_hdr_upd_vol { ubi32_t vol_id; - uint8_t padding[UBI_VID_HDR_IVOL_DATA_SIZE - 4]; + uint8_t padding[UBI_VID_HDR_IVOL_DATA_SIZE-4]; } __attribute__ ((packed)); /* @@ -295,13 +291,13 @@ struct ubi_vid_hdr_upd_vol { */ #define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096) -/** +/* * enum ubi_internal_volume_numbers - volume IDs of internal UBI volumes. * * %UBI_LAYOUT_VOL_ID: volume ID of the layout volume * %UBI_UPDATE_VOL_ID: volume ID of the update volume */ -enum ubi_internal_volume_ids { +enum { UBI_LAYOUT_VOL_ID = UBI_INTERNAL_VOL_START, UBI_UPDATE_VOL_ID = UBI_INTERNAL_VOL_START + 1 }; @@ -351,6 +347,7 @@ enum ubi_internal_volume_ids { * @alignment: volume alignment * @data_pad: how many bytes are not used at the end of the eraseblocks to * satisfy the requested alignment + * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) * @padding1: reserved, zeroes * @name_len: the volume name length * @name: the volume name @@ -382,7 +379,7 @@ struct ubi_vol_tbl_record { uint8_t vol_type; uint8_t padding1; ubi16_t name_len; - uint8_t name[UBI_VOL_NAME_MAX + 1]; + uint8_t name[UBI_VOL_NAME_MAX+1]; uint8_t padding2[24]; ubi32_t crc; } __attribute__ ((packed)); diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h index 161f674..0eb1470 100644 --- a/include/mtd/ubi-user.h +++ b/include/mtd/ubi-user.h @@ -32,6 +32,9 @@ */ #define UBI_VOL_NUM_AUTO (-1) +/* Maximum volume name length */ +#define UBI_MAX_VOLUME_NAME 127 + /* * IOCTL commands of UBI character devices */ @@ -56,7 +59,7 @@ /* An eraseblock erasure command, used for debugging, disabled by dafault */ #define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 0, int32_t) -/** +/* * UBI volume type constants. * * @UBI_DYNAMIC_VOLUME: dynamic volume @@ -113,7 +116,7 @@ struct ubi_mkvol_req { * * @vol_id: ID of the volume to re-size * @bytes: new size of the volume in bytes - * + * * Re-sizing is possible for both dynamic and static volumes. But while dynamic * volumes may be re-sized arbitrarily, static volumes cannot be made to be * smaller then the number of bytes they bear. To arbitrarily shrink a static diff --git a/ubi-utils/src/config.h b/ubi-utils/src/config.h index 746fa3c..b5bbd5b 100644 --- a/ubi-utils/src/config.h +++ b/ubi-utils/src/config.h @@ -20,8 +20,8 @@ * Author: Frank Haverkamp */ -#define PACKAGE_VERSION "1.0" -#define PACKAGE_BUGREPORT "dedekind@oktetlabs.ru, haver@vnet.ibm.com, or tglx@linutronix.de" +#define PACKAGE_VERSION "1.1" +#define PACKAGE_BUGREPORT "haver@vnet.ibm.com, dedekind@linutronix.de, or tglx@linutronix.de" #define __unused __attribute__((unused)) diff --git a/ubi-utils/src/libubi.c b/ubi-utils/src/libubi.c index 9b9a793..979f157 100644 --- a/ubi-utils/src/libubi.c +++ b/ubi-utils/src/libubi.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "libubi.h" #include "libubi_int.h" @@ -47,7 +48,6 @@ * @sysfs_root sysfs root directory * @ubi_root UBI root directory in sysfs * - * @nlen_max full path to the "maximum volume name length" sysfs file * @version full path to the "UBI version" sysfs file * * @cdev_path path pattern to UBI character devices @@ -85,7 +85,6 @@ struct ubi_lib char *sysfs_root; char *ubi_root; - char *nlen_max; char *version; char *cdev_path; int cdev_path_len; @@ -147,10 +146,6 @@ get_ubi_info(ubi_lib_t desc, struct ubi_info *ubi) if (err) return -1; - err = sysfs_read_int(desc->nlen_max, (int*) &ubi->nlen_max); - if (err) - return -1; - /* Calculate number of UBI devices */ do { char dir[20]; @@ -186,7 +181,6 @@ ubi_dump_handler(ubi_lib_t desc) ubi_lib_t d = desc; printf( "UBI Library Descriptor:\n" "ubi_root: %s\n" - "nlen_max: %s\n" "version: %s\n" "cdev_path: %s\n" "udev_path: %s\n" @@ -204,12 +198,12 @@ ubi_dump_handler(ubi_lib_t desc) "vol_type_path: %s\n" "vol_name_path: %s\n" "cdev_path_len: %d\n\n", - d->ubi_root, d->nlen_max, d->version, d->cdev_path, - d->udev_path, d->wear_path, d->vol_count_path, - d->tot_ebs_path, d->avail_ebs_path, d->eb_size_path, - d->nums_path, d->vol_cdev_path, d->vdev_path, - d->vol_nums_path, d->vol_bytes_path, d->vol_ebs_path, - d->vol_type_path, d->vol_name_path, d->cdev_path_len); + d->ubi_root, d->version, d->cdev_path, d->udev_path, + d->wear_path, d->vol_count_path, d->tot_ebs_path, + d->avail_ebs_path, d->eb_size_path, d->nums_path, + d->vol_cdev_path, d->vdev_path, d->vol_nums_path, + d->vol_bytes_path, d->vol_ebs_path, d->vol_type_path, + d->vol_name_path, d->cdev_path_len); } int @@ -281,11 +275,7 @@ ubi_open(ubi_lib_t *desc) if (!res->ubi_root) goto error; - res->nlen_max = mkpath(res->ubi_root, UBI_NLEN_MAX); - if (!res->nlen_max) - goto error; - - res->version = mkpath(res->ubi_root, UBI_VERSION); + res->version = mkpath(res->ubi_root, UBI_VER); if (!res->version) goto error; @@ -394,7 +384,6 @@ ubi_close(ubi_lib_t *desc) free(tmp->udev_path); free(tmp->cdev_path); free(tmp->version); - free(tmp->nlen_max); free(tmp->ubi_root); free(tmp->sysfs_root); free(tmp); @@ -486,7 +475,7 @@ ubi_get_vol_info(ubi_lib_t desc, unsigned int devn, unsigned int vol_id, int err; int len; char buf1[10]; - char buf2[desc->ubi.nlen_max]; + char buf2[UBI_MAX_VOLUME_NAME]; err = sysfs_read_dev_subst(desc->vol_nums_path, &req->major, &req->minor, 2, devn, vol_id); @@ -523,7 +512,7 @@ ubi_get_vol_info(ubi_lib_t desc, unsigned int devn, unsigned int vol_id, } len = sysfs_read_data_subst(desc->vol_name_path, &buf2[0], - desc->ubi.nlen_max, 2, devn, vol_id); + UBI_MAX_VOLUME_NAME, 2, devn, vol_id); if (len == -1) return -1; diff --git a/ubi-utils/src/libubi_int.h b/ubi-utils/src/libubi_int.h index ab387f5..830a682 100644 --- a/ubi-utils/src/libubi_int.h +++ b/ubi-utils/src/libubi_int.h @@ -1,5 +1,3 @@ -#ifndef __UBI_INT_H__ -#define __UBI_INT_H__ /* * Copyright (c) International Business Machines Corp., 2006 * @@ -24,6 +22,8 @@ * Author: Artem B. Bityutskiy */ +#ifndef __UBI_INT_H__ +#define __UBI_INT_H__ /* * Enable/disable UBI library debugging messages. */ @@ -58,7 +58,7 @@ * @def UBI_NLEN_MAX * @brief Name of syfs file containing the maximum UBI volume name length. * - * @def UBI_VERSION + * @def UBI_VER * @brief Name of sysfs file containing UBI version. * * @def UBI_WEAR @@ -98,7 +98,7 @@ **/ #define UBI_ROOT "ubi" #define UBI_NLEN_MAX "volume_name_max" -#define UBI_VERSION "version" +#define UBI_VER "version" #define UBI_WEAR "wear" #define UBI_VOL_COUNT "volumes_count" #define UBI_TOT_EBS "total_eraseblocks" diff --git a/ubi-utils/src/ubimkvol.c b/ubi-utils/src/ubimkvol.c index 30c569c..d35d2f3 100644 --- a/ubi-utils/src/ubimkvol.c +++ b/ubi-utils/src/ubimkvol.c @@ -19,13 +19,16 @@ /* * An utility to create UBI volumes. * - * Author: Artem B. Bityutskiy + * Author: Artem B. Bityutskiy + * Frank Haverkamp * * 1.0 Initial release * 1.1 Does not support erase blocks anymore. This is replaced by * the number of bytes. + * 1.2 Reworked the user-interface to use argp. */ +#include #include #include #include @@ -33,178 +36,229 @@ #include #include -#include "config.h" +#include #include -static void usage(void); -static int param_sanity_check(ubi_lib_t lib); -static int parse_options(int argc, char * const argv[]); +#define VERSION "1.2" /* - * The variables below are set by command line arguments. + * The variables below are set by command line arguments. */ -static int vol_type = UBI_DYNAMIC_VOLUME; -static int devn = -1; -static long long bytes = 0; -static int alignment = 1; -static int vol_id = UBI_VOL_NUM_AUTO; -static char *name = NULL; -static int nlen = 0; +struct args { + int devn; + int vol_id; + int vol_type; + long long bytes; + int alignment; + char *name; + int nlen; -int main(int argc, char * const argv[]) -{ - int err; - ubi_lib_t lib; + /* special stuff needed to get additional arguments */ + char *arg1; + char **options; /* [STRING...] */ +}; - err = parse_options(argc, argv); - if (err) { - fprintf(stderr, "Wrong options ...\n"); - return err == 1 ? 0 : -1; - } +static struct args myargs = { + .vol_type = UBI_DYNAMIC_VOLUME, + .devn = -1, + .bytes = 0, + .alignment = 1, + .vol_id = UBI_VOL_NUM_AUTO, + .name = NULL, + .nlen = 0, +}; - if (devn == -1) { - fprintf(stderr, "Device number was not specified\n"); - fprintf(stderr, "Use -h option for help\n"); - return -1; - } +static int param_sanity_check(struct args *args, ubi_lib_t lib); +static error_t parse_opt(int key, char *optarg, struct argp_state *state); +const char *argp_program_bug_address = PACKAGE_BUGREPORT; - err = ubi_open(&lib); - if (err) { - perror("Cannot open libubi"); - return -1; - } +static char doc[] = "\nVersion: " VERSION "\n\t" + BUILD_OS" "BUILD_CPU" at "__DATE__" "__TIME__"\n" + "\nMake UBI Volume.\n"; - err = param_sanity_check(lib); - if (err) { - perror("Input parameters check"); - fprintf(stderr, "Use -h option for help\n"); - goto out_libubi; - } +static struct argp_option options[] = { + { .name = "devn", + .key = 'd', + .arg = "", + .flags = 0, + .doc = "UBI device", + .group = OPTION_ARG_OPTIONAL }, - err = ubi_mkvol(lib, devn, vol_id, vol_type, bytes, alignment, name); - if (err < 0) { - perror("Cannot create volume"); - fprintf(stderr, " err=%d\n", err); - goto out_libubi; - } + { .name = "vol_id", + .key = 'n', + .arg = "", + .flags = 0, + .doc = "UBI volume id, if not specified, the volume ID will be " + "assigned automatically", + .group = OPTION_ARG_OPTIONAL }, - /* printf("Created volume %d, %lld bytes, type %s, name %s\n", - vol_id, bytes, vol_type == UBI_DYNAMIC_VOLUME ? - "dynamic" : "static", name); */ + { .name = "type", + .key = 't', + .arg = "", + .flags = 0, + .doc = "volume type (dynamic, static), default is dynamic", + .group = OPTION_ARG_OPTIONAL }, - vol_id = err; - ubi_close(&lib); - return 0; + { .name = "size", + .key = 's', + .arg = "", + .flags = 0, + .doc = "volume size volume size in bytes, " + "kilobytes (KiB) or megabytes (MiB)", + .group = OPTION_ARG_OPTIONAL }, -out_libubi: - ubi_close(&lib); - return -1; -} + { .name = "name", + .key = 'N', + .arg = "", + .flags = 0, + .doc = "volume name", + .group = OPTION_ARG_OPTIONAL }, + + { .name = "alignment", + .key = 'a', + .arg = "", + .flags = 0, + .doc = "volume alignment (default is 1)", + .group = OPTION_ARG_OPTIONAL }, -/* 'getopt()' option string */ -static const char *optstring = "ht:s:n:N:d:a:"; + { .name = NULL, .key = 0, .arg = NULL, .flags = 0, + .doc = NULL, .group = 0 }, +}; -static int parse_options(int argc, char * const argv[]) +static struct argp argp = { + .options = options, + .parser = parse_opt, + .args_doc = 0, + .doc = doc, + .children = NULL, + .help_filter = NULL, + .argp_domain = NULL, +}; + +/* + * @brief Parse the arguments passed into the test case. + * + * @param key The parameter. + * @param arg Argument passed to parameter. + * @param state Location to put information on parameters. + * + * @return error + * + * Get the `input' argument from `argp_parse', which we know is a + * pointer to our arguments structure. + */ +static error_t +parse_opt(int key, char *optarg, struct argp_state *state) { - int opt = 0; - - while (opt != -1) { - char *endp; - - opt = getopt(argc, argv, optstring); - - switch (opt) { - case 'h': - usage(); - return 1; - case 't': - if (!strcmp(optarg, "dynamic")) - vol_type = UBI_DYNAMIC_VOLUME; - else if (!strcmp(optarg, "static")) - vol_type = UBI_STATIC_VOLUME; - else { - fprintf(stderr, "Bad volume type: \"%s\"\n", - optarg); - goto out; - } - break; - case 's': - bytes = strtoull(optarg, &endp, 0); - if (endp == optarg || bytes < 0) { - fprintf(stderr, "Bad volume size: \"%s\"\n", - optarg); - goto out; - } - if (endp != '\0') { - if (strcmp(endp, "KiB") == 0) - bytes *= 1024; - else if (strcmp(endp, "MiB") == 0) - bytes *= 1024*1024; - } - break; - case 'a': - alignment = strtoul(optarg, &endp, 0); - if (*endp != '\0' || endp == optarg || - alignment <= 0) { - fprintf(stderr, "Bad volume alignment: " - "\"%s\"\n", optarg); - goto out; - } - break; - case 'd': - devn = strtoul(optarg, &endp, 0); - if (*endp != '\0' || endp == optarg || devn < 0) { - fprintf(stderr, "Bad UBI device number: " - "\"%s\"\n", optarg); - goto out; - } - break; - case 'n': - vol_id = strtoul(optarg, &endp, 0); - if (*endp != '\0' || endp == optarg || - (vol_id < 0 && vol_id != UBI_DYNAMIC_VOLUME)) { - fprintf(stderr, "Bad volume ID: " - "\"%s\"\n", optarg); - goto out; - } - break; - case 'N': - name = optarg; - nlen = strlen(name); - break; - - case ':': - fprintf(stderr, "Parameter is missing\n"); + char *endp; + struct args *args = state->input; + + switch (key) { + case 't': + if (!strcmp(optarg, "dynamic")) + args->vol_type = UBI_DYNAMIC_VOLUME; + else if (!strcmp(optarg, "static")) + args->vol_type = UBI_STATIC_VOLUME; + else { + fprintf(stderr, "Bad volume type: \"%s\"\n", + optarg); goto out; - case '?': - fprintf(stderr, "Unknown parameter\n"); + } + break; + case 's': + args->bytes = strtoull(optarg, &endp, 0); + if (endp == optarg || args->bytes < 0) { + fprintf(stderr, "Bad volume size: \"%s\"\n", + optarg); goto out; - case -1: - break; - default: - fprintf(stderr, "Internal error\n"); + } + if (endp != '\0') { + if (strcmp(endp, "KiB") == 0) + args->bytes *= 1024; + else if (strcmp(endp, "MiB") == 0) + args->bytes *= 1024*1024; + } + break; + case 'a': + args->alignment = strtoul(optarg, &endp, 0); + if (*endp != '\0' || endp == optarg || + args->alignment <= 0) { + fprintf(stderr, "Bad volume alignment: " + "\"%s\"\n", optarg); goto out; } + break; + case 'd': /* --devn= */ + args->devn = strtoul(optarg, &endp, 0); + if (*endp != '\0' || endp == optarg || args->devn < 0) { + fprintf(stderr, "Bad UBI device number: " + "\"%s\"\n", optarg); + goto out; + } + break; + case 'n': /* --volid= */ + args->vol_id = strtoul(optarg, &endp, 0); + if (*endp != '\0' || endp == optarg || + (args->vol_id < 0 && args->vol_id != UBI_DYNAMIC_VOLUME)) { + fprintf(stderr, "Bad volume ID: " + "\"%s\"\n", optarg); + goto out; + } + break; + case 'N': + args->name = optarg; + args->nlen = strlen(args->name); + break; + + case ':': + fprintf(stderr, "Parameter is missing\n"); + goto out; + + case ARGP_KEY_NO_ARGS: + /* argp_usage(state); */ + break; + + case ARGP_KEY_ARG: + args->arg1 = optarg; + /* Now we consume all the rest of the arguments. + `state->next' is the index in `state->argv' of the + next argument to be parsed, which is the first STRING + we're interested in, so we can just use + `&state->argv[state->next]' as the value for + arguments->strings. + + _In addition_, by setting `state->next' to the end + of the arguments, we can force argp to stop parsing + here and return. */ + + args->options = &state->argv[state->next]; + state->next = state->argc; + break; + + case ARGP_KEY_END: + /* argp_usage(state); */ + break; + + default: + return(ARGP_ERR_UNKNOWN); } return 0; - out: - errno = EINVAL; - return -1; + return(ARGP_ERR_UNKNOWN); } -static int param_sanity_check(ubi_lib_t lib) +static int param_sanity_check(struct args *args, ubi_lib_t lib) { int err, len; struct ubi_info ubi; - if (bytes == 0) { + if (args->bytes == 0) { fprintf(stderr, "Volume size was not specified\n"); goto out; } - if (name == NULL) { + if (args->name == NULL) { fprintf(stderr, "Volume name was not specified\n"); goto out; } @@ -213,39 +267,72 @@ static int param_sanity_check(ubi_lib_t lib) if (err) return -1; - if (devn >= (int)ubi.dev_count) { - fprintf(stderr, "Device %d does not exist\n", devn); + if (args->devn >= (int)ubi.dev_count) { + fprintf(stderr, "Device %d does not exist\n", args->devn); goto out; } - len = strlen(name); - if (len > (int)ubi.nlen_max) { + len = strlen(args->name); + if (len > UBI_MAX_VOLUME_NAME) { fprintf(stderr, "Too long name (%d symbols), max is %d\n", - len, ubi.nlen_max); + len, UBI_MAX_VOLUME_NAME); goto out; } return 0; - out: errno = EINVAL; return -1; } -static void usage(void) +int main(int argc, char * const argv[]) { - printf("Usage: ubi_mkvol OPTIONS\n" - "Version: " PACKAGE_VERSION "\n" - "The command line options:\n" - "\t-h - this help message\n" - "\t-d - UBI device number\n" - "\t-t TYPE - volume type (dynamic, static) " - "(default is dynamic)\n" - "\t-n VOLID - volume ID to assign to the new volume. If not" - "specified, \n" - "\t the volume ID will be assigned automatically\n" - "\t-s BYTES - volume size in bytes, " - "kilobytes (KiB) or megabytes (MiB)\n" - "\t-N NAME - volume name\n" - "\t-a ALIGNMENT - volume alignment (default is 1)\n"); + int err; + ubi_lib_t lib; + + err = argp_parse(&argp, argc, (char **)argv, ARGP_IN_ORDER, 0, + &myargs); + if (err) { + fprintf(stderr, "Wrong options ...\n"); + return err == 1 ? 0 : -1; + } + + if (myargs.devn == -1) { + fprintf(stderr, "Device number was not specified\n"); + fprintf(stderr, "Use -h option for help\n"); + return -1; + } + + err = ubi_open(&lib); + if (err) { + perror("Cannot open libubi"); + return -1; + } + + err = param_sanity_check(&myargs, lib); + if (err) { + perror("Input parameters check"); + fprintf(stderr, "Use -h option for help\n"); + goto out_libubi; + } + + err = ubi_mkvol(lib, myargs.devn, myargs.vol_id, myargs.vol_type, + myargs.bytes, myargs.alignment, myargs.name); + if (err < 0) { + perror("Cannot create volume"); + fprintf(stderr, " err=%d\n", err); + goto out_libubi; + } + + /* printf("Created volume %d, %lld bytes, type %s, name %s\n", + vol_id, bytes, vol_type == UBI_DYNAMIC_VOLUME ? + "dynamic" : "static", name); */ + + myargs.vol_id = err; + ubi_close(&lib); + return 0; + +out_libubi: + ubi_close(&lib); + return -1; } diff --git a/ubi-utils/src/ubirmvol.c b/ubi-utils/src/ubirmvol.c index cb47b6c..f810263 100644 --- a/ubi-utils/src/ubirmvol.c +++ b/ubi-utils/src/ubirmvol.c @@ -17,11 +17,15 @@ */ /* - * An utility to create UBI volumes. + * An utility to remove UBI volumes. * - * Autor: Artem B. Bityutskiy + * Author: Artem B. Bityutskiy + * Frank Haverkamp + * + * 1.1 Reworked the userinterface to use argp. */ +#include #include #include #include @@ -29,29 +33,180 @@ #include #include -#include "config.h" +#include #include -static void usage(void); -static int param_sanity_check(ubi_lib_t lib); -static int parse_options(int argc, char * const argv[]); +#define VERSION "1.1" /* * The below variables are set by command line options. */ -static int vol_id = -1; -static int devn = -1; +struct args { + int devn; + int vol_id; + + /* special stuff needed to get additional arguments */ + char *arg1; + char **options; /* [STRING...] */ +}; + +static struct args myargs = { + .devn = -1, + .vol_id = -1, + + .arg1 = NULL, + .options = NULL, +}; + +static int param_sanity_check(struct args *args, ubi_lib_t lib); +static error_t parse_opt(int key, char *optarg, struct argp_state *state); +const char *argp_program_bug_address = PACKAGE_BUGREPORT; + +static char doc[] = "\nVersion: " VERSION "\n\t" + BUILD_OS" "BUILD_CPU" at "__DATE__" "__TIME__"\n" + "\nMake UBI Volume.\n"; + +static struct argp_option options[] = { + { .name = "devn", + .key = 'd', + .arg = "", + .flags = 0, + .doc = "UBI device", + .group = OPTION_ARG_OPTIONAL }, + + { .name = "vol_id", + .key = 'n', + .arg = "", + .flags = 0, + .doc = "UBI volume id, if not specified, the volume ID will be " + "assigned automatically", + .group = OPTION_ARG_OPTIONAL }, + + { .name = NULL, .key = 0, .arg = NULL, .flags = 0, + .doc = NULL, .group = 0 }, +}; + +static struct argp argp = { + .options = options, + .parser = parse_opt, + .args_doc = 0, + .doc = doc, + .children = NULL, + .help_filter = NULL, + .argp_domain = NULL, +}; + +/* + * @brief Parse the arguments passed into the test case. + * + * @param key The parameter. + * @param arg Argument passed to parameter. + * @param state Location to put information on parameters. + * + * @return error + * + * Get the `input' argument from `argp_parse', which we know is a + * pointer to our arguments structure. + */ +static error_t +parse_opt(int key, char *optarg, struct argp_state *state) +{ + char *endp; + struct args *args = state->input; + + switch (key) { + case 'd': /* --devn= */ + args->devn = strtoul(optarg, &endp, 0); + if (*endp != '\0' || endp == optarg || args->devn < 0) { + fprintf(stderr, "Bad UBI device number: " + "\"%s\"\n", optarg); + goto out; + } + break; + case 'n': /* --volid= */ + args->vol_id = strtoul(optarg, &endp, 0); + if (*endp != '\0' || endp == optarg || + (args->vol_id < 0 && args->vol_id != UBI_DYNAMIC_VOLUME)) { + fprintf(stderr, "Bad volume ID: " + "\"%s\"\n", optarg); + goto out; + } + break; + case ':': + fprintf(stderr, "Parameter is missing\n"); + goto out; + + case ARGP_KEY_NO_ARGS: + /* argp_usage(state); */ + break; + + case ARGP_KEY_ARG: + args->arg1 = optarg; + /* Now we consume all the rest of the arguments. + `state->next' is the index in `state->argv' of the + next argument to be parsed, which is the first STRING + we're interested in, so we can just use + `&state->argv[state->next]' as the value for + arguments->strings. + + _In addition_, by setting `state->next' to the end + of the arguments, we can force argp to stop parsing + here and return. */ + + args->options = &state->argv[state->next]; + state->next = state->argc; + break; + + case ARGP_KEY_END: + /* argp_usage(state); */ + break; + + default: + return(ARGP_ERR_UNKNOWN); + } + + return 0; + out: + return(ARGP_ERR_UNKNOWN); +} + +static int param_sanity_check(struct args *args, ubi_lib_t lib) +{ + int err; + struct ubi_info ubi; + + if (args->vol_id == -1) { + fprintf(stderr, "Volume ID was not specified\n"); + goto out; + } + + err = ubi_get_info(lib, &ubi); + if (err) + return -1; + + if (args->devn >= (int)ubi.dev_count) { + fprintf(stderr, "Device %d does not exist\n", args->devn); + goto out; + } + + return 0; + +out: + errno = EINVAL; + return -1; +} int main(int argc, char * const argv[]) { int err, old_errno; ubi_lib_t lib; - err = parse_options(argc, argv); + err = argp_parse(&argp, argc, (char **)argv, ARGP_IN_ORDER, 0, + &myargs); if (err) return err == 1 ? 0 : -1; - if (devn == -1) { + if (myargs.devn == -1) { fprintf(stderr, "Device number was not specified\n"); fprintf(stderr, "Use -h option for help\n"); return -1; @@ -63,14 +218,14 @@ int main(int argc, char * const argv[]) return -1; } - err = param_sanity_check(lib); + err = param_sanity_check(&myargs, lib); if (err) { perror("Input parameters check"); fprintf(stderr, "Use -h option for help\n"); goto out_libubi; } - err = ubi_rmvol(lib, devn, vol_id); + err = ubi_rmvol(lib, myargs.devn, myargs.vol_id); old_errno = errno; if (err < 0) { perror("Cannot remove volume"); @@ -84,91 +239,3 @@ out_libubi: ubi_close(&lib); return -1; } - -/* 'getopt()' option string */ -static const char *optstring = "hd:n:"; - -static int parse_options(int argc, char * const argv[]) -{ - int opt = 0; - - while (opt != -1) { - char *endp; - - opt = getopt(argc, argv, optstring); - - switch (opt) { - case 'h': - usage(); - return 1; - case 'n': - vol_id = strtoul(optarg, &endp, 0); - if (*endp != '\0' || endp == optarg || vol_id < 0) { - fprintf(stderr, "Bad volume " - "number: \"%s\"\n", optarg); - goto out; - } - break; - case 'd': - devn = strtoul(optarg, &endp, 0); - if (*endp != '\0' || endp == optarg || devn < 0) { - fprintf(stderr, "Bad UBI device " - "number: \"%s\"\n", optarg); - goto out; - } - break; - case ':': - fprintf(stderr, "Parameter is missing\n"); - goto out; - case '?': - fprintf(stderr, "Unknown parameter\n"); - goto out; - case -1: - break; - default: - fprintf(stderr, "Internal error\n"); - goto out; - } - } - - return 0; - -out: - errno = EINVAL; - return -1; -} - -static int param_sanity_check(ubi_lib_t lib) -{ - int err; - struct ubi_info ubi; - - if (vol_id == -1) { - fprintf(stderr, "Volume ID was not specified\n"); - goto out; - } - - err = ubi_get_info(lib, &ubi); - if (err) - return -1; - - if (devn >= (int)ubi.dev_count) { - fprintf(stderr, "Device %d does not exist\n", devn); - goto out; - } - - return 0; - -out: - errno = EINVAL; - return -1; -} - -static void usage(void) -{ - printf("Usage: ubi_rmvol OPTIONS\n" - "Command line options:\n" - "\t-h - this help message\n" - "\t-d - UBI device number\n" - "\t-n VOLNUM - volume number to remove\n"); -} diff --git a/ubi-utils/src/ubiupdatevol.c b/ubi-utils/src/ubiupdatevol.c index 28e1e8f..fd68dd8 100644 --- a/ubi-utils/src/ubiupdatevol.c +++ b/ubi-utils/src/ubiupdatevol.c @@ -14,14 +14,16 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/* + * An utility to update UBI volumes. * * Author: Frank Haverkamp * - * An utility to update UBI volumes. + * 1.0 Reworked the userinterface to use argp. */ -#include - #include #include #include @@ -36,15 +38,18 @@ #include #include +#include #include +#define VERSION "1.0" + #define MAXPATH 1024 #define BUFSIZE 128 * 1024 #define MIN(x,y) ((x)<(y)?(x):(y)) struct args { - int device; - int volume; + int devn; + int vol_id; int truncate; int broken_update; int bufsize; @@ -55,8 +60,8 @@ struct args { }; static struct args myargs = { - .device = -1, - .volume = -1, + .devn = -1, + .vol_id = -1, .truncate = 0, .broken_update = 0, .bufsize = BUFSIZE, @@ -64,25 +69,24 @@ static struct args myargs = { .options = NULL, }; -static int verbose = 0; - static error_t parse_opt (int key, char *arg, struct argp_state *state); +static int verbose = 0; const char *argp_program_bug_address = PACKAGE_BUGREPORT; -static char doc[] = "\nVersion: " PACKAGE_VERSION "\n\t" +static char doc[] = "\nVersion: " VERSION "\n\t" BUILD_OS" "BUILD_CPU" at "__DATE__" "__TIME__"\n" "\nWrite to UBI Volume.\n"; static struct argp_option options[] = { - { .name = "device", + { .name = "devn", .key = 'd', - .arg = "", + .arg = "", .flags = 0, .doc = "UBI device", .group = OPTION_ARG_OPTIONAL }, - { .name = "volume", + { .name = "vol_id", .key = 'n', .arg = "", .flags = 0, @@ -139,8 +143,12 @@ parse_opt(int key, char *arg, struct argp_state *state) verbose = strtoul(arg, (char **)NULL, 0); break; - case 'd': /* --device= */ - args->device = strtol(arg, (char **)NULL, 0); + case 'n': /* --vol_id= */ + args->vol_id = strtol(arg, (char **)NULL, 0); + break; + + case 'd': /* --devn= */ + args->devn = strtol(arg, (char **)NULL, 0); break; case 'b': /* --bufsize= */ @@ -157,10 +165,6 @@ parse_opt(int key, char *arg, struct argp_state *state) args->broken_update = 1; break; - case 'n': /* --volume= */ - args->volume = strtol(arg, (char **)NULL, 0); - break; - case ARGP_KEY_NO_ARGS: /* argp_usage(state); */ break; @@ -175,8 +179,8 @@ parse_opt(int key, char *arg, struct argp_state *state) arguments->strings. _In addition_, by setting `state->next' to the end - of the arguments, we can force argp to stop parsing here and - return. */ + of the arguments, we can force argp to stop parsing + here and return. */ args->options = &state->argv[state->next]; state->next = state->argc; @@ -205,7 +209,7 @@ ubi_truncate_volume(struct args *args, int64_t bytes) char path[MAXPATH]; int old_errno; - snprintf(path, MAXPATH-1, "/dev/ubi%d_%d", args->device, args->volume); + snprintf(path, MAXPATH-1, "/dev/ubi%d_%d", args->devn, args->vol_id); path[MAXPATH-1] = '\0'; ofd = open(path, O_RDWR); @@ -279,7 +283,7 @@ ubi_update_volume(struct args *args) if (!ifp) exit(EXIT_FAILURE); - snprintf(path, MAXPATH-1, "/dev/ubi%d_%d", args->device, args->volume); + snprintf(path, MAXPATH-1, "/dev/ubi%d_%d", args->devn, args->vol_id); path[MAXPATH-1] = '\0'; ofd = open(path, O_RDWR); -- cgit v1.2.3 From 55d6da34828c784b7f3f9ffddb4a6dd5bdf65d46 Mon Sep 17 00:00:00 2001 From: Adrian Hunter Date: Fri, 16 Mar 2007 18:06:30 +0200 Subject: UBI-Utils: Updated headers for latest UBI Signed-off-by: Adrian Hunter --- include/mtd/ubi-header.h | 309 ++++++++++++++++++++++------------------------- include/mtd/ubi-user.h | 66 +++++++--- 2 files changed, 193 insertions(+), 182 deletions(-) (limited to 'include/mtd') diff --git a/include/mtd/ubi-header.h b/include/mtd/ubi-header.h index ca96fc9..c003d98 100644 --- a/include/mtd/ubi-header.h +++ b/include/mtd/ubi-header.h @@ -24,36 +24,30 @@ /* * This file defines the layout of UBI headers and all the other UBI on-flash - * data structures. + * data structures. May be included by user-space. */ #ifndef __UBI_HEADER_H__ #define __UBI_HEADER_H__ -#include +#include -/* The version of this UBI implementation */ +/* The version of UBI images supported by this implementation */ #define UBI_VERSION 1 -/* The highest erase counter value supported by this implementation of UBI */ +/* The highest erase counter value supported by this implementation */ #define UBI_MAX_ERASECOUNTER 0x7FFFFFFF /* The initial CRC32 value used when calculating CRC checksums */ #define UBI_CRC32_INIT 0xFFFFFFFFU -/* - * Magic numbers of the UBI headers. - * - * @UBI_EC_HDR_MAGIC: erase counter header magic number (ASCII "UBI#") - * @UBI_VID_HDR_MAGIC: volume identifier header magic number (ASCII "UBI!") - */ -enum { - UBI_EC_HDR_MAGIC = 0x55424923, - UBI_VID_HDR_MAGIC = 0x55424921 -}; +/* Erase counter header magic number (ASCII "UBI#") */ +#define UBI_EC_HDR_MAGIC 0x55424923 +/* Volume identifier header magic number (ASCII "UBI!") */ +#define UBI_VID_HDR_MAGIC 0x55424921 /* - * Molume type constants used in volume identifier headers. + * Volume type constants used in the volume identifier header. * * @UBI_VID_DYNAMIC: dynamic volume * @UBI_VID_STATIC: static volume @@ -97,59 +91,50 @@ typedef struct { } __attribute__ ((packed)) ubi64_t; /* - * In this implementation UBI uses the big-endian format for on-flash integers. - * The below are the corresponding endianess conversion macros. + * In this implementation of UBI uses the big-endian format for on-flash + * integers. The below are the corresponding conversion macros. */ -#define cpu_to_ubi16(x) ((ubi16_t){cpu_to_be16(x)}) -#define ubi16_to_cpu(x) ((uint16_t)be16_to_cpu((x).int16)) +#define cpu_to_ubi16(x) ((ubi16_t){__cpu_to_be16(x)}) +#define ubi16_to_cpu(x) ((uint16_t)__be16_to_cpu((x).int16)) -#define cpu_to_ubi32(x) ((ubi32_t){cpu_to_be32(x)}) -#define ubi32_to_cpu(x) ((uint32_t)be32_to_cpu((x).int32)) +#define cpu_to_ubi32(x) ((ubi32_t){__cpu_to_be32(x)}) +#define ubi32_to_cpu(x) ((uint32_t)__be32_to_cpu((x).int32)) -#define cpu_to_ubi64(x) ((ubi64_t){cpu_to_be64(x)}) -#define ubi64_to_cpu(x) ((uint64_t)be64_to_cpu((x).int64)) +#define cpu_to_ubi64(x) ((ubi64_t){__cpu_to_be64(x)}) +#define ubi64_to_cpu(x) ((uint64_t)__be64_to_cpu((x).int64)) -/* - * Sizes of UBI headers. - */ +/* Sizes of UBI headers */ #define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr) #define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr) -/* - * Sizes of UBI headers without the ending CRC. - */ +/* Sizes of UBI headers without the ending CRC */ #define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(ubi32_t)) #define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(ubi32_t)) -/* - * How much private data may internal volumes store in the VID header. - */ -#define UBI_VID_HDR_IVOL_DATA_SIZE 12 - /** * struct ubi_ec_hdr - UBI erase counter header. - * - * @magic: the erase counter header magic number (%UBI_EC_HDR_MAGIC) - * @version: the version of UBI implementation which is supposed to accept this - * UBI image (%UBI_VERSION) + * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC) + * @version: version of UBI implementation which is supposed to accept this + * UBI image * @padding1: reserved for future, zeroes * @ec: the erase counter - * @vid_hdr_offset: where the VID header begins - * @data_offset: where the user data begins + * @vid_hdr_offset: where the VID header starts + * @data_offset: where the user data start * @padding2: reserved for future, zeroes - * @hdr_crc: the erase counter header CRC checksum + * @hdr_crc: erase counter header CRC checksum * * The erase counter header takes 64 bytes and has a plenty of unused space for * future usage. The unused fields are zeroed. The @version field is used to * indicate the version of UBI implementation which is supposed to be able to * work with this UBI image. If @version is greater then the current UBI - * version, the image is rejecter. This may be useful in future if something + * version, the image is rejected. This may be useful in future if something * is changed radically. This field is duplicated in the volume identifier * header. * * The @vid_hdr_offset and @data_offset fields contain the offset of the the * volume identifier header and user data, relative to the beginning of the - * eraseblock. These values have to be the same for all eraseblocks. + * physical eraseblock. These values have to be the same for all physical + * eraseblocks. */ struct ubi_ec_hdr { ubi32_t magic; @@ -164,77 +149,108 @@ struct ubi_ec_hdr { /** * struct ubi_vid_hdr - on-flash UBI volume identifier header. - * * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC) * @version: UBI implementation version which is supposed to accept this UBI * image (%UBI_VERSION) * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC) - * @copy_flag: a flag indicating if this physical eraseblock was created by - * means of copying an original physical eraseblock to ensure wear-leveling. - * @compat: compatibility of this volume (%UBI_COMPAT_DELETE, + * @copy_flag: if this logical eraseblock was copied from another physical + * eraseblock (for wear-leveling reasons) + * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE, * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT) - * @vol_id: volume ID + * @vol_id: ID of this volume * @lnum: logical eraseblock number - * @leb_ver: eraseblock copy number - * @data_size: how many bytes of data this eraseblock contains. + * @leb_ver: version of this logical eraseblock (IMPORTANT: obsolete, to be + * removed, kept only for not breaking older UBI users) + * @data_size: how many bytes of data this logical eraseblock contains * @used_ebs: total number of used logical eraseblocks in this volume - * @data_pad: how many bytes at the end of this eraseblock are not used - * @data_crc: CRC checksum of data containing in this eraseblock + * @data_pad: how many bytes at the end of this physical eraseblock are not + * used + * @data_crc: CRC checksum of the data stored in this logical eraseblock * @padding1: reserved for future, zeroes - * @ivol_data: private data of internal volumes + * @sqnum: sequence number + * @padding2: reserved for future, zeroes * @hdr_crc: volume identifier header CRC checksum * - * The @leb_ver and the @copy_flag fields are used to distinguish between older - * and newer copies of logical eraseblocks, as well as to guarantee robustness - * to unclean reboots. As UBI erases logical eraseblocks asynchronously, it has - * to distinguish between older and newer copies of eraseblocks. This is done - * using the @version field. On the other hand, when UBI moves an eraseblock, - * its version is also increased and the @copy_flag is set to 1. Additionally, - * when moving eraseblocks, UBI calculates data CRC and stores it in the - * @data_crc field, even for dynamic volumes. + * The @sqnum is the value of the global sequence counter at the time when this + * VID header was created. The global sequence counter is incremented each time + * UBI writes a new VID header to the flash, i.e. when it maps a logical + * eraseblock to a new physical eraseblock. The global sequence counter is an + * unsigned 64-bit integer and we assume it never overflows. The @sqnum + * (sequence number) is used to distinguish between older and newer versions of + * logical eraseblocks. + * + * There are 2 situations when there may be more then one physical eraseblock + * corresponding to the same logical eraseblock, i.e., having the same @vol_id + * and @lnum values in the volume identifier header. Suppose we have a logical + * eraseblock L and it is mapped to the physical eraseblock P. + * + * 1. Because UBI may erase physical eraseblocks asynchronously, the following + * situation is possible: L is asynchronously erased, so P is scheduled for + * erasure, then L is written to,i.e. mapped to another physical eraseblock P1, + * so P1 is written to, then an unclean reboot happens. Result - there are 2 + * physical eraseblocks P and P1 corresponding to the same logical eraseblock + * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the + * flash. * - * Thus, if there are 2 eraseblocks of the same volume and logical number, UBI - * uses the following algorithm to pick one of them. It first picks the one - * with larger version (say, A). If @copy_flag is not set, then A is picked. If - * @copy_flag is set, UBI checks the CRC of the eraseblock (@data_crc). This is - * needed to ensure that copying was finished. If the CRC is all right, A is - * picked. If not, the older eraseblock is picked. + * 2. From time to time UBI moves logical eraseblocks to other physical + * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P + * to P1, and an unclean reboot happens before P is physically erased, there + * are two physical eraseblocks P and P1 corresponding to L and UBI has to + * select one of them when the flash is attached. The @sqnum field says which + * PEB is the original (obviously P will have lower @sqnum) and the copy. But + * it is not enough to select the physical eraseblock with the higher sequence + * number, because the unclean reboot could have happen in the middle of the + * copying process, so the data in P is corrupted. It is also not enough to + * just select the physical eraseblock with lower sequence number, because the + * data there may be old (consider a case if more data was added to P1 after + * the copying). Moreover, the unclean reboot may happen when the erasure of P + * was just started, so it result in unstable P, which is "mostly" OK, but + * still has unstable bits. * - * Note, the @leb_ver field may overflow. Thus, if you have 2 versions A and B, - * then A > B if abs(A-B) < 0x7FFFFFFF, and A < B otherwise. + * UBI uses the @copy_flag field to indicate that this logical eraseblock is a + * copy. UBI also calculates data CRC when the data is moved and stores it at + * the @data_crc field of the copy (P1). So when UBI needs to pick one physical + * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is + * examined. If it is cleared, the situation* is simple and the newer one is + * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC + * checksum is correct, this physical eraseblock is selected (P1). Otherwise + * the older one (P) is selected. + * + * Note, there is an obsolete @leb_ver field which was used instead of @sqnum + * in the past. But it is not used anymore and we keep it in order to be able + * to deal with old UBI images. It will be removed at some point. * * There are 2 sorts of volumes in UBI: user volumes and internal volumes. - * Internal volumes are not seen from outside and are used for different - * internal UBI purposes. In this implementation there are only two internal - * volumes: the layout volume and the update volume. Internal volumes are the - * main mechanism of UBI extensions. For example, in future one may introduce a - * journal internal volume. + * Internal volumes are not seen from outside and are used for various internal + * UBI purposes. In this implementation there is only one internal volume - the + * layout volume. Internal volumes are the main mechanism of UBI extensions. + * For example, in future one may introduce a journal internal volume. Internal + * volumes have their own reserved range of IDs. * - * The @compat field is only used for internal volumes and contains the degree - * of their compatibility. This field is always zero for user volumes. This - * field provides a mechanism to introduce UBI extensions and to be still - * compatible with older UBI binaries. For example, if someone introduced an - * journal internal volume in future, he would probably use %UBI_COMPAT_DELETE - * compatibility. And in this case, older UBI binaries, which know nothing - * about the journal volume, would just delete this and work perfectly fine. - * This is somewhat similar to what Ext2fs does when it is fed by an Ext3fs - * image - it just ignores the Ext3fs journal. + * The @compat field is only used for internal volumes and contains the "degree + * of their compatibility". It is always zero for user volumes. This field + * provides a mechanism to introduce UBI extensions and to be still compatible + * with older UBI binaries. For example, if someone introduced a journal in + * future, he would probably use %UBI_COMPAT_DELETE compatibility for the + * journal volume. And in this case, older UBI binaries, which know nothing + * about the journal volume, would just delete this volume and work perfectly + * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image + * - it just ignores the Ext3fs journal. * * The @data_crc field contains the CRC checksum of the contents of the logical - * eraseblock if this is a static volume. In case of dynamic volumes, it does + * eraseblock if this is a static volume. In case of dynamic volumes, it does * not contain the CRC checksum as a rule. The only exception is when the - * logical eraseblock was moved by the wear-leveling unit, then the - * wear-leveling unit calculates the eraseblocks' CRC and stores it at - * @data_crc. + * data of the physical eraseblock was moved by the wear-leveling unit, then + * the wear-leveling unit calculates the data CRC and stores it in the + * @data_crc field. And of course, the @copy_flag is %in this case. * - * The @data_size field is always used for static volumes because we want to - * know about how many bytes of data are stored in this eraseblock. For - * dynamic eraseblocks, this field usually contains zero. The only exception is - * when the logical eraseblock is moved to another physical eraseblock due to + * The @data_size field is used only for static volumes because UBI has to know + * how many bytes of data are stored in this eraseblock. For dynamic volumes, + * this field usually contains zero. The only exception is when the data of the + * physical eraseblock was moved to another physical eraseblock for * wear-leveling reasons. In this case, UBI calculates CRC checksum of the * contents and uses both @data_crc and @data_size fields. In this case, the - * @data_size field contains the size of logical eraseblock of this volume - * (which may vary owing to @alignment). + * @data_size field contains data size. * * The @used_ebs field is used only for static volumes and indicates how many * eraseblocks the data of the volume takes. For dynamic volumes this field is @@ -244,11 +260,6 @@ struct ubi_ec_hdr { * parameter. So, effectively, the @data_pad field reduces the size of logical * eraseblocks of this volume. This is very handy when one uses block-oriented * software (say, cramfs) on top of the UBI volume. - * - * The @ivol_data contains private data of internal volumes. This might be very - * handy to store data in the VID header, not in the eraseblock's contents. For - * example it may make life of simple boot-loaders easier. The @ivol_data field - * contains zeroes for user volumes. */ struct ubi_vid_hdr { ubi32_t magic; @@ -258,117 +269,87 @@ struct ubi_vid_hdr { uint8_t compat; ubi32_t vol_id; ubi32_t lnum; - ubi32_t leb_ver; + ubi32_t leb_ver; /* obsolete, to be removed, don't use */ ubi32_t data_size; ubi32_t used_ebs; ubi32_t data_pad; ubi32_t data_crc; - uint8_t padding1[12]; - uint8_t ivol_data[UBI_VID_HDR_IVOL_DATA_SIZE]; + uint8_t padding1[4]; + ubi64_t sqnum; + uint8_t padding2[12]; ubi32_t hdr_crc; } __attribute__ ((packed)); -/** - * struct ubi_vid_hdr_upd_vol - private data of the update internal volume - * stored in volume identifier headers. - * - * @vol_id: volume ID of the volume under update - * @padding: zeroes - */ -struct ubi_vid_hdr_upd_vol { - ubi32_t vol_id; - uint8_t padding[UBI_VID_HDR_IVOL_DATA_SIZE-4]; -} __attribute__ ((packed)); - -/* - * Count of internal UBI volumes. - */ -#define UBI_INT_VOL_COUNT 2 +/* Internal UBI volumes count */ +#define UBI_INT_VOL_COUNT 1 /* - * Internal volume IDs start from this digit. There is a reserved room for 4096 - * internal volumes. + * Starting ID of internal volumes. There is reserved room for 4096 internal + * volumes. */ #define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096) /* - * enum ubi_internal_volume_numbers - volume IDs of internal UBI volumes. + * IDs of internal UBI volumes. * - * %UBI_LAYOUT_VOL_ID: volume ID of the layout volume - * %UBI_UPDATE_VOL_ID: volume ID of the update volume + * %UBI_LAYOUT_VOL_ID: layout volume ID */ enum { UBI_LAYOUT_VOL_ID = UBI_INTERNAL_VOL_START, - UBI_UPDATE_VOL_ID = UBI_INTERNAL_VOL_START + 1 }; + +/* The layout volume contains the volume table */ -/* - * Number of logical eraseblocks reserved for internal volumes. - */ -#define UBI_LAYOUT_VOLUME_EBS 2 -#define UBI_UPDATE_VOLUME_EBS 1 - -/* - * Names of internal volumes - */ -#define UBI_LAYOUT_VOLUME_NAME "The layout volume" -#define UBI_UPDATE_VOLUME_NAME "The update volume" - -/* - * Compatibility flags of internal volumes. - */ +#define UBI_LAYOUT_VOLUME_EBS 2 +#define UBI_LAYOUT_VOLUME_NAME "The layout volume" #define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT -#define UBI_UPDATE_VOLUME_COMPAT UBI_COMPAT_REJECT -/* - * The maximum number of volumes per one UBI device. - */ +/* The maximum number of volumes per one UBI device */ #define UBI_MAX_VOLUMES 128 -/* - * The maximum volume name length. - */ +/* The maximum volume name length */ #define UBI_VOL_NAME_MAX 127 -/* - * Size of volume table records. - */ +/* Size of the volume table record */ #define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vol_tbl_record) -/* - * Size of volume table records without the ending CRC. - */ +/* Size of the volume table record without the ending CRC */ #define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(ubi32_t)) /** * struct ubi_vol_tbl_record - a record in the volume table. - * * @reserved_pebs: how many physical eraseblocks are reserved for this volume * @alignment: volume alignment - * @data_pad: how many bytes are not used at the end of the eraseblocks to - * satisfy the requested alignment + * @data_pad: how many bytes are unused at the end of the each physical + * eraseblock to satisfy the requested alignment * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) - * @padding1: reserved, zeroes - * @name_len: the volume name length + * @upd_marker: if volume update was started but not finished + * @name_len: volume name length * @name: the volume name * @padding2: reserved, zeroes * @crc: a CRC32 checksum of the record * - * The layout volume consists of 2 logical eraseblock, each of which contains - * the volume table (i.e., the volume table is duplicated). The volume table is - * an array of &struct ubi_vol_tbl_record objects indexed by the volume ID. + * The volume table records are stored in the volume table, which is stored in + * the layout volume. The layout volume consists of 2 logical eraseblock, each + * of which contains a copy of the volume table (i.e., the volume table is + * duplicated). The volume table is an array of &struct ubi_vol_tbl_record + * objects indexed by the volume ID. * * If the size of the logical eraseblock is large enough to fit - * %UBI_MAX_VOLUMES, the volume table contains %UBI_MAX_VOLUMES records. - * Otherwise, it contains as much records as can be fit (i.e., size of logical - * eraseblock divided by sizeof(struct ubi_vol_tbl_record)). + * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES + * records. Otherwise, it contains as many records as it can fit (i.e., size of + * logical eraseblock divided by sizeof(struct ubi_vol_tbl_record)). + * + * The @upd_marker flag is used to implement volume update. It is set to %1 + * before update and set to %0 after the update. So if the update operation was + * interrupted, UBI knows that the volume is corrupted. * * The @alignment field is specified when the volume is created and cannot be * later changed. It may be useful, for example, when a block-oriented file * system works on top of UBI. The @data_pad field is calculated using the * logical eraseblock size and @alignment. The alignment must be multiple to the * minimal flash I/O unit. If @alignment is 1, all the available space of - * eraseblocks is used. + * the physical eraseblocks is used. * * Empty records contain all zeroes and the CRC checksum of those zeroes. */ @@ -377,7 +358,7 @@ struct ubi_vol_tbl_record { ubi32_t alignment; ubi32_t data_pad; uint8_t vol_type; - uint8_t padding1; + uint8_t upd_marker; ubi16_t name_len; uint8_t name[UBI_VOL_NAME_MAX+1]; uint8_t padding2[24]; diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h index 0eb1470..bb0aca6 100644 --- a/include/mtd/ubi-user.h +++ b/include/mtd/ubi-user.h @@ -21,9 +21,43 @@ #ifndef __UBI_USER_H__ #define __UBI_USER_H__ -#ifndef __KERNEL__ -#define __user -#endif +/* + * UBI volume creation + * ~~~~~~~~~~~~~~~~~~~ + * + * UBI volumes are created via the %UBI_IOCMKVOL IOCTL command of UBI character + * device. A &struct ubi_mkvol_req object has to be properly filled and a + * pointer to it has to be passed to the IOCTL. + * + * UBI volume deletion + * ~~~~~~~~~~~~~~~~~~~ + * + * To delete a volume, the %UBI_IOCRMVOL IOCTL command of the UBI character + * device should be used. A pointer to the 32-bit volume ID hast to be passed + * to the IOCTL. + * + * UBI volume re-size + * ~~~~~~~~~~~~~~~~~~ + * + * To re-size a volume, the %UBI_IOCRSVOL IOCTL command of the UBI character + * device should be used. A &struct ubi_rsvol_req object has to be properly + * filled and a pointer to it has to be passed to the IOCTL. + * + * UBI volume update + * ~~~~~~~~~~~~~~~~~ + * + * Volume update should be done via the %UBI_IOCVOLUP IOCTL command of the + * corresponding UBI volume character device. A pointer to a 64-bit update + * size should be passed to the IOCTL. After then, UBI expects user to write + * this number of bytes to the volume character device. The update is finished + * when the claimed number of bytes is passed. So, the volume update sequence + * is something like: + * + * fd = open("/dev/my_volume"); + * ioctl(fd, UBI_IOCVOLUP, &image_size); + * write(fd, buf, image_size); + * close(fd); + */ /* * When a new volume is created, users may either specify the volume number they @@ -35,9 +69,7 @@ /* Maximum volume name length */ #define UBI_MAX_VOLUME_NAME 127 -/* - * IOCTL commands of UBI character devices - */ +/* IOCTL commands of UBI character devices */ #define UBI_IOC_MAGIC 'o' @@ -48,16 +80,14 @@ /* Re-size an UBI volume */ #define UBI_IOCRSVOL _IOW(UBI_IOC_MAGIC, 2, struct ubi_rsvol_req) -/* - * IOCTL commands of UBI volume character devices. - */ +/* IOCTL commands of UBI volume character devices */ #define UBI_VOL_IOC_MAGIC 'O' /* Start UBI volume update */ #define UBI_IOCVOLUP _IOW(UBI_VOL_IOC_MAGIC, 0, int64_t) -/* An eraseblock erasure command, used for debugging, disabled by dafault */ -#define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 0, int32_t) +/* An eraseblock erasure command, used for debugging, disabled by default */ +#define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 1, int32_t) /* * UBI volume type constants. @@ -73,13 +103,13 @@ enum { /** * struct ubi_mkvol_req - volume description data structure used in * volume creation requests. - * * @vol_id: volume number * @alignment: volume alignment * @bytes: volume size in bytes * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) - * @padding: reserved for future, not used + * @padding1: reserved for future, not used * @name_len: volume name length + * @padding2: reserved for future, not used * @name: volume name * * This structure is used by userspace programs when creating new volumes. The @@ -88,7 +118,7 @@ enum { * The @alignment field specifies the required alignment of the volume logical * eraseblock. This means, that the size of logical eraseblocks will be aligned * to this number, i.e., - * (UBI device logical eraseblock size) mod (@alignment) = 0. + * (UBI device logical eraseblock size) mod (@alignment) = 0. * * To put it differently, the logical eraseblock of this volume may be slightly * shortened in order to make it properly aligned. The alignment has to be @@ -99,21 +129,21 @@ enum { * a block device on top of an UBI volume. In this case, it is desirable to fit * an integer number of blocks in logical eraseblocks of this UBI volume. With * alignment it is possible to update this volume using plane UBI volume image - * BLOBs, without caring about how to properly write them. + * BLOBs, without caring about how to properly align them. */ struct ubi_mkvol_req { int32_t vol_id; int32_t alignment; int64_t bytes; int8_t vol_type; - int8_t padding[9]; + int8_t padding1; int16_t name_len; - __user const char *name; + int8_t padding2[4]; + char name[UBI_MAX_VOLUME_NAME+1]; } __attribute__ ((packed)); /** * struct ubi_rsvol_req - a data structure used in volume re-size requests. - * * @vol_id: ID of the volume to re-size * @bytes: new size of the volume in bytes * -- cgit v1.2.3