osd-contiki/tools/avr-makecoffeedata
2009-07-14 19:25:46 +00:00

136 lines
4.4 KiB
Perl

#!/usr/bin/perl
#Generate a .c source that preinitializes a coffee file system
#David Kopf <dak664@embarqmail.com> July 2009
#Assumes coffee file_header structure is
#struct file_header {
# coffee_page_t log_page;
# uint16_t log_records;
# uint16_t log_record_size;
# coffee_page_t max_pages;
# uint8_t deprecated_eof_hint;
# uint8_t flags;
# char name[COFFEE_NAME_LENGTH];
# } __attribute__((packed));
$coffee_page_t=1;
$coffee_name_length=16;
$coffee_header_length=2*$coffee_page_t+$coffee_name_length+6;
#coffee_sector_size is the number of bytes in the smallest allocated block of the coffee file system
$coffee_sector_size=256;
open(OUTPUT, "> httpd-coffeefsdata.c");
chdir("httpd-fs");
opendir(DIR, ".");
@files = grep { !/^\./ && !/(CVS|~)/ } readdir(DIR);
closedir(DIR);
foreach $file (@files) {
# $dirname = fileparse($file);
if(-d $file && $file !~ /^\./) {
print "Processing directory $file\n";
opendir(DIR, $file);
@newfiles = grep { !/^\./ && !/(CVS|~)/ } readdir(DIR);
closedir(DIR);
printf "Adding files @newfiles\n";
@files = (@files, map { $_ = "$file/$_" } @newfiles);
next;
}
}
print(OUTPUT "/**************Generated by /tools/avr-makecoffeedata*****************/\n");
print(OUTPUT "/*For coffee filesystem of sector size $coffee_sector_size and header length $coffee_header_length bytes*/\n");
foreach $file (@files) {if(-f $file) {
if (length($file)>($coffee_name_length-1)) {die "$file: name is too long!";}
print "Adding file $file\n";
open(FILE, $file) || die "Could not open file $file\n";
if (grep /.png/,$file) {binmode FILE;}
if (grep /.jpg/,$file) {binmode FILE;}
if (grep /.gif/,$file) {binmode FILE;}
$file =~ s-^-/-;
$fvar = $file;
$fvar =~ s-/-_-g;
$fvar =~ s-\.-_-g;
$file_length= -s FILE;
$coffee_sectors=int(($coffee_header_length+$file_length+$coffee_sector_size-1)/$coffee_sector_size);
# $coffee_sectors=sprintf("%.0f",($coffee_header_length+$file_length+$coffee_sector_size-1)/$coffee_sector_size)-1;
$coffee_length=$coffee_sectors*$coffee_sector_size;
# for AVR, add PROGMEM here
print(OUTPUT "\n__attribute__ ((section (\".coffeedata\")))\n");
print(OUTPUT "static const char data".$fvar."[$coffee_length] PROGMEM = {\n");
print(OUTPUT "\t/* $file */\n\t ");
#--------------------Header-----------------------------
#log_page
for($j=0;$j<$coffee_page_t;$j++) {print (OUTPUT "0x00, ");}
#log_records, log_record_size
for($j=0;$j<4;$j++) {print (OUTPUT "0x00, ");}
#max_pages
if ($coffee_page_t==1) {
printf(OUTPUT "0x%2.2x, ",$coffee_sectors);
} elsif ($coffee_page_t==2) {
printf(OUTPUT "0x%2.2x, ",($coffee_sectors>> 8)&0xff);
printf(OUTPUT "0x%2.2x, ",($coffee_sectors )&0xff);
} elsif ($coffee_page_t==4) {
printf(OUTPUT "0x%2.2x, ",($coffee_sectors>>24)&0xff);
printf(OUTPUT "0x%2.2x, ",($coffee_sectors>>16)&0xff);
printf(OUTPUT "0x%2.2x, ",($coffee_sectors>> 8)&0xff);
printf(OUTPUT "0x%2.2x, ",($coffee_sectors )&0xff);
} else {
die "Unsupported coffee_page_t $coffee_page_t\n";
}
#eof hint and flags
print(OUTPUT "0x00, 0x03,\n\t");
#file name
for($j = 0; $j < length($file); $j++) {
printf(OUTPUT " %#02.2x,", unpack("C", substr($file, $j, 1)));
}
for(; $j < $coffee_name_length; $j++) {
printf(OUTPUT " 0x00,");
}
print(OUTPUT "\n\t");
#------------------File Data---------------------------
$coffee_length-=$coffee_header_length;
$i = 0;
while(read(FILE, $data, 1)) {
# printf(OUTPUT " %#2.2x,", unpack("C", $data));
printf(OUTPUT " 0x%2.2x,", unpack("C", $data));
$i++;$coffee_length--;
if($i == 10) {
print(OUTPUT "\n\t");
$i = 0;
}
}
while (--$coffee_length>1) {
print (OUTPUT " 0x00,");
if($i++ == 9) {
print(OUTPUT "\n\t");
$i = 0;
}
}
print (OUTPUT " 0x00};\n");
close(FILE);
push(@fvars, $fvar);
push(@pfiles, $file);
}}
for($i = 0; $i < @fvars; $i++) {
$file = $pfiles[$i];
$fvar = $fvars[$i];
if($i == 0) {
$prevfile = "NULL";
} else {
$prevfile = "file" . $fvars[$i - 1];
}
# for AVR, add PROGMEM here
print(OUTPUT "const struct httpd_fsdata_file file".$fvar."[] PROGMEM = {{$prevfile, data$fvar, ");
print(OUTPUT "data$fvar + ". (length($file) + 1) .", ");
print(OUTPUT "sizeof(data$fvar) - ". (length($file) + 1) ."}};\n\n");
}
print(OUTPUT "#define HTTPD_FS_ROOT file$fvars[$i - 1]\n\n");
print(OUTPUT "#define HTTPD_FS_NUMFILES $i\n");