<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2656.31">
<TITLE>Multi-file image comment in image.h is misleading</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>I tried using the "How to Add Files to a SELF Ramdisk" (<A HREF="http://www.denx.de/twiki/bin/view/DULG/HowToAddFiles" TARGET="_blank">http://www.denx.de/twiki/bin/view/DULG/HowToAddFiles</A>) instructions to extract a ramdisk from an image until I realized that I am dealing with a multi-file image.</FONT></P>
<P><FONT SIZE=2># mkimage -l pImage</FONT>
<BR><FONT SIZE=2>Image Name: Linux-2.4.22</FONT>
<BR><FONT SIZE=2>Created: Mon Aug 30 15:56:07 2004</FONT>
<BR><FONT SIZE=2>Image Type: PowerPC Linux Multi-File Image (gzip compressed)</FONT>
<BR><FONT SIZE=2>Data Size: 5406339 Bytes = 5279.63 kB = 5.16 MB</FONT>
<BR><FONT SIZE=2>Load Address: 0x00000000</FONT>
<BR><FONT SIZE=2>Entry Point: 0x00000000</FONT>
<BR><FONT SIZE=2>Contents:</FONT>
<BR><FONT SIZE=2> Image 0: 1147513 Bytes = 1120 kB = 1 MB</FONT>
<BR><FONT SIZE=2> Image 1: 4258811 Bytes = 4158 kB = 4 MB</FONT>
</P>
<P><FONT SIZE=2>My image file is 5406403 bytes but the total size of the files inside the image is 5406324 (1147513+4258811) which is a difference of 79 bytes. Given that the header is 64 bytes, I am left with 15 that must be related to the multi-file image type.</FONT></P>
<P><FONT SIZE=2>When looking at include/image.h I read the following:</FONT>
<BR><FONT SIZE=2> * "Multi-File Images" start with a list of image sizes, each</FONT>
<BR><FONT SIZE=2> * image size (in bytes) specified by an "uint32_t" in network</FONT>
<BR><FONT SIZE=2> * byte order. This list is terminated by an "(uint32_t)0".</FONT>
<BR><FONT SIZE=2> * Immediately after the terminating 0 follow the images, one by</FONT>
<BR><FONT SIZE=2> * one, all aligned on "uint32_t" boundaries (size rounded up to</FONT>
<BR><FONT SIZE=2> * a multiple of 4 bytes).</FONT>
</P>
<P><FONT SIZE=2>So my 15 extra bytes were explained as</FONT>
<BR><FONT SIZE=2>4 bytes for the size of the first file</FONT>
<BR><FONT SIZE=2>4 bytes for the size of the second file</FONT>
<BR><FONT SIZE=2>4 bytes for the terminating 0</FONT>
<BR><FONT SIZE=2>3 bytes for the padding on the first file (4 - (1147513 % 4)) = 3</FONT>
</P>
<P><FONT SIZE=2>Now for the confusion...</FONT>
<BR><FONT SIZE=2>The second file should also be padded given the image.h comment above and the fact that (4 - (4258811 % 4)) = 1</FONT>
<BR><FONT SIZE=2>So why was there no padding?</FONT>
</P>
<P><FONT SIZE=2>As it turns out, the code in mkimage.c responsible for copying the data files does not add the padding to the last file in the multi-file image. The code I am referring to is in main() and is as follows:</FONT></P>
<P><FONT SIZE=2>for (;;) {</FONT>
<BR><FONT SIZE=2>char *sep = strchr(file, ':');</FONT>
<BR><FONT SIZE=2>if (sep) {</FONT>
<BR><FONT SIZE=2> *sep = '\0';</FONT>
<BR><FONT SIZE=2> copy_file (ifd, file, 1);</FONT>
<BR><FONT SIZE=2> *sep++ = ':';</FONT>
<BR><FONT SIZE=2> file = sep;</FONT>
<BR><FONT SIZE=2>} else {</FONT>
<BR><FONT SIZE=2> copy_file (ifd, file, 0);</FONT>
<BR><FONT SIZE=2> break;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>With copy_file defined as</FONT>
<BR><FONT SIZE=2>copy_file (int ifd, const char *datafile, int pad)</FONT>
</P>
<P><FONT SIZE=2>Clearly the pad flag is only set when there are more files to copy.</FONT>
</P>
<P><FONT SIZE=2>My suggestion is a simple update to the comment in image.h to state that the last file will not have any padding applied.</FONT></P>
<P><FONT SIZE=2>BTW: Anyone know if a tool for extracting files from a multi-file image exists or do I have to write one myself? ;)</FONT>
</P>
<P><FONT SIZE=2>Jason</FONT>
</P>
</BODY>
</HTML>