From 689dad6eab2ffd5cbaa2c7c78aa9f68049880aa4 Mon Sep 17 00:00:00 2001 From: Myles Watson Date: Wed, 5 Mar 2008 14:51:35 +0000 Subject: [PATCH] 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 Acked-by: Ronald G. Minnich git-svn-id: svn://coreboot.org/repository/coreboot-v3@632 f3766cd6-281f-0410-b1cd-43a5c92072e9 --- util/lar/lar.c | 30 ++++++++++++++---------------- util/lar/stream.c | 29 +++++++++++++++-------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/util/lar/lar.c b/util/lar/lar.c index dd726704ad..5c6e0f910f 100644 --- a/util/lar/lar.c +++ b/util/lar/lar.c @@ -44,8 +44,9 @@ enum compalgo algo = ALGO_NONE; 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 @@ char *get_bootblock(void) 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 @@ int main(int argc, char *argv[]) } /* 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) { diff --git a/util/lar/stream.c b/util/lar/stream.c index d6dabd401a..3bea09914b 100644 --- a/util/lar/stream.c +++ b/util/lar/stream.c @@ -82,7 +82,7 @@ int output_elf_segments(struct lar *lar, char *name, char *filebuf, 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 @@ static int file_in_list(struct file *files, char *filename) 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 @@ void lar_list_files(struct lar *lar, struct file *files) 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 @@ void lar_list_files(struct lar *lar, struct file *files) 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 @@ void lar_list_files(struct lar *lar, struct file *files) } /* 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 @@ char *mapfile(char *filename, u32 *size) * 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 @@ int hlen(char *pathname) 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 @@ int maxsize(struct lar *lar, char *name) 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 @@ int lar_add_entry(struct lar *lar, char *pathname, void *data, /* 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");