Add initial version of lar to the archive.

Signed-off-by: Stefan Reinauer <stepan@coresystems.de>
Acked-By: Stefan Reinauer  <stepan@coresystems.de> and others.


git-svn-id: svn://coreboot.org/repository/LinuxBIOSv3@39 f3766cd6-281f-0410-b1cd-43a5c92072e9
This commit is contained in:
Stefan Reinauer 2006-11-07 17:32:43 +00:00
parent a597d175aa
commit 92efb61fa7
13 changed files with 898 additions and 0 deletions

45
util/lar/Makefile Normal file
View file

@ -0,0 +1,45 @@
#
# lar makefile. This file is GPL. All rights reserved.
#
# (c) 2006 coresystems GmbH
#
SOURCE = lar.c create.c extract.c list.c lib.c
CC=gcc
CFLAGS=-O2 -Wall -W
lar: $(SOURCE)
$(CC) $(CFLAGS) -o $@ $^
strip -s $@
example: example.c
$(CC) $(CFLAGS) -o $@ $^
clean:
rm -rf lar tree.lar tree tree2 example
tree:
@printf "creating sample tree... "
@rm -rf tree
@mkdir tree
@mkdir -p tree/compression
@mkdir -p tree/normal
@mkdir -p tree/fallback
@dd if=/dev/urandom of=tree/compression/lzma bs=1k count=8 &>/dev/null
@dd if=/dev/urandom of=tree/normal/linuxbios.elf.lzma bs=1k count=32 &>/dev/null
@dd if=/dev/urandom of=tree/normal/initmem bs=1k count=16 &>/dev/null
@dd if=/dev/urandom of=tree/fallback/linuxbios.elf.lzma bs=1k count=32 &>/dev/null
@dd if=/dev/urandom of=tree/fallback/initmem bs=1k count=16 &>/dev/null
@printf "done.\n"
tree.lar: lar tree
cd tree; ../lar c ../tree.lar `find . -type f|cut -c3-`
test: lar example tree.lar
mkdir tree2; cd tree2; ../lar x ../tree.lar
@echo Comparing tree:
diff -urN tree tree2
@rm -rf tree tree2
./example tree.lar

87
util/lar/README Normal file
View file

@ -0,0 +1,87 @@
LinuxBIOS Archiver: lar
-----------------------
TOC
- Introduction
- Usage
- Archive format
- TODO
- ChangeLog
Introduction
------------
This is a simple archiver, similar to cpio, ar or tar.
Design goals were
- minimum overhead
- maximum fault tolerance
- simplicity
For a usage example see example.c
For questions contact Stefan Reinauer <stepan@coresystems.de>
Usage
-----
Create archive archive.lar containting files file1 ... fileN
$ lar c archive.lar file1 ... fileN
Extract files from archive.lar
$ lar x archive.lar [file1 .. fileN]
List files in archive:
$ lar l archive.lar
Archive format
--------------
The rough format is:
|--------------|
| header |
|--------------|
| data |
|--------------|
| header |
|--------------|
| data |
|--------------|
...
Headers have to be 16 byte aligned.
|--------------------|
| magic (8byte) |
|--------------------|
| length (4byte) |
|--------------------|
| checksum (4byte) |
|--------------------|
| offset to blob (4b)|
|--------------------|
| "path name" | <-- null terminated, aligned to 16b
|--------------------|
| blob (aligned 16b) |
|--------------------|
TODO
----
* reading flash layouts
* This does not enforce any alignment yet
* Alignment enforcing will be optional.
ChangeLog
2006/10/9
- inital version

126
util/lar/create.c Normal file
View file

@ -0,0 +1,126 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include "lib.h"
#include "lar.h"
int create_lar(int argc, char *argv[])
{
int i, ret;
FILE *archive, *source;
char *archivename;
char *tempmem;
char *filebuf;
char *pathname;
u32 *walk;
u32 csum;
int pathlen, entrylen, filelen;
struct lar_header *header;
struct stat statbuf;
archivename=argv[2];
if (argc<=3) {
printf("No files for archive %s\n", archivename);
exit(1);
}
printf("Opening %s\n", archivename);
archive=fopen(archivename, "w");
if(!archive) {
// error
exit(1);
}
for (i=3; i<argc; i++) {
printf(" Adding %s to archive\n", argv[i]);
ret=stat(argv[i], &statbuf);
if(ret) {
printf(" no such file %s\n", argv[i]);
exit(1);
}
filelen=statbuf.st_size;
tempmem=malloc(sizeof(struct lar_header)+MAX_PATHLEN+filelen+16);
if(!tempmem) {
printf ("no memory\n");
return (1);
}
memset(tempmem, 0, sizeof(struct lar_header)+MAX_PATHLEN+filelen+16);
header=(struct lar_header *)tempmem;
pathname=tempmem+sizeof(struct lar_header);
pathlen=sprintf(pathname, argv[i])+1;
pathlen = (pathlen+15)&0xfffffff0; // align it to 16 bytes
/* read file into memory */
filebuf=pathname+pathlen;
source=fopen(argv[i], "r");
if(!source) {
printf(" no such file %s\n", argv[i]);
exit(1);
}
fread(filebuf, statbuf.st_size, 1, source);
fclose(source);
/* create correct header */
memcpy(header, MAGIC, 8);
header->len=htonl(statbuf.st_size);
header->offset=htonl(sizeof(struct lar_header)+pathlen);
/* calculate checksum */
csum=0;
for (walk=(u32 *)tempmem;
walk<(u32 *)(tempmem+ statbuf.st_size+sizeof(struct lar_header)+pathlen);
walk++) {
csum+=ntohl(*walk);
}
header->checksum=htonl(csum);
/* write out entry to archive */
entrylen=(filelen+pathlen+sizeof(struct lar_header)+15) & 0xfffffff0;
fwrite(tempmem, entrylen, 1, archive);
free(tempmem);
}
fclose(archive);
printf("done.\n");
return 0;
}

28
util/lar/create.h Normal file
View file

@ -0,0 +1,28 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#ifndef __LAR_CREATE_H
#define __LAR_CREATE_H 1
int create_lar(int argc, char *argv[]);
#endif

124
util/lar/example.c Normal file
View file

@ -0,0 +1,124 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
* This file may be dual licensed with the new BSD license.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include "lar.h"
struct mem_file {
char *start;
int len;
};
int find_file(struct mem_file *archive, char *filename, struct mem_file *result)
{
char * walk, *fullname;
struct lar_header * header;
for (walk = archive->start; walk < archive->start +
archive->len; walk+=16) {
if(strcmp(walk, MAGIC)!=0)
continue;
header=(struct lar_header *)walk;
fullname=walk+sizeof(struct lar_header);
// FIXME: check checksum
if(strcmp(fullname, filename)!=0) {
result->start=walk + ntohl(header->offset);
result->len=ntohl(header->len);
return 0;
}
// skip file
walk += ( ntohl(header->offset) + ntohl(header->len)
+ 15 ) & 0xfffffff0;
}
return 1;
}
int main(int argc, char *argv[])
{
int fd, ret;
struct stat statbuf;
struct mem_file archive, result;
if (argc!=2) {
printf("Usage: example archive.lar\n");
exit(0);
}
if (stat(argv[1], &statbuf)!=0) {
printf("Error opening %s: %s\n",
argv[1], strerror(errno));
exit(1);
}
printf("Opening %s\n", argv[1]);
fd=open(argv[1], O_RDONLY);
if (fd==-1) {
printf("Error while opening %s: %s\n",
argv[1], strerror(errno));
exit(1);
}
archive.len=statbuf.st_size;
archive.start=mmap(NULL, statbuf.st_size, PROT_READ,
MAP_SHARED, fd, 0);
/* OS stuff ends here */
/* ------------------------------------------------- */
// find first compressor
ret=find_file(&archive, "compression/", &result);
if(!ret) printf("file found.\n");
else printf("file not found.\n");
ret=find_file(&archive, "normal/initram", &result);
if(!ret) printf("file found.\n");
else printf("file not found.\n");
/* ------------------------------------------------- */
/* OS stuff starts again here */
munmap(archive.start, archive.len);
close(fd);
return 0;
}

127
util/lar/extract.c Normal file
View file

@ -0,0 +1,127 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include "lib.h"
#include "lar.h"
int extract_lar(int argc, char *argv[])
{
int archivefile;
char * archivename;
char * inmap;
char * walk;
char *fullname, *pathname, *pos;
struct lar_header * header;
struct stat statbuf;
int archivelen;
int do_extract;
int i;
archivename=argv[2];
if (stat(archivename, &statbuf)!=0) {
printf("Error opening %s: %s\n",
archivename, strerror(errno));
exit(1);
}
printf("Opening %s\n", archivename);
archivefile=open(archivename, O_RDONLY);
if (archivefile==-1) {
printf("Error while opening %s: %s\n",
archivename, strerror(errno));
exit(1);
}
archivelen=statbuf.st_size;
inmap=mmap(NULL, statbuf.st_size, PROT_READ,
MAP_SHARED, archivefile, 0);
for (walk=inmap; walk<inmap+statbuf.st_size; walk+=16) {
FILE * file_to_extract;
if(strcmp(walk, MAGIC)!=0)
continue;
header=(struct lar_header *)walk;
fullname=walk+sizeof(struct lar_header);
// FIXME: check checksum
do_extract=1;
if(argc>3) {
do_extract=0;
for (i=3; i<argc; i++) {
if(strcmp(fullname, argv[i])==0) {
do_extract=1;
break;
}
}
}
// dont extract this one, skip it.
if(!do_extract)
continue;
printf(" Extracting file %s\n", walk+sizeof(struct lar_header));
// Create the directory if it does not exist.
pathname=strdup(fullname);
pos=strrchr(pathname, '/');
if(pos) {
pos[1]=0;
//printf("pathname %s\n",pathname);
mkdirp(pathname);
}
free(pathname);
file_to_extract=fopen(fullname, "w");
if(!file_to_extract) {
printf("error creating file.\n");
exit(1);
}
//printf("starting offs=%d, len=%d\n", ntohl(header->offset),
// ntohl(header->len));
fwrite(walk+ntohl(header->offset), ntohl(header->len),
1, file_to_extract);
fclose(file_to_extract);
}
munmap(inmap, statbuf.st_size);
close(archivefile);
printf("done.\n");
return 0;
}

28
util/lar/extract.h Normal file
View file

@ -0,0 +1,28 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#ifndef __LAR_EXTRACT_H
#define __LAR_EXTRACT_H 1
int extract_lar(int argc, char *argv[]);
#endif

58
util/lar/lar.c Normal file
View file

@ -0,0 +1,58 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "lib.h"
#include "lar.h"
#include "create.h"
#include "extract.h"
#include "list.h"
int main(int argc, char *argv[])
{
if (argc<2) {
printf("Usage: lar [cxl] archive.lar <files>\n");
exit(0);
}
if (strcmp(argv[1], "x")==0)
extract_lar(argc, argv);
else if (strcmp(argv[1], "c")==0)
create_lar(argc,argv);
else if (strcmp(argv[1], "l")==0)
list_lar(argc,argv);
else {
printf("mode must be c or x\n");
exit(1);
}
return 0;
}

39
util/lar/lar.h Normal file
View file

@ -0,0 +1,39 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
* This file may be dual licensed with the new BSD license.
*
*/
#include <stdint.h>
#define MAGIC "LARCHIVE"
#define MAX_PATHLEN 1024
typedef uint32_t u32;
struct lar_header {
char magic[8];
u32 len;
u32 checksum;
u32 offset;
};

75
util/lar/lib.c Normal file
View file

@ -0,0 +1,75 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int mkdirp(char *dirpath)
{
#define MAX_PATH 1024
char *pos, *currpath, *path;
char cwd[MAX_PATH];
int ret=0;
path = strdup(dirpath);
currpath = path;
if(!getcwd(cwd, MAX_PATH)) {
free(path);
printf("error getting cwd\n");
return -1;
}
do {
pos=index(currpath,'/');
if (pos)*pos=0;
//printf("cp=%s\n",currpath);
mkdir(currpath,0755);
ret=chdir(currpath);
if(pos) currpath=pos+1;
} while (pos && !ret && strlen(currpath)) ;
chdir(cwd);
free(path);
return ret;
}
#if 0
int main(void) {
int ret;
ret=mkdirp("a/b/c/d/");
if (ret) printf("error! mkdir\n\n");
else printf("jippie! mkdir\n\n");
ret=mkdirp("a/b/c/d");
if (ret) printf("error! mkdir\n");
else printf("jippie! mkdir\n");
return 0;
}
#endif

28
util/lar/lib.h Normal file
View file

@ -0,0 +1,28 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#ifndef __LAR_LIB_H
#define __LAR_LIB_H 1
int mkdirp(char *dirpath);
#endif

105
util/lar/list.c Normal file
View file

@ -0,0 +1,105 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include "lib.h"
#include "lar.h"
int list_lar(int argc, char *argv[])
{
int archivefile;
char * archivename;
char * inmap;
char * walk;
char *fullname;
struct lar_header * header;
struct stat statbuf;
int archivelen;
int do_extract;
int i;
archivename=argv[2];
if (stat(archivename, &statbuf)!=0) {
printf("Error opening %s: %s\n",
archivename, strerror(errno));
exit(1);
}
printf("Opening %s\n", archivename);
archivefile=open(archivename, O_RDONLY);
if (archivefile==-1) {
printf("Error while opening %s: %s\n",
archivename, strerror(errno));
exit(1);
}
archivelen=statbuf.st_size;
inmap=mmap(NULL, statbuf.st_size, PROT_READ,
MAP_SHARED, archivefile, 0);
for (walk=inmap; walk<inmap+statbuf.st_size; walk+=16) {
if(strcmp(walk, MAGIC)!=0)
continue;
header=(struct lar_header *)walk;
fullname=walk+sizeof(struct lar_header);
do_extract=1;
if(argc>3) {
do_extract=0;
for (i=3; i<argc; i++) {
if(strcmp(fullname, argv[i])==0) {
do_extract=1;
break;
}
}
}
// dont extract this one, skip it.
if(!do_extract)
continue;
printf(" %s ", walk+sizeof(struct lar_header));
printf("(%d bytes @0x%lx)\n", ntohl(header->len),
(walk-inmap)+ntohl(header->offset));
}
munmap(inmap, statbuf.st_size);
close(archivefile);
printf("done.\n");
return 0;
}

28
util/lar/list.h Normal file
View file

@ -0,0 +1,28 @@
/*
* lar - linuxbios archiver
*
* written by Stefan Reinauer <stepan@coresystems.de>
*
* (C) 2006 coresystems GmbH. All rights reserved.
*
* 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; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
#ifndef __LAR_LIST_H
#define __LAR_LIST_H 1
int list_lar(int argc, char *argv[]);
#endif