Skip to contentSkip to conversation

ext4fs write support

PATCH Superseded uma.shankar at samsung.com 4 participants 14 messages Dec 15, 2011 – Aug 13, 2012 ext4 file system

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 ext4fs write support

From: Uma Shankar <[email protected]>

---
 common/cmd_ext4.c      |  135 +++++
 fs/ext4/Makefile       |    2 +-
 fs/ext4/crc16.c        |   62 +++
 fs/ext4/crc16.h        |   16 +
 fs/ext4/ext4_common.c  | 1386 +++++++++++++++++++++++++++++++++++++++++++++++-
 fs/ext4/ext4_common.h  |   17 +
 fs/ext4/ext4_journal.c |  650 +++++++++++++++++++++++
 fs/ext4/ext4_journal.h |  147 +++++
 fs/ext4/ext4fs.c       | 1038 ++++++++++++++++++++++++++++++++++++-
 include/ext4fs.h       |    8 +
 10 files changed, 3457 insertions(+), 4 deletions(-)
 create mode 100644 fs/ext4/crc16.c
 create mode 100644 fs/ext4/crc16.h
 create mode 100644 fs/ext4/ext4_journal.c
 create mode 100644 fs/ext4/ext4_journal.h
common/cmd_ext4.c +135 -0
index 7c2d541..781c6fe 100644
@@ -47,11 +47,53 @@
 
 uint64_t total_sector;
 uint64_t part_offset;
+static unsigned long part_size;
+static int cur_part = 1;
 
 #define DOS_PART_MAGIC_OFFSET		0x1fe
 #define DOS_FS_TYPE_OFFSET		0x36
 #define DOS_FS32_TYPE_OFFSET		0x52
 
+static int ext4_register_device(block_dev_desc_t *dev_desc, int part_no)
+{
+	unsigned char buffer[SECTOR_SIZE];
+	disk_partition_t info;
+
+	if (!dev_desc->block_read)
+		return -1;
+
+	/* check if we have a MBR (on floppies we have only a PBR) */
+	if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
+		printf("** Can't read from device %d **\n", dev_desc->dev);
+		return -1;
+	}
+	if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
+	    buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
+		/* no signature found */
+		return -1;
+	}
+
+	/* First we assume there is a MBR */
+	if (!get_partition_info(dev_desc, part_no, &info)) {
+		part_offset = info.start;
+		cur_part = part_no;
+		part_size = info.size;
+	} else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],
+			    "FAT", 3) == 0) || (strncmp((char *)&buffer
+							[DOS_FS32_TYPE_OFFSET],
+							"FAT32", 5) == 0)) {
+		/* ok, we assume we are on a PBR only */
+		cur_part = 1;
+		part_offset = 0;
+	} else {
+		printf("** Partition %d not valid on device %d **\n",
+		       part_no, dev_desc->dev);
+		return -1;
+	}
+
+	return 0;
+}
+
 int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
 	char *filename = NULL;
@@ -241,6 +283,94 @@ int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 	return 0;
 }
 
+int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+{
+	char *filename = "/";
+	int part_length;
+	unsigned long part = 1;
+	int dev = 0;
+	char *ep;
+	unsigned long ram_address;
+	unsigned long file_size;
+	disk_partition_t info;
+	struct ext_filesystem *fs;
+
+	if (argc < 6)
+		return cmd_usage(cmdtp);
+
+	dev = (int)simple_strtoul(argv[2], &ep, 16);
+	ext4_dev_desc = get_dev(argv[1], dev);
+	if (ext4_dev_desc == NULL) {
+		printf("Block device %s %d not"
+			"supported\n", argv[1], dev);
+		return 1;
+	}
+	if (init_fs(ext4_dev_desc))
+		return 1;
+
+	fs = get_fs();
+	if (*ep) {
+		if (*ep != ':') {
+			puts("Invalid boot device, use `dev[:part]'\n");
+			goto fail;
+		}
+		(int)strict_strtoul(++ep, 16, &part);
+	}
+
+	/*get the filename */
+	filename = argv[3];
+
+	/*get the address in hexadecimal format (string to int) */
+	strict_strtoul(argv[4], 16, &ram_address);
+
+	/*get the filesize in base 10 format */
+	strict_strtoul(argv[5], 10, &file_size);
+
+	/*set the device as block device */
+	part_length = ext2fs_set_blk_dev(fs->dev_desc, part);
+	if (part_length == 0) {
+		printf("Bad partition - %s %d:%lu\n", argv[1], dev, part);
+		goto fail;
+	}
+
+	/*register the device and partition */
+	if (ext4_register_device(fs->dev_desc, part) != 0) {
+		printf("Unable to use %s %d:%lu for fattable\n",
+		       argv[1], dev, part);
+		goto fail;
+	}
+
+	/*get the partition information */
+	if (!get_partition_info(fs->dev_desc, part, &info)) {
+		total_sector = (info.size * info.blksz) / SECTOR_SIZE;
+		fs->total_sect = total_sector;
+	} else {
+		printf("error : get partition info\n");
+		goto fail;
+	}
+
+	/*mount the filesystem */
+	if (!ext4fs_mount(part_length)) {
+		printf("Bad ext4 partition %s %d:%lu\n", argv[1], dev, part);
+		goto fail;
+	}
+
+	/*start write */
+	if (ext4fs_write(filename, (unsigned char *)ram_address, file_size)) {
+		printf("** Error ext4fs_write() **\n");
+		goto fail;
+	}
+
+	ext4fs_close();
+	deinit_fs(fs->dev_desc);
+	return 0;
+
+fail:
+	ext4fs_close();
+	deinit_fs(fs->dev_desc);
+	return 1;
+}
+
 U_BOOT_CMD(ext4ls, 4, 1, do_ext4_ls,
 	   "list files in a directory (default /)",
 	   "<interface> <dev[:part]> [directory]\n"
@@ -251,3 +381,8 @@ U_BOOT_CMD(ext4load, 6, 0, do_ext4_load,
 	   "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
 	   "	  - load binary file 'filename' from 'dev' on 'interface'\n"
 	   "		 to address 'addr' from ext2 filesystem");
+U_BOOT_CMD
+		(ext4write, 6, 1, do_ext4_write,
+		"create a file in the root directory",
+		"<interface> <dev[:part]> [Absolute filename path] [Address] [sizebytes]\n"
+		"	  - create a file in / directory");
fs/ext4/Makefile +1 -1
index 850f821..1e7e7a8 100644
@@ -30,7 +30,7 @@ include $(TOPDIR)/config.mk
 LIB	= $(obj)libext4fs.o
 
 AOBJS	=
-COBJS-$(CONFIG_CMD_EXT4) := ext4fs.o ext4_common.o
+COBJS-$(CONFIG_CMD_EXT4) := ext4fs.o ext4_common.o ext4_journal.o crc16.o
 
 SRCS	:= $(AOBJS:.o=.S) $(COBJS-y:.o=.c)
 OBJS	:= $(addprefix $(obj),$(AOBJS) $(COBJS-y))
fs/ext4/crc16.c new file +62 -0

62 lines not on this page · raw message

fs/ext4/crc16.h new file +16 -0
new file mode 100644
index 0000000..5fd113a
@@ -0,0 +1,16 @@
+/*
+ * crc16.h - CRC-16 routine
+ * Implements the standard CRC-16:
+ *  Width 16
+ *  Poly  0x8005 (x16 + x15 + x2 + 1)
+ *  Init  0
+ *
+ * Copyright (c) 2005 Ben Gardner <[email protected]>
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+ */
+#ifndef __CRC16_H
+#define __CRC16_H
+extern unsigned int ext2fs_crc16(unsigned int crc,
+	const void *buffer, unsigned int len);
+#endif
fs/ext4/ext4_common.c +1384 -2

1,410 lines not on this page · raw message

fs/ext4/ext4_common.h +17 -0
index 73f218d..a2a7e7a 100644
@@ -41,4 +41,21 @@
 extern unsigned long part_offset;
 int ext4fs_read_inode(struct ext2_data *data, int ino,
 		      struct ext2_inode *inode);
+uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n);
+int ext4fs_checksum_update(unsigned int i);
+int get_parent_inode_num(char *dirname, char *dname, int flags);
+void update_parent_dentry(char *filename, int *p_ino, int file_type);
+long int get_new_blk_no(void);
+int get_new_inode_no(void);
+void reset_block_bmap(long int blockno, unsigned char *buffer, int index);
+int set_block_bmap(long int blockno, unsigned char *buffer, int index);
+int set_inode_bmap(int inode_no, unsigned char *buffer, int index);
+void reset_inode_bmap(int inode_no, unsigned char *buffer, int index);
+int iget(int inode_no, struct ext2_inode *inode);
+void allocate_blocks(struct ext2_inode *file_inode,
+		     unsigned int total_remaining_blocks,
+		     unsigned int *total_no_of_block);
+void put_ext4(uint64_t off, void *buf, uint32_t size);
+unsigned int be_le(unsigned int num);
+unsigned int le_be(unsigned int num);
 #endif
fs/ext4/ext4_journal.c new file +650 -0

650 lines not on this page · raw message

fs/ext4/ext4_journal.h new file +147 -0

147 lines not on this page · raw message

fs/ext4/ext4fs.c +1037 -1

531 lines not on this page · raw message

include/ext4fs.h +8 -0
index fd7bd47..e4e2c3e 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.0.4

Part of the diff is not shown here. View the raw message.

#1 lore raw

PATCH 2/2 ext4fs write support

Quoted 2 lines · On Thu, 15 Dec 2011 23:09:53 +0530 <[email protected]> wrote:
 fs/ext4/crc16.c        |   62 +++
 fs/ext4/crc16.h        |   16 +

there are already three crc16 implementations in u-boot according to my count. Please let's not add a fourth - hopefully the only thing wrong with using lib/crc16.c was that you weren't aware it existed.

Quoted 5 lines
common/cmd_ext4.c +3 -0
+	/*get the filename */
 
+	/*get the address in hexadecimal format (string to int) */
 
+	/*get the filesize in base 10 format */

common/cmd_ext4.c int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) · line 358

Quoted 10 lines
common/cmd_ext4.c +5 -0
 
+	/*set the device as block device */
 
+	/*register the device and partition */
 
+	/*get the partition information */
 
+	/*mount the filesystem */
 
+	/*start write */

space after the /*

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 361

Quoted 6 lines
fs/ext4/ext4_common.c +5 -0
+	if (status)
+		*ptr = *ptr & ~(operand);
+	return;
 
+	}
+	return crc;

insert blank line before return statements

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 375

Quoted 4 lines
fs/ext4/ext4_common.c +3 -0
+static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
 
+	dentry_length = sizeof(struct ext2_dirent) +
+	    dir->namelen + padding_factor;

alignment

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 380

Quoted 5 lines
fs/ext4/ext4_common.c +5 -0
+	sizeof_void_space = dir->direntlen - dentry_length;
+	if (sizeof_void_space == 0) {
+		return 0;
+	} else {
+		padding_factor = 0;

if (sizeof_void_space == 0)

	return 0;

padding_factor = 0;
...

Review

Quoted 2 lines
(quoted diff) +2 -0
+		new_entry_byte_reqd = strlen(filename) +
+		    sizeof(struct ext2_dirent) + padding_factor;

alignment

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 390

Quoted 5 lines
fs/ext4/ext4_common.c +5 -0
+		if (sizeof_void_space >= new_entry_byte_reqd) {
+			dir->direntlen = dentry_length;
+			return sizeof_void_space;
+		} else
+			return 0;
if (sizeof_void_space >= new_entry_byte_reqd) {
	dir->direntlen = dentry_length;
	return sizeof_void_space;
}

return 0;

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 418

Quoted 5 lines
fs/ext4/ext4_common.c +5 -0
+	/*##START: Update the root directory entry block of root inode ### */
+	/*since we are populating this file under  root
+	 *directory take the root inode and its first block
+	 *currently we are looking only in first block of root
+	 *inode*/

/*
* multi-line comments
* are formatted
* like this
*/

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 425

Quoted 7 lines
fs/ext4/ext4_common.c +6 -0
+	zero_buffer = xzalloc(fs->blksz);
+	if (!zero_buffer)
+		return;
 
+	root_first_block_buffer = (char *)xzalloc(fs->blksz);
+	if (!root_first_block_buffer)
+		return;

first time reviewing fs code, can this fn be modified to return an error code? If not, at least print something.

Quoted 1 line

+RESTART:

labels in lowercase

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 441

Quoted 2 lines
fs/ext4/ext4_common.c +2 -0
+	status = ext2fs_devread(first_block_no_of_root
+				* fs->sect_perblk,

operator at end of prior line

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 446

Quoted 5 lines
fs/ext4/ext4_common.c +5 -0
+				0, fs->blksz, root_first_block_buffer);
+	if (status == 0)
+		goto fail;
+	else {
+		if (log_journal(root_first_block_buffer,

if (...)

	goto fail;

if (log_journal(...

this will save horizontal space for some tightly right-justified code under it.

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 475

Quoted 2 lines
fs/ext4/ext4_common.c +2 -0
+				last_entry_dirlen = dir->namelen +
+				    sizeof(struct ext2_dirent) + padding_factor;

alignment

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 477

Quoted 2 lines
fs/ext4/ext4_common.c +2 -0
+				if ((fs->blksz - totalbytes - last_entry_dirlen)
+				    < new_entry_byte_reqd) {

operator on prior line

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 479

Quoted 2 lines
fs/ext4/ext4_common.c +2 -0
+					printf("1st Block Full:"
+					"allocating new block\n");

here,

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 484

Quoted 4 lines
fs/ext4/ext4_common.c +4 -0
+					if (direct_blk_idx ==
+					    INDIRECT_BLOCKS - 1) {
+						printf("Directory capacity"
+						"exceeds the limit\n");

here,

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 492

Quoted 8 lines
fs/ext4/ext4_common.c +8 -0
+						goto fail;
+					}
+					g_parent_inode->b.blocks.dir_blocks
+					    [direct_blk_idx] = get_new_blk_no();
+					if (g_parent_inode->b.blocks.dir_blocks
+					    [direct_blk_idx] == -1) {
+						printf("no block"
+							"left to assign\n");

and here, this prevents users trying to grep for this error text - I believe it's allowed to exceed 80 columns in this case.

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 560

Quoted 1 line
fs/ext4/ext4_common.c +1 -0
+	/*END: Update the root directory entry block of root inode */

is some tool you're using supposed to correlate this with the above?:

/*##START: Update the root directory entry block of root inode ### */

If so, we (or certainly I) don't use it, and therefore don't care for it - please remove it, and any other instances.

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 575

Quoted 2 lines
fs/ext4/ext4_common.c +2 -0
+	/*get the first block of root */
+	/*read the block no allocated to a file */

fix comment format

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 591

Quoted 5 lines
fs/ext4/ext4_common.c +5 -0
+		status = ext2fs_devread(blknr * fs->sect_perblk,
+					0, fs->blksz, (char *)block_buffer);
+		if (status == 0)
+			goto fail;
+		else {

again, rm the else to avoid unnecessary indentation

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 600

Quoted 6 lines
fs/ext4/ext4_common.c +6 -0
+			while (dir->direntlen >= 0) {
+				/*blocksize-totalbytes because last directory
+				 *length i.e.,*dir->direntlen is free availble
+				 *space in the block that means
+				 *it is a last entry of directory entry
+				 */

fix comment format

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 673

Quoted 4 lines
fs/ext4/ext4_common.c +4 -0
+	/*add root */
+	arr[i] = xzalloc(strlen("/") + 1);
+	if (!arr[i])
+		return -1;

return -ENOMEM?

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 697

Quoted 7 lines
fs/ext4/ext4_common.c +7 -0
+int iget(int inode_no, struct ext2_inode *inode)
+{
+	if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
+		return -1;
+	else
+		return 0;
+}

rm else

fs/ext4/ext4_common.c void *xzalloc(size_t size) · line 810

Quoted 11 lines
fs/ext4/ext4_common.c +11 -0
+fail:
+	if (depth_dirname)
+		free(depth_dirname);
+	if (parse_dirname)
+		free(parse_dirname);
+	if (ptr)
+		free(ptr);
+	if (parent_inode)
+		free(parent_inode);
+	if (first_inode)
+		free(first_inode);

free(0) is already protected for you

fs/ext4/ext4_common.c int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode) · line 1234

Quoted 4 lines
fs/ext4/ext4_common.c +4 -0
+	/*single indirect */
+	unsigned int *SI_buffer = NULL;
+	long int SI_blockno;
+	unsigned int *SI_start_addr = NULL;

no caps in variable names

fs/ext4/ext4_common.c int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode) · line 1505

Quoted 1 line
fs/ext4/ext4_common.c +1 -0
+	/*allocation of direct blocks */

this

fs/ext4/ext4_common.c int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode) · line 1530

Quoted 11 lines
fs/ext4/ext4_common.c +11 -0
+	/*allocation of single indirect blocks */
+	alloc_single_indirect_block(file_inode, &total_remaining_blocks,
+				    &no_blks_reqd);
+
+	/*allocation of double indirect blocks */
+	alloc_double_indirect_block(file_inode, &total_remaining_blocks,
+				    &no_blks_reqd);
+
+	/*allocation of triple indirect blocks */
+	alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
+				    &no_blks_reqd);

and these three comments don't seem to add any real value, given the names of the functions.

fs/ext4/ext4fs.c int ext4fs_read(char *buf, unsigned len) · line 476

Quoted 2 lines
fs/ext4/ext4fs.c +2 -0
+	if (inode->b.blocks.tripple_indir_block != 0) {
+		TIGP_buffer = (unsigned int *)xzalloc(fs->blksz);

s/tripple/triple/g

s/TIGP/tigp/g

also buy horizontal indentation space via

	if (inode->b.blocks.tripple_indir_block == 0)
		goto fail;
	TIGP_buffer = (unsigned int *)xzalloc(fs->blksz);
	...

fs/ext4/ext4fs.c int ext4fs_read(char *buf, unsigned len) · line 501

Quoted 4 lines
fs/ext4/ext4fs.c +4 -0
+				if (fs->blksz != 1024) {
+					bg_idx = (*TIP_buffer) / (uint32_t)
+					    ext4fs_root->sblock.
+					    blocks_per_group;

also assign ext4fs_root->sblock.blocks_per_group to a function local variable, to avoid tight right-justification like this.

This is a lot of code with recurring CodingStyle review patterns, so I'm stopping my review here. Please detect and fix the other occurrences and resubmit.

Thank you for your contribution!

Kim

#2 lore raw

PATCH 2/2 ext4fs write support

In reply to uma.shankar at samsung.com

this needs to be behind a CONFIG_EXT4_WRITE or something similar. most people want to read their fs's, not write them.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20111216/77ca0825/attachment.pgp>

#3 lore raw

PATCH v3 2/2 ext4fs write support

In reply to uma.shankar at samsung.com

From: Uma Shankar <[email protected]>

---
Changes for v2:
- Code cleanup, changed comment style
- camel case removed, resolved code alignment issues
- memory allocation logic changed, removed busybox logic
- Modified ext4 load to remove grub dependency (GPLv3)
- Introduced new Config for ext4 write

Changes for v1:
- Removed checkpatch warnings and errors
- Moved common API's of ext2 and ext4 to one generic header file

 common/cmd_ext4.c      |  141 +++++
 fs/ext4/Makefile       |    1 +
 fs/ext4/README         |   46 ++
 fs/ext4/crc16.c        |   62 +++
 fs/ext4/crc16.h        |   16 +
 fs/ext4/ext4_common.c  | 1380 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/ext4_common.h  |   20 +
 fs/ext4/ext4_journal.c |  662 +++++++++++++++++++++++
 fs/ext4/ext4_journal.h |  147 +++++
 fs/ext4/ext4fs.c       |  972 ++++++++++++++++++++++++++++++++++
 include/ext4fs.h       |   12 +
 11 files changed, 3459 insertions(+), 0 deletions(-)
 create mode 100644 fs/ext4/README
 create mode 100644 fs/ext4/crc16.c
 create mode 100644 fs/ext4/crc16.h
 create mode 100644 fs/ext4/ext4_journal.c
 create mode 100644 fs/ext4/ext4_journal.h
common/cmd_ext4.c +141 -0
index 85343db..b7c3ae0 100644
@@ -47,6 +47,10 @@
 
 uint64_t total_sector;
 uint64_t part_offset;
+#if defined(CONFIG_CMD_EXT4_WRITE)
+static uint64_t part_size;
+static uint16_t cur_part = 1;
+#endif
 
 #define DOS_PART_MAGIC_OFFSET		0x1fe
 #define DOS_FS_TYPE_OFFSET		0x36
@@ -234,9 +238,146 @@ static int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 
 	ext4fs_close();
 	deinit_fs(fs->dev_desc);
+
+	return 0;
+}
+
+#if defined(CONFIG_CMD_EXT4_WRITE)
+static int ext4_register_device(block_dev_desc_t *dev_desc, int part_no)
+{
+	unsigned char buffer[SECTOR_SIZE];
+	disk_partition_t info;
+
+	if (!dev_desc->block_read)
+		return -1;
+
+	/* check if we have a MBR (on floppies we have only a PBR) */
+	if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
+		printf("** Can't read from device %d **\n", dev_desc->dev);
+		return -1;
+	}
+	if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
+	    buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
+		/* no signature found */
+		return -1;
+	}
+
+	/* First we assume there is a MBR */
+	if (!get_partition_info(dev_desc, part_no, &info)) {
+		part_offset = info.start;
+		cur_part = part_no;
+		part_size = info.size;
+	} else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],
+			    "FAT", 3) == 0) || (strncmp((char *)&buffer
+							[DOS_FS32_TYPE_OFFSET],
+							"FAT32", 5) == 0)) {
+		/* ok, we assume we are on a PBR only */
+		cur_part = 1;
+		part_offset = 0;
+	} else {
+		printf("** Partition %d not valid on device %d **\n",
+		       part_no, dev_desc->dev);
+		return -1;
+	}
+
 	return 0;
 }
 
+int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+{
+	char *filename = "/";
+	int part_length;
+	unsigned long part = 1;
+	int dev = 0;
+	char *ep;
+	unsigned long ram_address;
+	unsigned long file_size;
+	disk_partition_t info;
+	struct ext_filesystem *fs;
+
+	if (argc < 6)
+		return cmd_usage(cmdtp);
+
+	dev = (int)simple_strtoul(argv[2], &ep, 16);
+	ext4_dev_desc = get_dev(argv[1], dev);
+	if (ext4_dev_desc == NULL) {
+		printf("Block device %s %d not supported\n", argv[1], dev);
+		return 1;
+	}
+	if (init_fs(ext4_dev_desc))
+		return 1;
+
+	fs = get_fs();
+	if (*ep) {
+		if (*ep != ':') {
+			puts("Invalid boot device, use `dev[:part]'\n");
+			goto fail;
+		}
+		strict_strtoul(++ep, 16, &part);
+	}
+
+	/*get the filename */
+	filename = argv[3];
+
+	/*get the address in hexadecimal format (string to int) */
+	strict_strtoul(argv[4], 16, &ram_address);
+
+	/*get the filesize in base 10 format */
+	strict_strtoul(argv[5], 10, &file_size);
+
+	/*set the device as block device */
+	part_length = ext2fs_set_blk_dev(fs->dev_desc, part);
+	if (part_length == 0) {
+		printf("Bad partition - %s %d:%lu\n", argv[1], dev, part);
+		goto fail;
+	}
+
+	/*register the device and partition */
+	if (ext4_register_device(fs->dev_desc, part) != 0) {
+		printf("Unable to use %s %d:%lu for fattable\n",
+		       argv[1], dev, part);
+		goto fail;
+	}
+
+	/*get the partition information */
+	if (!get_partition_info(fs->dev_desc, part, &info)) {
+		total_sector = (info.size * info.blksz) / SECTOR_SIZE;
+		fs->total_sect = total_sector;
+	} else {
+		printf("error : get partition info\n");
+		goto fail;
+	}
+
+	/*mount the filesystem */
+	if (!ext4fs_mount(part_length)) {
+		printf("Bad ext4 partition %s %d:%lu\n", argv[1], dev, part);
+		goto fail;
+	}
+
+	/*start write */
+	if (ext4fs_write(filename, (unsigned char *)ram_address, file_size)) {
+		printf("** Error ext4fs_write() **\n");
+		goto fail;
+	}
+	ext4fs_close();
+	deinit_fs(fs->dev_desc);
+
+	return 0;
+
+fail:
+	ext4fs_close();
+	deinit_fs(fs->dev_desc);
+
+	return 1;
+}
+
+U_BOOT_CMD(ext4write, 6, 1, do_ext4_write,
+	"create a file in the root directory",
+	"<interface> <dev[:part]> [Absolute filename path] [Address] [sizebytes]\n"
+	"	  - create a file in / directory");
+
+#endif
+
 U_BOOT_CMD(ext4ls, 4, 1, do_ext4_ls,
 	   "list files in a directory (default /)",
 	   "<interface> <dev[:part]> [directory]\n"
fs/ext4/Makefile +1 -0
index 850f821..548d464 100644
@@ -31,6 +31,7 @@ LIB	= $(obj)libext4fs.o
 
 AOBJS	=
 COBJS-$(CONFIG_CMD_EXT4) := ext4fs.o ext4_common.o
+COBJS-$(CONFIG_CMD_EXT4_WRITE) += ext4_journal.o crc16.o
 
 SRCS	:= $(AOBJS:.o=.S) $(COBJS-y:.o=.c)
 OBJS	:= $(addprefix $(obj),$(AOBJS) $(COBJS-y))
fs/ext4/README new file +46 -0

46 lines not on this page · raw message

fs/ext4/crc16.c new file +62 -0

62 lines not on this page · raw message

fs/ext4/crc16.h new file +16 -0
new file mode 100644
index 0000000..5fd113a
@@ -0,0 +1,16 @@
+/*
+ * crc16.h - CRC-16 routine
+ * Implements the standard CRC-16:
+ *  Width 16
+ *  Poly  0x8005 (x16 + x15 + x2 + 1)
+ *  Init  0
+ *
+ * Copyright (c) 2005 Ben Gardner <[email protected]>
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+ */
+#ifndef __CRC16_H
+#define __CRC16_H
+extern unsigned int ext2fs_crc16(unsigned int crc,
+	const void *buffer, unsigned int len);
+#endif
fs/ext4/ext4_common.c +1380 -0

1,399 lines not on this page · raw message

fs/ext4/ext4_common.h +20 -0
index a0e04e0..bcf57d2 100644
@@ -45,4 +45,24 @@
 extern unsigned long part_offset;
 int ext4fs_read_inode(struct ext2_data *data, int ino,
 		      struct ext2_inode *inode);
+
+#if defined(CONFIG_CMD_EXT4_WRITE)
+uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n);
+int ext4fs_checksum_update(unsigned int i);
+int get_parent_inode_num(char *dirname, char *dname, int flags);
+void update_parent_dentry(char *filename, int *p_ino, int file_type);
+long int get_new_blk_no(void);
+int get_new_inode_no(void);
+void reset_block_bmap(long int blockno, unsigned char *buffer, int index);
+int set_block_bmap(long int blockno, unsigned char *buffer, int index);
+int set_inode_bmap(int inode_no, unsigned char *buffer, int index);
+void reset_inode_bmap(int inode_no, unsigned char *buffer, int index);
+int iget(int inode_no, struct ext2_inode *inode);
+void allocate_blocks(struct ext2_inode *file_inode,
+		     unsigned int total_remaining_blocks,
+		     unsigned int *total_no_of_block);
+void put_ext4(uint64_t off, void *buf, uint32_t size);
+unsigned int be_le(unsigned int num);
+unsigned int le_be(unsigned int num);
+#endif
 #endif
fs/ext4/ext4_journal.c new file +662 -0

662 lines not on this page · raw message

fs/ext4/ext4_journal.h new file +147 -0

147 lines not on this page · raw message

fs/ext4/ext4fs.c +972 -0

481 lines not on this page · raw message

include/ext4fs.h +12 -0
index e1eb17f..151b7b1 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.0.4

Part of the diff is not shown here. View the raw message.

#4 lore raw

PATCH v3 2/2 ext4fs write support

Dear uma.shankar at samsung.com,

Quoted 27 lines · In message <[email protected]> you…

From: Uma Shankar <[email protected]>

---
Changes for v2:
- Code cleanup, changed comment style
- camel case removed, resolved code alignment issues
- memory allocation logic changed, removed busybox logic
- Modified ext4 load to remove grub dependency (GPLv3)
- Introduced new Config for ext4 write

Changes for v1:
- Removed checkpatch warnings and errors
- Moved common API's of ext2 and ext4 to one generic header file

 common/cmd_ext4.c      |  141 +++++
 fs/ext4/Makefile       |    1 +
 fs/ext4/README         |   46 ++
 fs/ext4/crc16.c        |   62 +++
 fs/ext4/crc16.h        |   16 +
 fs/ext4/ext4_common.c  | 1380 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/ext4_common.h  |   20 +
 fs/ext4/ext4_journal.c |  662 +++++++++++++++++++++++
 fs/ext4/ext4_journal.h |  147 +++++
Quoted 8 lines
 fs/ext4/ext4fs.c       |  972 ++++++++++++++++++++++++++++++++++
 include/ext4fs.h       |   12 +
 11 files changed, 3459 insertions(+), 0 deletions(-)
 create mode 100644 fs/ext4/README
 create mode 100644 fs/ext4/crc16.c
 create mode 100644 fs/ext4/crc16.h
 create mode 100644 fs/ext4/ext4_journal.c
 create mode 100644 fs/ext4/ext4_journal.h

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Youth doesn't excuse everything.
	-- Dr. Janice Lester (in Kirk's body), "Turnabout Intruder",
	   stardate 5928.5.
#5 lore raw

PATCH v3 2/2 ext4fs write support

In reply to uma.shankar at samsung.com
Quoted 6 lines · On Tuesday 27 December 2011 21:23:27 uma.shankar at samsung.com wrote:

From: Uma Shankar <[email protected]>

you need to note the sources of these files in the changelog

common/cmd_ext4.c static int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) · line 286

Quoted 4 lines
common/cmd_ext4.c +1 -0
 
+int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])

static

common/cmd_ext4.c static int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) · line 288

Quoted 1 line
common/cmd_ext4.c +1 -0
+	char *filename = "/";

const

common/cmd_ext4.c static int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) · line 291

Quoted 1 line
common/cmd_ext4.c +1 -0
+	int dev = 0;

considering you set dev first below, no point in setting this to 0

common/cmd_ext4.c static int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) · line 319

Quoted 1 line
common/cmd_ext4.c +1 -0
+	/*get the filename */

many of the comments here are missing a space after the "/*"

fs/ext4/README

Quoted 2 lines
fs/ext4/README new file +0 -0

docs belong in docs/

fs/ext4/crc16.c line 6

Quoted 9 lines
fs/ext4/crc16.c new file +6 -0
 
+/*
+ *      crc16.c
+ *
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+ */

why can't you use the existing lib/crc16.c code ?

fs/ext4/ext4_common.c line 42

Quoted 9 lines
fs/ext4/ext4_common.c +5 -0
 
+#if defined(CONFIG_CMD_EXT4_WRITE)
+#include "ext4_journal.h"
+#include "crc16.h"
+#else
 #include "ext4_common.h"
+#endif

conditional header includes are a bad idea

fs/ext4/ext4_common.c int ext4fs_indir3_size; · line 81

Quoted 5 lines
fs/ext4/ext4_common.c +4 -0
+/* To convert big endian journal superblock entries to little endian */
+unsigned int be_le(unsigned int num)
...
+/* 4-byte number */
+unsigned int le_be(unsigned int num)

we already have swap functions in include/ you should be able to use instead of duplicating them

fs/ext4/ext4_common.c int ext4fs_indir3_size; · line 332

Quoted 5 lines
fs/ext4/ext4_common.c +4 -0
+void reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
...
+
+	return;
+}

useless return -> delete it

Quoted 21 lines
fs/ext4/ext4_common.h +13 -0
 
+int get_parent_inode_num(char *dirname, char *dname, int flags);
+void update_parent_dentry(char *filename, int *p_ino, int file_type);
+long int get_new_blk_no(void);
+int get_new_inode_no(void);
+void reset_block_bmap(long int blockno, unsigned char *buffer, int index);
+int set_block_bmap(long int blockno, unsigned char *buffer, int index);
+int set_inode_bmap(int inode_no, unsigned char *buffer, int index);
+void reset_inode_bmap(int inode_no, unsigned char *buffer, int index);
+int iget(int inode_no, struct ext2_inode *inode);
+void allocate_blocks(struct ext2_inode *file_inode,
+		     unsigned int total_remaining_blocks,
+		     unsigned int *total_no_of_block);
+void put_ext4(uint64_t off, void *buf, uint32_t size);
fs/ext4/ext4_journal.h new file +1 -0
 
+int init_journal(void);

fs/ext4/ext4_journal.h line 146

Quoted 10 lines
fs/ext4/ext4_journal.h new file +10 -0
+void deinit_journal(void);
+int log_gdt(char *gd_table);
+void update_journal(void);
+int check_journal_state(int recovery_flag);
+int log_journal(char *journal_buffer, long int blknr);
+int put_metadata(char *metadata_buffer, long int blknr);
+void dump_metadata(void);
+void free_journal(void);
+void free_revoke_blks(void);
+void push_revoke_blk(char *buffer);

these are a lot of bad names. these need to be properly namespaced if you're going to export them.

fs/ext4/ext4_journal.c line 85

Quoted 12 lines
fs/ext4/ext4_journal.c new file +6 -0
 
+		journal_ptr[i] = (struct journal_log *)zalloc
+		    (sizeof(struct journal_log));
...
+		dirty_block_ptr[i] = (struct dirty_blocks *)
+		    zalloc(sizeof(struct dirty_blocks));
...
+		journal_ptr[gindex]->buf = (char *)zalloc(fs->blksz);
...
+		journal_ptr[gindex]->buf = (char *)zalloc(fs->blksz);

useless casts. zalloc() returns void *. there's a bunch more throughout this patch.

fs/ext4/ext4_journal.c line 143

Quoted 6 lines
fs/ext4/ext4_journal.c new file +5 -0
+void free_journal(void)
+{
...
+
+	return;
+}

useless return

fs/ext4/ext4_journal.c line 234

Quoted 6 lines
fs/ext4/ext4_journal.c new file +5 -0
+void print_revoke_blks(char *revk_blk)
+{
...
+
+	return;
+}

useless return

fs/ext4/ext4_journal.c line 258

Quoted 6 lines
fs/ext4/ext4_journal.c new file +5 -0
+static struct revoke_blk_list *_get_node(void)
+{
...
+
+	return;
+}

useless return

Quoted 1 line

+void free_revoke_blks()

needs (void)

fs/ext4/ext4_journal.c

Quoted 2 lines
fs/ext4/ext4_journal.c new file +2 -0
+	return;
+}

useless return

fs/ext4/ext4_journal.c line 409

Quoted 6 lines
fs/ext4/ext4_journal.c new file +5 -0
+void print_jrnl_status(int recovery_flag)
+{
...
+
+	return;
+}

useless return

fs/ext4/ext4fs.c int ext4fs_read(char *buf, unsigned len) · line 351

Quoted 10 lines
fs/ext4/ext4fs.c +6 -0
 
+static void delete_single_indirect_block(struct ext2_inode *inode)
+{
...
+
+	return;
+
+}

useless return

fs/ext4/ext4fs.c int ext4fs_read(char *buf, unsigned len) · line 379

Quoted 6 lines
fs/ext4/ext4fs.c +5 -0
+static void delete_double_indirect_block(struct ext2_inode *inode)
+{
...
+
+	return;
+}

useless return

fs/ext4/ext4fs.c int ext4fs_read(char *buf, unsigned len) · line 482

Quoted 6 lines
fs/ext4/ext4fs.c +5 -0
+static void delete_triple_indirect_block(struct ext2_inode *inode)
+{
...
+
+	return;
+}

useless return

fs/ext4/ext4fs.c int ext4fs_read(char *buf, unsigned len) · line 956

Quoted 6 lines
fs/ext4/ext4fs.c +5 -0
+void ext4fs_deinit(void)
+{
...
+
+	return;
+}

useless return
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120107/11d75ffc/attachment.pgp>

#6 lore raw

PATCH v4 2/2 ext4fs write support

In reply to uma.shankar at samsung.com

From: uma.shankar <[email protected]>

---
Changes for v3:
- Copyright has been updated in respective files
- ext4fs has been made independant of ext2fs.c
- Fixed API namespace
- Removed endianness conversion API, used uboot defined API for the same
- Fixed coding style issues
- Moved README.ext4 file into doc folder

Changes for v2:
- Code cleanup, changed comment style
- camel case removed, resolved code alignment issues
- memory allocation logic changed, removed busybox logic
- Modified ext4 load to remove grub dependency (GPLv3)
- Introduced new Config for ext4 write

Changes for v1:
- Removed checkpatch warnings and errors
- Moved common API's of ext2 and ext4 to one generic header file

 common/Makefile        |    3 +
 common/cmd_ext4.c      |  143 +++++-
 doc/README.ext4        |   43 ++
 fs/ext4/Makefile       |    4 +
 fs/ext4/crc16.c        |   62 +++
 fs/ext4/crc16.h        |   16 +
 fs/ext4/ext4_common.c  | 1346 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/ext4_common.h  |   25 +
 fs/ext4/ext4_journal.c |  666 ++++++++++++++++++++++++
 fs/ext4/ext4_journal.h |  141 +++++
 fs/ext4/ext4fs.c       |  960 ++++++++++++++++++++++++++++++++++
 include/ext4fs.h       |   12 +
 12 files changed, 3420 insertions(+), 1 deletions(-)
 create mode 100644 doc/README.ext4
 create mode 100644 fs/ext4/crc16.c
 create mode 100644 fs/ext4/crc16.h
 create mode 100644 fs/ext4/ext4_journal.c
 create mode 100644 fs/ext4/ext4_journal.h
common/Makefile +3 -0
index f5243f6..a088310 100644
@@ -87,6 +87,9 @@ COBJS-$(CONFIG_CMD_EEPROM) += cmd_eeprom.o
 COBJS-$(CONFIG_CMD_ELF) += cmd_elf.o
 COBJS-$(CONFIG_SYS_HUSH_PARSER) += cmd_exit.o
 COBJS-$(CONFIG_CMD_EXT4) += cmd_ext4.o
+ifndef CONFIG_CMD_EXT4
+COBJS-$(CONFIG_CMD_EXT4_WRITE) += cmd_ext4.o
+endif
 COBJS-$(CONFIG_CMD_EXT2) += cmd_ext2.o
 COBJS-$(CONFIG_CMD_FAT) += cmd_fat.o
 COBJS-$(CONFIG_CMD_FDC)$(CONFIG_CMD_FDOS) += cmd_fdc.o
common/cmd_ext4.c +142 -1

161 lines not on this page · raw message

doc/README.ext4 new file +43 -0

43 lines not on this page · raw message

fs/ext4/Makefile +4 -0
index 7add4ab..5b86dab 100644
@@ -31,6 +31,10 @@ LIB	= $(obj)libext4fs.o
 
 AOBJS	=
 COBJS-$(CONFIG_CMD_EXT4) := ext4fs.o ext4_common.o dev.o
+ifndef CONFIG_CMD_EXT4
+COBJS-$(CONFIG_CMD_EXT4_WRITE) := ext4fs.o ext4_common.o dev.o
+endif
+COBJS-$(CONFIG_CMD_EXT4_WRITE) += ext4_journal.o crc16.o
 
 SRCS	:= $(AOBJS:.o=.S) $(COBJS-y:.o=.c)
 OBJS	:= $(addprefix $(obj),$(AOBJS) $(COBJS-y))
fs/ext4/crc16.c new file +62 -0

62 lines not on this page · raw message

fs/ext4/crc16.h new file +16 -0
new file mode 100644
index 0000000..688b871
@@ -0,0 +1,16 @@
+/*
+ * crc16.h - CRC-16 routine
+ * Implements the standard CRC-16:
+ *  Width 16
+ *  Poly  0x8005 (x16 + x15 + x2 + 1)
+ *  Init  0
+ *
+ * Copyright (c) 2005 Ben Gardner <[email protected]>
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+ */
+#ifndef __CRC16_H
+#define __CRC16_H
+extern unsigned int ext2fs_crc16(unsigned int crc,
+				 const void *buffer, unsigned int len);
+#endif
fs/ext4/ext4_common.c +1346 -0

1,358 lines not on this page · raw message

fs/ext4/ext4_common.h +25 -0
index 182049d..0424798 100644
@@ -14,6 +14,8 @@
  * GRUB  --  GRand Unified Bootloader
  * Copyright (C) 2003, 2004  Free Software Foundation, Inc.
  *
+ * ext4write : Based on generic ext4 protocol.
+ *
  * 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
@@ -35,6 +37,10 @@
 #include <ext4fs.h>
 #include <malloc.h>
 #include <asm/errno.h>
+#if defined(CONFIG_CMD_EXT4_WRITE)
+#include "ext4_journal.h"
+#include "crc16.h"
+#endif
 
 #define YES		1
 #define NO		0
@@ -60,4 +66,23 @@ int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
 			struct ext2fs_node **foundnode, int expecttype);
 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
 			struct ext2fs_node **fnode, int *ftype);
+
+#if defined(CONFIG_CMD_EXT4_WRITE)
+uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n);
+int ext4fs_checksum_update(unsigned int i);
+int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags);
+void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type);
+long int ext4fs_get_new_blk_no(void);
+int ext4fs_get_new_inode_no(void);
+void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer,
+			     int index);
+int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index);
+int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index);
+void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index);
+int ext4fs_iget(int inode_no, struct ext2_inode *inode);
+void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
+			    unsigned int total_remaining_blocks,
+			    unsigned int *total_no_of_block);
+void put_ext4(uint64_t off, void *buf, uint32_t size);
+#endif
 #endif
fs/ext4/ext4_journal.c new file +666 -0

666 lines not on this page · raw message

fs/ext4/ext4_journal.h new file +141 -0

141 lines not on this page · raw message

fs/ext4/ext4fs.c +960 -0

493 lines not on this page · raw message

include/ext4fs.h +12 -0
index 55f9ccc..a56e302 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.0.4

Part of the diff is not shown here. View the raw message.

#7 lore raw

PATCH v4 2/2 ext4fs write support

common/Makefile COBJS-$(CONFIG_CMD_EEPROM) += cmd_eeprom.o · line 92

Quoted 6 lines · On Monday 09 January 2012 12:56:16 uma.shankar at samsung.com wrote:
common/Makefile +3 -0
 
+ifndef CONFIG_CMD_EXT4
+COBJS-$(CONFIG_CMD_EXT4_WRITE) += cmd_ext4.o
+endif

the CONFIG_CMD_EXT4 check is not necessary. if someone tries to enable CONFIG_CMD_EXT4_WRITE but not CONFIG_CMD_EXT4, that's their fault.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120115/b04fd1e9/attachment.pgp>

#8 lore raw

PATCH v4 2/2 ext4fs write support

Dear Mike Frysinger,

Quoted 7 lines · In message <[email protected]> you wrote:

common/Makefile COBJS-$(CONFIG_CMD_EEPROM) += cmd_eeprom.o · line 92

Quoted 4 lines
common/Makefile +3 -0
+ifndef CONFIG_CMD_EXT4
+COBJS-$(CONFIG_CMD_EXT4_WRITE) += cmd_ext4.o
+endif

the CONFIG_CMD_EXT4 check is not necessary. if someone tries to enable CONFIG_CMD_EXT4_WRITE but not CONFIG_CMD_EXT4, that's their fault.

we can try to be a bit user-friendly at least and throw a compile time error message.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"Virtual" means never knowing where your next byte is coming from.
#9 lore raw

PATCH v4 2/2 ext4fs write support

Quoted 10 lines · On Monday 16 January 2012 02:58:20 Wolfgang Denk wrote:
Quoted 6 lines · Mike Frysinger

common/Makefile COBJS-$(CONFIG_CMD_EEPROM) += cmd_eeprom.o · line 92

Quoted 3 lines
common/Makefile +3 -0
+ifndef CONFIG_CMD_EXT4
+COBJS-$(CONFIG_CMD_EXT4_WRITE) += cmd_ext4.o
+endif

the CONFIG_CMD_EXT4 check is not necessary. if someone tries to enable CONFIG_CMD_EXT4_WRITE but not CONFIG_CMD_EXT4, that's their fault.

we can try to be a bit user-friendly at least and throw a compile time error message.

i'm fine with that. i just don't think this checking belongs in the Makefiles. -mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120116/5c48c87c/attachment.pgp>

#10 lore raw

PATCH v5 2/2 ext4fs write support

In reply to uma.shankar at samsung.com

---
Changes for v4:
- Redesigned ext2, ext4 command interface
- Removed ext2 folder from fs/
- Memory Leak issue handled

Changes for v3:
- Copyright has been updated in respective files
- ext4fs has been made independant of ext2fs.c
- Fixed API namespace
- Removed endianness conversion API, used uboot defined API for the same
- Fixed coding style issues
- Moved README.ext4 file into doc folder

Changes for v2:
- Code cleanup, changed comment style
- camel case removed, resolved code alignment issues
- memory allocation logic changed, removed busybox logic
- Modified ext4 load to remove grub dependency (GPLv3)
- Introduced new Config for ext4 write

Changes for v1:
- Removed checkpatch warnings and errors
- Moved common API's of ext2 and ext4 to one generic header file

---
 common/cmd_ext4.c      |  141 +++++
 doc/README.ext4        |   46 ++
 fs/ext4/Makefile       |    1 +
 fs/ext4/crc16.c        |   62 +++
 fs/ext4/crc16.h        |   16 +
 fs/ext4/ext4_common.c  | 1353 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/ext4_common.h  |   25 +
 fs/ext4/ext4_journal.c |  667 ++++++++++++++++++++++++
 fs/ext4/ext4_journal.h |  141 +++++
 fs/ext4/ext4fs.c       |  961 ++++++++++++++++++++++++++++++++++
 include/ext4fs.h       |   12 +
 include/ext_common.h   |    2 +
 12 files changed, 3427 insertions(+), 0 deletions(-)
 create mode 100644 doc/README.ext4
 create mode 100644 fs/ext4/crc16.c
 create mode 100644 fs/ext4/crc16.h
 create mode 100644 fs/ext4/ext4_journal.c
 create mode 100644 fs/ext4/ext4_journal.h
common/cmd_ext4.c +141 -0
index 3298fee..77094c4 100644
@@ -62,6 +62,10 @@
 
 uint64_t total_sector;
 uint64_t part_offset;
+#if defined(CONFIG_CMD_EXT4_WRITE)
+static uint64_t part_size;
+static uint16_t cur_part = 1;
+#endif
 
 #define DOS_PART_MAGIC_OFFSET		0x1fe
 #define DOS_FS_TYPE_OFFSET		0x36
@@ -84,6 +88,143 @@ int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 	return 0;
 }
 
+#if defined(CONFIG_CMD_EXT4_WRITE)
+static int ext4_register_device(block_dev_desc_t *dev_desc, int part_no)
+{
+	unsigned char buffer[SECTOR_SIZE];
+	disk_partition_t info;
+
+	if (!dev_desc->block_read)
+		return -1;
+
+	/* check if we have a MBR (on floppies we have only a PBR) */
+	if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
+		printf("** Can't read from device %d **\n", dev_desc->dev);
+		return -1;
+	}
+	if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
+	    buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
+		/* no signature found */
+		return -1;
+	}
+
+	/* First we assume there is a MBR */
+	if (!get_partition_info(dev_desc, part_no, &info)) {
+		part_offset = info.start;
+		cur_part = part_no;
+		part_size = info.size;
+	} else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],
+			    "FAT", 3) == 0) || (strncmp((char *)&buffer
+							[DOS_FS32_TYPE_OFFSET],
+							"FAT32", 5) == 0)) {
+		/* ok, we assume we are on a PBR only */
+		cur_part = 1;
+		part_offset = 0;
+	} else {
+		printf("** Partition %d not valid on device %d **\n",
+		       part_no, dev_desc->dev);
+		return -1;
+	}
+
+	return 0;
+}
+
+int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
+				char *const argv[])
+{
+	const char *filename = "/";
+	int part_length;
+	unsigned long part = 1;
+	int dev;
+	char *ep;
+	unsigned long ram_address;
+	unsigned long file_size;
+	disk_partition_t info;
+	struct ext_filesystem *fs;
+
+	if (argc < 6)
+		return cmd_usage(cmdtp);
+
+	dev = (int)simple_strtoul(argv[2], &ep, 16);
+	ext4_dev_desc = get_dev(argv[1], dev);
+	if (ext4_dev_desc == NULL) {
+		printf("Block device %s %d not supported\n", argv[1], dev);
+		return 1;
+	}
+	if (init_fs(ext4_dev_desc))
+		return 1;
+
+	fs = get_fs();
+	if (*ep) {
+		if (*ep != ':') {
+			puts("Invalid boot device, use `dev[:part]'\n");
+			goto fail;
+		}
+		part = simple_strtoul(++ep, NULL, 16);
+	}
+
+	/* get the filename */
+	filename = argv[3];
+
+	/* get the address in hexadecimal format (string to int) */
+	ram_address = simple_strtoul(argv[4], NULL, 16);
+
+	/* get the filesize in base 10 format */
+	file_size = simple_strtoul(argv[5], NULL, 10);
+
+	/* set the device as block device */
+	part_length = ext4fs_set_blk_dev(fs->dev_desc, part);
+	if (part_length == 0) {
+		printf("Bad partition - %s %d:%lu\n", argv[1], dev, part);
+		goto fail;
+	}
+
+	/* register the device and partition */
+	if (ext4_register_device(fs->dev_desc, part) != 0) {
+		printf("Unable to use %s %d:%lu for fattable\n",
+		       argv[1], dev, part);
+		goto fail;
+	}
+
+	/* get the partition information */
+	if (!get_partition_info(fs->dev_desc, part, &info)) {
+		total_sector = (info.size * info.blksz) / SECTOR_SIZE;
+		fs->total_sect = total_sector;
+	} else {
+		printf("error : get partition info\n");
+		goto fail;
+	}
+
+	/* mount the filesystem */
+	if (!ext4fs_mount(part_length)) {
+		printf("Bad ext4 partition %s %d:%lu\n", argv[1], dev, part);
+		goto fail;
+	}
+
+	/* start write */
+	if (ext4fs_write(filename, (unsigned char *)ram_address, file_size)) {
+		printf("** Error ext4fs_write() **\n");
+		goto fail;
+	}
+	ext4fs_close();
+	deinit_fs(fs->dev_desc);
+
+	return 0;
+
+fail:
+	ext4fs_close();
+	deinit_fs(fs->dev_desc);
+
+	return 1;
+}
+
+U_BOOT_CMD(ext4write, 6, 1, do_ext4_write,
+	"create a file in the root directory",
+	"<interface> <dev[:part]> [Absolute filename path] [Address] [sizebytes]\n"
+	"	  - create a file in / directory");
+
+#endif
+
 U_BOOT_CMD(ext4ls, 4, 1, do_ext4_ls,
 	   "list files in a directory (default /)",
 	   "<interface> <dev[:part]> [directory]\n"
doc/README.ext4 new file +46 -0

46 lines not on this page · raw message

fs/ext4/Makefile +1 -0
index 7b7fcbd..82cd9ae 100644
@@ -34,6 +34,7 @@ COBJS-$(CONFIG_CMD_EXT4) := ext4fs.o ext4_common.o dev.o
 ifndef CONFIG_CMD_EXT4
 COBJS-$(CONFIG_CMD_EXT2) := ext4fs.o ext4_common.o dev.o
 endif
+COBJS-$(CONFIG_CMD_EXT4_WRITE) += ext4_journal.o crc16.o
 
 SRCS	:= $(AOBJS:.o=.S) $(COBJS-y:.o=.c)
 OBJS	:= $(addprefix $(obj),$(AOBJS) $(COBJS-y))
fs/ext4/crc16.c new file +62 -0

62 lines not on this page · raw message

fs/ext4/crc16.h new file +16 -0
new file mode 100644
index 0000000..5fd113a
@@ -0,0 +1,16 @@
+/*
+ * crc16.h - CRC-16 routine
+ * Implements the standard CRC-16:
+ *  Width 16
+ *  Poly  0x8005 (x16 + x15 + x2 + 1)
+ *  Init  0
+ *
+ * Copyright (c) 2005 Ben Gardner <[email protected]>
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+ */
+#ifndef __CRC16_H
+#define __CRC16_H
+extern unsigned int ext2fs_crc16(unsigned int crc,
+	const void *buffer, unsigned int len);
+#endif
fs/ext4/ext4_common.c +1353 -0

1,365 lines not on this page · raw message

fs/ext4/ext4_common.h +25 -0
index 182049d..bcfcd44 100644
@@ -14,6 +14,8 @@
  * GRUB  --  GRand Unified Bootloader
  * Copyright (C) 2003, 2004  Free Software Foundation, Inc.
  *
+ * ext4write : Based on generic ext4 protocol.
+ *
  * 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
@@ -35,6 +37,10 @@
 #include <ext4fs.h>
 #include <malloc.h>
 #include <asm/errno.h>
+#if defined(CONFIG_CMD_EXT4_WRITE)
+#include "ext4_journal.h"
+#include "crc16.h"
+#endif
 
 #define YES		1
 #define NO		0
@@ -60,4 +66,23 @@ int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
 			struct ext2fs_node **foundnode, int expecttype);
 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
 			struct ext2fs_node **fnode, int *ftype);
+
+#if defined(CONFIG_CMD_EXT4_WRITE)
+uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n);
+int ext4fs_checksum_update(unsigned int i);
+int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags);
+void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type);
+long int ext4fs_get_new_blk_no(void);
+int ext4fs_get_new_inode_no(void);
+void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer,
+					int index);
+int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index);
+int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index);
+void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index);
+int ext4fs_iget(int inode_no, struct ext2_inode *inode);
+void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
+				unsigned int total_remaining_blocks,
+				unsigned int *total_no_of_block);
+void put_ext4(uint64_t off, void *buf, uint32_t size);
+#endif
 #endif
fs/ext4/ext4_journal.c new file +667 -0

667 lines not on this page · raw message

fs/ext4/ext4_journal.h new file +141 -0

141 lines not on this page · raw message

fs/ext4/ext4fs.c +961 -0

502 lines not on this page · raw message

include/ext4fs.h +12 -0
index 55f9ccc..a56e302 100644

Lines of this file are not shown. View the raw message.

include/ext_common.h +2 -0
index ef71e1d..b671fc3 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.0.4

Part of the diff is not shown here. View the raw message.

#11 lore raw

PATCH v5 2/2 ext4fs write support

Dear Uma Shankar,

Quoted 43 lines · In message <[email protected]> you…

---
Changes for v4:
- Redesigned ext2, ext4 command interface
- Removed ext2 folder from fs/
- Memory Leak issue handled

Changes for v3:
- Copyright has been updated in respective files
- ext4fs has been made independant of ext2fs.c
- Fixed API namespace
- Removed endianness conversion API, used uboot defined API for the same
- Fixed coding style issues
- Moved README.ext4 file into doc folder

Changes for v2:
- Code cleanup, changed comment style
- camel case removed, resolved code alignment issues
- memory allocation logic changed, removed busybox logic
- Modified ext4 load to remove grub dependency (GPLv3)
- Introduced new Config for ext4 write

Changes for v1:
- Removed checkpatch warnings and errors
- Moved common API's of ext2 and ext4 to one generic header file

---
 common/cmd_ext4.c      |  141 +++++
 doc/README.ext4        |   46 ++
 fs/ext4/Makefile       |    1 +
 fs/ext4/crc16.c        |   62 +++
 fs/ext4/crc16.h        |   16 +
 fs/ext4/ext4_common.c  | 1353 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/ext4_common.h  |   25 +
 fs/ext4/ext4_journal.c |  667 ++++++++++++++++++++++++
 fs/ext4/ext4_journal.h |  141 +++++
 fs/ext4/ext4fs.c       |  961 ++++++++++++++++++++++++++++++++++
Quoted 8 lines
 include/ext4fs.h       |   12 +
 include/ext_common.h   |    2 +
 12 files changed, 3427 insertions(+), 0 deletions(-)
 create mode 100644 doc/README.ext4
 create mode 100644 fs/ext4/crc16.c
 create mode 100644 fs/ext4/crc16.h
 create mode 100644 fs/ext4/ext4_journal.c
 create mode 100644 fs/ext4/ext4_journal.h

Applied to "ext4" branch, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The human race has one really effective weapon, and that is laughter.
                                                         - Mark Twain
#12 lore raw

PATCH v5 2/2 ext4fs write support

Dear Uma Shankar,

Quoted 2 lines · In message <[email protected]> I wrote:

Applied to "ext4" branch, thanks.

I did some performance tests on a MPC5200 based board using the IDE interface (no DMA):

=> vers

U-Boot 2012.07-00125-ged34f34 (Aug 10 2012 - 09:36:33) powerpc-linux-gcc (GCC) 4.6.4 20120303 (prerelease) GNU ld (GNU Binutils) 2.22
=> date ; ext2load ide 0:4 100000 usr-bin.tar 2000000 ; date
Date: 2012-08-10 (Saturday) Time: 16:27:21
Loading file "usr-bin.tar" from ide device 0:4 hda4 33554432 bytes read
Date: 2012-08-10 (Saturday) Time: 16:27:48

=================================================

27 seconds => 1.2 MiB/s

=> vers

U-Boot 2012.07 (Aug 02 2012 - 10:31:44) powerpc-linux-gcc (GCC) 4.6.4 20120303 (prerelease) GNU ld (GNU Binutils) 2.22
=> date ; ext2load ide 0:4 100000 usr-bin.tar 2000000 ; date
Date: 2012-08-10 (Saturday) Time: 16:49:31
Loading file "usr-bin.tar" from ide device 0:4 (hda4) 33554432 bytes read
Date: 2012-08-10 (Saturday) Time: 16:49:57

=================================================

26 seconds => 1.2 MiB/s

So within the accuracy of this measurement,the performance of the new code compared to the old ext2 code appears to be pretty much the same.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Keep your eyes wide open before marriage, half shut afterwards.
                                                 -- Benjamin Franklin
#13 lore raw

PATCH v5 2/2 ext4fs write support

Dear Wolfgang,
We have tested this feature on Samsung Boards using mmc interface. Here is a sample test run with exactly the same file size which you have used for testing

[Mon Aug 13 19:05:51.824 2012]  # ext4load mmc 0:6 0x40007fc0 abc.tar
[Mon Aug 13 19:05:51.846 2012] Loading file "abc.tar" from mmc device 0:6 
(dt)
[Mon Aug 13 19:05:53.087 2012] 33554432 bytes read
[Mon Aug 13 19:05:53.091 2012] #

Time taken 1.267s
=> 25.25 MiB/s

[Mon Aug 13 19:07:35.323 2012] # ext4load mmc 0:6 0x40007fc0 abc.tar
[Mon Aug 13 19:07:35.351 2012] Loading file "abc.tar" from mmc device 0:6 
(dt)
[Mon Aug 13 19:07:36.591 2012] 33554432 bytes read
[Mon Aug 13 19:07:36.595 2012] #

Time taken 1.272s
=> 25.15 MiB/s

I tried to look into ide interface implementation. It seems in case of ide (cmd_ide.c), "ide_read" function tries to read block by block. Whereas in case of mmc interface, it has bulk read implementation where in multiple continuous blocks can be read in one shot.

Previous implementation of ext2 relies on block by block read, where as in current implementation multiple block read has been used. Thus, we are getting slow performance for ide in both the cases.

Note: I am using teraterm timestamp instead of date command to measure time.

Thanks & Regards,
Uma Shankar
--------------------------------------------------
From: "Wolfgang Denk" <[email protected]>
Sent: Monday, August 13, 2012 5:25 PM
To: "Uma Shankar" <[email protected]>
Cc: "Manjunatha C Achar" <[email protected]>; <[email protected]>; <[email protected]>; "Hakgoo Lee" <[email protected]>; "Iqbal Shareef" <[email protected]>
Subject: Re: [U-Boot] [PATCH V5 2/2] ext4fs write support

Quoted 55 lines

Dear Uma Shankar,

Quoted 2 lines · In message <[email protected]> I wrote:

Applied to "ext4" branch, thanks.

I did some performance tests on a MPC5200 based board using the IDE interface (no DMA):

=> vers

U-Boot 2012.07-00125-ged34f34 (Aug 10 2012 - 09:36:33) powerpc-linux-gcc (GCC) 4.6.4 20120303 (prerelease) GNU ld (GNU Binutils) 2.22
=> date ; ext2load ide 0:4 100000 usr-bin.tar 2000000 ; date
Date: 2012-08-10 (Saturday) Time: 16:27:21
Loading file "usr-bin.tar" from ide device 0:4 hda4 33554432 bytes read
Date: 2012-08-10 (Saturday) Time: 16:27:48

=================================================

27 seconds => 1.2 MiB/s

=> vers

U-Boot 2012.07 (Aug 02 2012 - 10:31:44) powerpc-linux-gcc (GCC) 4.6.4 20120303 (prerelease) GNU ld (GNU Binutils) 2.22
=> date ; ext2load ide 0:4 100000 usr-bin.tar 2000000 ; date
Date: 2012-08-10 (Saturday) Time: 16:49:31
Loading file "usr-bin.tar" from ide device 0:4 (hda4) 33554432 bytes read
Date: 2012-08-10 (Saturday) Time: 16:49:57

=================================================

26 seconds => 1.2 MiB/s

So within the accuracy of this measurement,the performance of the new code compared to the old ext2 code appears to be pretty much the same.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Keep your eyes wide open before marriage, half shut afterwards.
                                                -- Benjamin Franklin