Author: myles
Date: 2008-03-05 15:51:35 +0100 (Wed, 05 Mar 2008)
New Revision: 632
Modified:
coreboot-v3/util/lar/lar.c
coreboot-v3/util/lar/stream.c
Log:
This patch fixes lar options parsing, a seg fault with long path names, and
makes use of functions that were already defined. It also adds greedy name
matching for listing and extracting archives, which allows recursive descent
into the lar directory structure.
changes file-by-file:
util/lar/lar.c:
add more options to the usage message
use get_larsize() instead of using larsize
rearrange errors from parsing args to be more correct
util/lar/stream.c:
change elfname size to MAX_PATHLEN instead of 64
make file_in_list greedy with filename matches
change total_size calculation to include file names
change lar_add_entry to use header_len function instead of reinventing
Signed-off-by: Myles Watson <mylesgw(a)gmail.com>
Acked-by: Ronald G. Minnich <rminnich(a)gmail.com>
Modified: coreboot-v3/util/lar/lar.c
===================================================================
--- coreboot-v3/util/lar/lar.c 2008-03-05 12:57:10 UTC (rev 631)
+++ coreboot-v3/util/lar/lar.c 2008-03-05 14:51:35 UTC (rev 632)
@@ -44,8 +44,9 @@
static void usage(char *name)
{
- printf("\nLAR - the Lightweight Archiver " VERSION "\n" COPYRIGHT "\n\n"
- "Usage: %s [-ecxal] archive.lar [[[file1] file2] ...]\n\n", name);
+ printf("\nLAR - the Lightweight Archiver " VERSION "\n" COPYRIGHT "\n\n");
+ printf("Usage: %s [-ecxal] archive.lar [-b NAME] [-s SIZE[M|k]]\n",name);
+ printf("\t[[[file1] file2] ...]\n\n");
printf("Examples:\n");
printf(" lar -c -s 32k -b bootblock myrom.lar foo nocompress:bar\n");
printf(" lar -a myrom.lar foo blob:baz\n");
@@ -121,7 +122,7 @@
int create_lar(const char *archivename, struct file *files)
{
- struct lar *lar = lar_new_archive(archivename, larsize);
+ struct lar *lar = lar_new_archive(archivename, get_larsize());
if (lar == NULL) {
fprintf(stderr, "Unable to create %s as a LAR archive.\n",
@@ -343,24 +344,21 @@
}
/* size only makes sense when creating a lar */
- if (larmode != CREATE && larsize) {
+ if (larmode != CREATE && get_larsize()) {
printf("Warning: size parameter ignored since "
"not creating an archive.\n");
}
- if (bootblock) {
+ if (larmode == CREATE && !get_larsize()) {
+ printf("When creating a LAR archive, "
+ "you must specify an archive size.\n");
+ exit(1);
+ }
- /* adding a bootblock only makes sense when creating a lar */
- if (larmode != CREATE) {
- printf("Warning: bootblock parameter ignored since "
- "not creating an archive.\n");
- }
-
- /* adding a bootblock only makes sense when creating a lar */
- if (!larsize) {
- printf("When creating a LAR archive, you must specify an archive size.\n");
- exit(1);
- }
+ /* adding a bootblock only makes sense when creating a lar */
+ if (bootblock && larmode != CREATE) {
+ printf("Warning: bootblock parameter ignored since "
+ "not creating an archive.\n");
}
if (optind < argc) {
Modified: coreboot-v3/util/lar/stream.c
===================================================================
--- coreboot-v3/util/lar/stream.c 2008-03-05 12:57:10 UTC (rev 631)
+++ coreboot-v3/util/lar/stream.c 2008-03-05 14:51:35 UTC (rev 632)
@@ -82,7 +82,7 @@
int size;
int segment = 0;
char *header;
- char ename[64];
+ char ename[MAX_PATHLEN];
int headers;
char *temp;
enum compalgo thisalgo;
@@ -529,7 +529,7 @@
return 1;
for(p = files ; p; p = p->next) {
- if (!strcmp(p->name, filename))
+ if (!strncmp(p->name, filename, strlen(p->name)))
return 1;
}
@@ -560,17 +560,17 @@
break;
filename = (char *) (ptr + sizeof(struct lar_header));
- total_size += sizeof(struct lar_header);
if (file_in_list(files, filename)) {
printf(" %s ", filename);
+ total_size += ntohl(header->offset);
+ total_size += ntohl(header->len);
if (ntohl(header->compression) == ALGO_NONE) {
printf("(%d bytes @0x%lx);",
ntohl(header->len),
(unsigned long)(ptr - lar->map) +
ntohl(header->offset));
- total_size += ntohl(header->len);
} else {
printf("(%d bytes, %s compressed to %d bytes "
"@0x%lx);",
@@ -579,7 +579,6 @@
ntohl(header->len),
(unsigned long)(ptr - lar->map) +
ntohl(header->offset));
- total_size += ntohl(header->len);
}
printf("loadaddress 0x%#x entry 0x%#x\n",
ntohl(header->loadaddress),
@@ -603,8 +602,11 @@
}
/* Print the total size */
- printf ("Total size = %d bytes (0x%x)\n", total_size, total_size);
-
+ if (total_size >0)
+ printf("Total size = %dB %dKB (0x%x)\n",
+ total_size, total_size/1024, total_size);
+ else
+ printf("No matching lar entries found.\n");
}
/**
@@ -797,9 +799,10 @@
* Given a name, return the size of the header for that name.
*
* @param name header name
+ * @param new_pathlen pointer to the (possibly truncated) pathlen
* @return header size
*/
-int hlen(char *pathname)
+int header_len(char *pathname, int *new_pathlen)
{
int pathlen;
int len;
@@ -808,6 +811,8 @@
MAX_PATHLEN : strlen(pathname) + 1;
len = sizeof(struct lar_header) + pathlen;
len = (len + 15) & 0xFFFFFFF0;
+ if (new_pathlen!=NULL)
+ *new_pathlen = pathlen;
return len;
}
@@ -827,7 +832,7 @@
offset = lar_empty_offset(lar);
/* Figure out how big our header will be */
- size = get_bootblock_offset(lar->size) - offset - hlen(name) - 1;
+ size = get_bootblock_offset(lar->size) - offset - header_len(name,NULL) - 1;
return size;
}
@@ -878,12 +883,8 @@
/* Find the beginning of the available space in the LAR */
offset = lar_empty_offset(lar);
- pathlen = strlen(pathname) + 1 > MAX_PATHLEN ?
- MAX_PATHLEN : strlen(pathname) + 1;
-
/* Figure out how big our header will be */
- hlen = sizeof(struct lar_header) + pathlen;
- hlen = (hlen + 15) & 0xFFFFFFF0;
+ hlen = header_len(pathname, &pathlen);
if (offset + hlen + complen >= get_bootblock_offset(lar->size)) {
err("Not enough room in the LAR to add the file.\n");