mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] image: use data base addr for image_print_contents, image_multi_count/getimg
@ 2011-09-27 14:22 Jean-Christophe PLAGNIOL-VILLARD
  2011-09-27 14:22 ` [PATCH 2/3] image: add multi image support for bootm Jean-Christophe PLAGNIOL-VILLARD
  2011-09-27 14:22 ` [PATCH 3/3] image: move map_image amd unmap_image to image.c Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 2 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-09-27 14:22 UTC (permalink / raw)
  To: barebox

as in barebox the data could be the mapped file or a allocated memory with
just the data (non header)

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/bootm.c  |    2 +-
 common/image.c    |   31 +++++++++++++++----------------
 include/image.h   |    8 ++++----
 scripts/mkimage.c |    5 +++--
 4 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/commands/bootm.c b/commands/bootm.c
index e5ffacb..e9a39c4 100644
--- a/commands/bootm.c
+++ b/commands/bootm.c
@@ -203,7 +203,7 @@ struct image_handle *map_image(const char *filename, int verify)
 		puts ("OK\n");
 	}
 
-	image_print_contents(header);
+	image_print_contents(header, handle->data);
 
 	close(fd);
 
diff --git a/common/image.c b/common/image.c
index a4c8b95..501218b 100644
--- a/common/image.c
+++ b/common/image.c
@@ -154,14 +154,14 @@ const char *image_get_comp_name(uint8_t comp)
  * returns:
  *     number of components
  */
-ulong image_multi_count(const image_header_t *hdr)
+ulong image_multi_count(void *data)
 {
 	ulong i, count = 0;
 	uint32_t *size;
 
 	/* get start of the image payload, which in case of multi
 	 * component images that points to a table of component sizes */
-	size = (uint32_t *)image_get_data (hdr);
+	size = (uint32_t *)data;
 
 	/* count non empty slots */
 	for (i = 0; size[i]; ++i)
@@ -187,23 +187,23 @@ ulong image_multi_count(const image_header_t *hdr)
  *     data address and size of the component, if idx is valid
  *     0 in data and len, if idx is out of range
  */
-void image_multi_getimg(const image_header_t *hdr, ulong idx,
-			ulong *data, ulong *len)
+void image_multi_getimg(void *data, ulong idx,
+			ulong *img_data, ulong *len)
 {
 	int i;
 	uint32_t *size;
-	ulong offset, count, img_data;
+	ulong offset, count, tmp_img_data;
 
 	/* get number of component */
-	count = image_multi_count(hdr);
+	count = image_multi_count(data);
 
 	/* get start of the image payload, which in case of multi
 	 * component images that points to a table of component sizes */
-	size = (uint32_t *)image_get_data(hdr);
+	size = (uint32_t *)data;
 
 	/* get address of the proper component data start, which means
 	 * skipping sizes table (add 1 for last, null entry) */
-	img_data = image_get_data(hdr) + (count + 1) * sizeof (uint32_t);
+	tmp_img_data = (ulong)data + (count + 1) * sizeof (uint32_t);
 
 	if (idx < count) {
 		*len = uimage_to_cpu(size[idx]);
@@ -216,10 +216,10 @@ void image_multi_getimg(const image_header_t *hdr, ulong idx,
 		}
 
 		/* calculate idx-th component data address */
-		*data = img_data + offset;
+		*img_data = tmp_img_data + offset;
 	} else {
 		*len = 0;
-		*data = 0;
+		*img_data = 0;
 	}
 }
 
@@ -262,9 +262,8 @@ void image_print_size(uint32_t size)
 #endif
 }
 
-void image_print_contents(const void *ptr)
+void image_print_contents(const image_header_t *hdr, void *data)
 {
-	const image_header_t *hdr = (const image_header_t *)ptr;
 	const char *p;
 	int type;
 
@@ -289,12 +288,12 @@ void image_print_contents(const void *ptr)
 	type = image_get_type(hdr);
 	if (type == IH_TYPE_MULTI || type == IH_TYPE_SCRIPT) {
 		int i;
-		ulong data, len;
-		ulong count = image_multi_count(hdr);
+		ulong img_data, len;
+		ulong count = image_multi_count(data);
 
 		printf ("%sContents:\n", p);
 		for (i = 0; i < count; i++) {
-			image_multi_getimg(hdr, i, &data, &len);
+			image_multi_getimg(data, i, &img_data, &len);
 
 			printf("%s   Image %d: ", p, i);
 			image_print_size(len);
@@ -305,7 +304,7 @@ void image_print_contents(const void *ptr)
 				 * if planning to do something with
 				 * multiple files
 				 */
-				printf("%s    Offset = 0x%08lx\n", p, data);
+				printf("%s    Offset = 0x%08lx\n", p, img_data);
 			}
 		}
 	}
diff --git a/include/image.h b/include/image.h
index 8932947..d913b21 100644
--- a/include/image.h
+++ b/include/image.h
@@ -320,13 +320,13 @@ static inline void image_set_name(image_header_t *hdr, const char *name)
 	strncpy(image_get_name(hdr), name, IH_NMLEN);
 }
 
-ulong image_multi_count(const image_header_t *hdr);
-void image_multi_getimg(const image_header_t *hdr, ulong idx,
-			ulong *data, ulong *len);
+ulong image_multi_count(void *data);
+void image_multi_getimg(void *data, ulong idx,
+			ulong *img_data, ulong *len);
 
 void image_print_size(uint32_t size);
 
-void image_print_contents(const void *ptr);
+void image_print_contents(const image_header_t *hdr, void *data);
 
 /* commamds/bootm.c */
 void	print_image_hdr (image_header_t *hdr);
diff --git a/scripts/mkimage.c b/scripts/mkimage.c
index 972ec05..3beab91 100644
--- a/scripts/mkimage.c
+++ b/scripts/mkimage.c
@@ -255,7 +255,8 @@ NXTARG:		;
 		}
 
 		/* for multi-file images we need the data part, too */
-		image_print_contents((image_header_t *)ptr);
+		image_print_contents((image_header_t *)ptr,
+				     (void*)image_get_data((image_header_t *)ptr));
 
 		(void) munmap((void *)ptr, sbuf.st_size);
 		(void) close (ifd);
@@ -381,7 +382,7 @@ NXTARG:		;
 
 	image_set_hcrc(hdr, checksum);
 
-	image_print_contents(hdr);
+	image_print_contents(hdr, (void*)image_get_data(hdr));
 
 	(void) munmap((void *)ptr, sbuf.st_size);
 
-- 
1.7.6.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/3] image: add multi image support for bootm
  2011-09-27 14:22 [PATCH 1/3] image: use data base addr for image_print_contents, image_multi_count/getimg Jean-Christophe PLAGNIOL-VILLARD
@ 2011-09-27 14:22 ` Jean-Christophe PLAGNIOL-VILLARD
  2011-09-27 14:22 ` [PATCH 3/3] image: move map_image amd unmap_image to image.c Jean-Christophe PLAGNIOL-VILLARD
  1 sibling, 0 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-09-27 14:22 UTC (permalink / raw)
  To: barebox

you can choose the initrd via @<num>

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/bootm.c |   78 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 include/image.h  |    7 +++++
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/commands/bootm.c b/commands/bootm.c
index e9a39c4..5adc3fa 100644
--- a/commands/bootm.c
+++ b/commands/bootm.c
@@ -95,16 +95,28 @@ fixup_silent_linux ()
 }
 #endif /* CONFIG_SILENT_CONSOLE */
 
+struct image_handle_data* image_handle_data_get_by_num(struct image_handle* handle, int num)
+{
+	if (!handle || num < 0 || num >= handle->nb_data_entries)
+		return NULL;
+
+	return &handle->data_entries[num];
+}
+
 int relocate_image(struct image_handle *handle, void *load_address)
 {
 	image_header_t *hdr = &handle->header;
 	unsigned long len  = image_get_size(hdr);
-	unsigned long data = (unsigned long)(handle->data);
+	struct image_handle_data *iha;
+	unsigned long data;
 
 #if defined CONFIG_CMD_BOOTM_ZLIB || defined CONFIG_CMD_BOOTM_BZLIB
 	uint	unc_len = CFG_BOOTM_LEN;
 #endif
 
+	iha = image_handle_data_get_by_num(handle, 0);
+	data = (unsigned long)(iha->data);
+
 	switch (image_get_comp(hdr)) {
 	case IH_COMP_NONE:
 		if(image_get_load(hdr) == data) {
@@ -146,12 +158,24 @@ int relocate_image(struct image_handle *handle, void *load_address)
 }
 EXPORT_SYMBOL(relocate_image);
 
+static struct image_handle_data * gen_image_handle_data(void* data, ulong len)
+{
+	struct image_handle_data *iha;
+
+	iha = xzalloc(sizeof(struct image_handle_data));
+	iha->data = data;
+	iha->len = len;
+
+	return iha;
+}
+
 struct image_handle *map_image(const char *filename, int verify)
 {
 	int fd;
 	uint32_t checksum, len;
 	struct image_handle *handle;
 	image_header_t *header;
+	int type;
 
 	fd = open(filename, O_RDONLY);
 	if (fd < 0) {
@@ -194,6 +218,28 @@ struct image_handle *map_image(const char *filename, int verify)
 						       image_get_header_size());
 	}
 
+	type = image_get_type(header);
+	if (type == IH_TYPE_MULTI) {
+		struct image_handle_data *data_entries;
+		int i;
+		ulong img_data;
+		ulong count = image_multi_count(handle->data);
+
+		data_entries = xzalloc(sizeof(struct image_handle_data) * count);
+
+		for (i = 0; i < count; i++) {
+			image_multi_getimg(handle->data, i, &img_data,
+					   &data_entries[i].len);
+
+			data_entries[i].data = (void*)img_data;
+		}
+		handle->data_entries = data_entries;
+		handle->nb_data_entries = count;
+	} else {
+		handle->data_entries = gen_image_handle_data(handle->data, len);
+		handle->nb_data_entries = 1;
+	}
+
 	if (verify) {
 		puts ("   Verifying Checksum ... ");
 		if (crc32 (0, handle->data, len) != image_get_dcrc(header)) {
@@ -221,6 +267,7 @@ void unmap_image(struct image_handle *handle)
 {
 	if (handle->flags & IH_MALLOC)
 		free(handle->data);
+	free(handle->data_entries);
 	free(handle);
 }
 EXPORT_SYMBOL(unmap_image);
@@ -233,13 +280,40 @@ int register_image_handler(struct image_handler *handler)
 	return 0;
 }
 
+/*
+ * generate a image_handle from a multi_image
+ * this image_handle can be free by unmap_image
+ */
+static struct image_handle *get_fake_image_handle(struct image_data *data, int num)
+{
+	struct image_handle *handle;
+	struct image_handle_data* iha;
+	image_header_t *header;
+
+	iha = image_handle_data_get_by_num(data->os, num);
+
+	handle = xzalloc(sizeof(struct image_handle));
+	header = &handle->header;
+	handle->data_entries = gen_image_handle_data(iha->data, iha->len);
+	handle->data = handle->data_entries[0].data;
+
+	return handle;
+}
+
 static int initrd_handler_parse_options(struct image_data *data, int opt,
 		char *optarg)
 {
 	switch(opt) {
 	case 'r':
 		printf("use initrd %s\n", optarg);
-		data->initrd = map_image(optarg, data->verify);
+		/* check for multi image @<num> */
+		if (optarg[0] == '@') {
+			int num = simple_strtol(optarg + 1, NULL, 0);
+
+			data->initrd = get_fake_image_handle(data, num);
+		} else {
+			data->initrd = map_image(optarg, data->verify);
+		}
 		if (!data->initrd)
 			return -1;
 		return 0;
diff --git a/include/image.h b/include/image.h
index d913b21..d34d432 100644
--- a/include/image.h
+++ b/include/image.h
@@ -188,9 +188,16 @@ typedef struct image_header {
 	uint8_t		ih_name[IH_NMLEN];	/* Image Name		*/
 } image_header_t;
 
+struct image_handle_data {
+	void *data;
+	ulong len;
+};
+
 struct image_handle {
 	image_header_t	header;
 	void *data;
+	struct image_handle_data *data_entries;
+	int nb_data_entries;
 #define IH_MALLOC	1
 	int flags;
 };
-- 
1.7.6.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/3] image: move map_image amd unmap_image to image.c
  2011-09-27 14:22 [PATCH 1/3] image: use data base addr for image_print_contents, image_multi_count/getimg Jean-Christophe PLAGNIOL-VILLARD
  2011-09-27 14:22 ` [PATCH 2/3] image: add multi image support for bootm Jean-Christophe PLAGNIOL-VILLARD
@ 2011-09-27 14:22 ` Jean-Christophe PLAGNIOL-VILLARD
  1 sibling, 0 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-09-27 14:22 UTC (permalink / raw)
  To: barebox

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/bootm.c |  114 --------------------------------------------------
 common/image.c   |  121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/image.h  |    1 +
 3 files changed, 122 insertions(+), 114 deletions(-)

diff --git a/commands/bootm.c b/commands/bootm.c
index 5adc3fa..755932b 100644
--- a/commands/bootm.c
+++ b/commands/bootm.c
@@ -158,120 +158,6 @@ int relocate_image(struct image_handle *handle, void *load_address)
 }
 EXPORT_SYMBOL(relocate_image);
 
-static struct image_handle_data * gen_image_handle_data(void* data, ulong len)
-{
-	struct image_handle_data *iha;
-
-	iha = xzalloc(sizeof(struct image_handle_data));
-	iha->data = data;
-	iha->len = len;
-
-	return iha;
-}
-
-struct image_handle *map_image(const char *filename, int verify)
-{
-	int fd;
-	uint32_t checksum, len;
-	struct image_handle *handle;
-	image_header_t *header;
-	int type;
-
-	fd = open(filename, O_RDONLY);
-	if (fd < 0) {
-		printf("could not open: %s\n", errno_str());
-		return NULL;
-	}
-
-	handle = xzalloc(sizeof(struct image_handle));
-	header = &handle->header;
-
-	if (read(fd, header, image_get_header_size()) < 0) {
-		printf("could not read: %s\n", errno_str());
-		goto err_out;
-	}
-
-	if (image_get_magic(header) != IH_MAGIC) {
-		puts ("Bad Magic Number\n");
-		goto err_out;
-	}
-
-	checksum = image_get_hcrc(header);
-	header->ih_hcrc = 0;
-
-	if (crc32 (0, (uchar *)header, image_get_header_size()) != checksum) {
-		puts ("Bad Header Checksum\n");
-		goto err_out;
-	}
-	len  = image_get_size(header);
-
-	handle->data = memmap(fd, PROT_READ);
-	if (handle->data == (void *)-1) {
-		handle->data = xmalloc(len);
-		handle->flags = IH_MALLOC;
-		if (read(fd, handle->data, len) < 0) {
-			printf("could not read: %s\n", errno_str());
-			goto err_out;
-		}
-	} else {
-		handle->data = (void *)((unsigned long)handle->data +
-						       image_get_header_size());
-	}
-
-	type = image_get_type(header);
-	if (type == IH_TYPE_MULTI) {
-		struct image_handle_data *data_entries;
-		int i;
-		ulong img_data;
-		ulong count = image_multi_count(handle->data);
-
-		data_entries = xzalloc(sizeof(struct image_handle_data) * count);
-
-		for (i = 0; i < count; i++) {
-			image_multi_getimg(handle->data, i, &img_data,
-					   &data_entries[i].len);
-
-			data_entries[i].data = (void*)img_data;
-		}
-		handle->data_entries = data_entries;
-		handle->nb_data_entries = count;
-	} else {
-		handle->data_entries = gen_image_handle_data(handle->data, len);
-		handle->nb_data_entries = 1;
-	}
-
-	if (verify) {
-		puts ("   Verifying Checksum ... ");
-		if (crc32 (0, handle->data, len) != image_get_dcrc(header)) {
-			printf ("Bad Data CRC\n");
-			goto err_out;
-		}
-		puts ("OK\n");
-	}
-
-	image_print_contents(header, handle->data);
-
-	close(fd);
-
-	return handle;
-err_out:
-	close(fd);
-	if (handle->flags & IH_MALLOC)
-		free(handle->data);
-	free(handle);
-	return NULL;
-}
-EXPORT_SYMBOL(map_image);
-
-void unmap_image(struct image_handle *handle)
-{
-	if (handle->flags & IH_MALLOC)
-		free(handle->data);
-	free(handle->data_entries);
-	free(handle);
-}
-EXPORT_SYMBOL(unmap_image);
-
 static LIST_HEAD(handler_list);
 
 int register_image_handler(struct image_handler *handler)
diff --git a/common/image.c b/common/image.c
index 501218b..4a6402d 100644
--- a/common/image.c
+++ b/common/image.c
@@ -27,6 +27,10 @@
 #include <common.h>
 #include <image.h>
 #include <rtc.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <malloc.h>
+#include <fs.h>
 #else
 #include <time.h>
 #endif
@@ -309,3 +313,120 @@ void image_print_contents(const image_header_t *hdr, void *data)
 		}
 	}
 }
+
+#ifdef __BAREBOX__
+struct image_handle *map_image(const char *filename, int verify)
+{
+	int fd;
+	uint32_t checksum, len;
+	struct image_handle *handle;
+	image_header_t *header;
+	int type;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		printf("could not open: %s\n", errno_str());
+		return NULL;
+	}
+
+	handle = xzalloc(sizeof(struct image_handle));
+	header = &handle->header;
+
+	if (read(fd, header, image_get_header_size()) < 0) {
+		printf("could not read: %s\n", errno_str());
+		goto err_out;
+	}
+
+	if (image_get_magic(header) != IH_MAGIC) {
+		puts ("Bad Magic Number\n");
+		goto err_out;
+	}
+
+	checksum = image_get_hcrc(header);
+	header->ih_hcrc = 0;
+
+	if (crc32 (0, (uchar *)header, image_get_header_size()) != checksum) {
+		puts ("Bad Header Checksum\n");
+		goto err_out;
+	}
+	len  = image_get_size(header);
+
+	handle->data = memmap(fd, PROT_READ);
+	if (handle->data == (void *)-1) {
+		handle->data = xmalloc(len);
+		handle->flags = IH_MALLOC;
+		if (read(fd, handle->data, len) < 0) {
+			printf("could not read: %s\n", errno_str());
+			goto err_out;
+		}
+	} else {
+		handle->data = (void *)((unsigned long)handle->data +
+						       image_get_header_size());
+	}
+
+	type = image_get_type(header);
+	if (type == IH_TYPE_MULTI) {
+		struct image_handle_data *data_entries;
+		int i;
+		ulong img_data;
+		ulong count = image_multi_count(handle->data);
+
+		data_entries = xzalloc(sizeof(struct image_handle_data) * count);
+
+		for (i = 0; i < count; i++) {
+			image_multi_getimg(handle->data, i, &img_data,
+					   &data_entries[i].len);
+
+			data_entries[i].data = (void*)img_data;
+		}
+		handle->data_entries = data_entries;
+		handle->nb_data_entries = count;
+	} else {
+		handle->data_entries = gen_image_handle_data(handle->data, len);
+		handle->nb_data_entries = 1;
+	}
+
+	if (verify) {
+		puts ("   Verifying Checksum ... ");
+		if (crc32 (0, handle->data, len) != image_get_dcrc(header)) {
+			printf ("Bad Data CRC\n");
+			goto err_out;
+		}
+		puts ("OK\n");
+	}
+
+	image_print_contents(header, handle->data);
+
+	close(fd);
+
+	return handle;
+err_out:
+	close(fd);
+	if (handle->flags & IH_MALLOC)
+		free(handle->data);
+	free(handle);
+	return NULL;
+}
+EXPORT_SYMBOL(map_image);
+
+void unmap_image(struct image_handle *handle)
+{
+	if (handle->flags & IH_MALLOC)
+		free(handle->data);
+	free(handle->data_entries);
+	free(handle);
+}
+EXPORT_SYMBOL(unmap_image);
+
+struct image_handle_data * gen_image_handle_data(void* data, ulong len)
+{
+	struct image_handle_data *iha;
+
+	iha = xzalloc(sizeof(struct image_handle_data));
+	iha->data = data;
+	iha->len = len;
+
+	return iha;
+}
+EXPORT_SYMBOL(gen_image_handle_data);
+#endif
diff --git a/include/image.h b/include/image.h
index d34d432..691bf2d 100644
--- a/include/image.h
+++ b/include/image.h
@@ -344,6 +344,7 @@ void	print_image_hdr (image_header_t *hdr);
  */
 struct image_handle *map_image(const char *filename, int verify);
 void unmap_image(struct image_handle *handle);
+struct image_handle_data* gen_image_handle_data(void* data, ulong len);
 
 /*
  * Relocate an image to load_address by uncompressing
-- 
1.7.6.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-09-27 14:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-27 14:22 [PATCH 1/3] image: use data base addr for image_print_contents, image_multi_count/getimg Jean-Christophe PLAGNIOL-VILLARD
2011-09-27 14:22 ` [PATCH 2/3] image: add multi image support for bootm Jean-Christophe PLAGNIOL-VILLARD
2011-09-27 14:22 ` [PATCH 3/3] image: move map_image amd unmap_image to image.c Jean-Christophe PLAGNIOL-VILLARD

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox