summaryrefslogtreecommitdiff
path: root/tools/release/sims.pl
blob: cc6e61ae6c5a760325536da0544079155d8dd2bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/perl
# This is basically a copy of tools/release/bins.pl, with small changes.
use File::Basename;
use File::Path;
use Cwd;

require "tools/builds.pm";

my $verbose, $strip, $update, $doonly, $version;
my @doonly;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
$year+=1900;
$mon+=1;
$verbose=0;

my $filename = "rockbox-sim-<target>-<version>";

while (scalar @ARGV > 0) {
    if ($ARGV[0] eq "-h") {
        print $ARGV[0]."\n";
        print <<MOO
Usage: w32sims [-v] [-u] [-s] [-w] [-r VERSION] [-f filename] [buildonly]
       -v Verbose output
       -u Run svn up before building
       -r Use the specified version string for filenames (defaults to SVN
          revision)
       -s Strip binaries before zipping them up.
       -w Crosscompile for Windows (requires mingw32)
       -f Filename format string (without extension). This can include a
          filepath (relative or absolute) May include the following special
          strings:
            <version> - Replaced by the revision (or version name if -r is
                        used)
            <target>  - Target shortname
            <YYYY>    - Year (4 digits)
            <MM>      - Month (2 digits)
            <DD>      - Day of month (2 digits - 0-padded)
            <HH>      - Hour of day (2 digits, 0-padded, 00-23)
            <mm>      - Minute (2 digits, 0-padded)
          The default filename is rockbox-sim-<target>-<version>
MOO
;
        exit 1;
    }
    elsif ($ARGV[0] eq "-v") {
        $verbose =1;
    }
    elsif ($ARGV[0] eq "-u") {
        $update =1;
    }
    elsif ($ARGV[0] eq "-s") {
        $strip =1;
    }
    elsif ($ARGV[0] eq "-w") {
        $cross =1;
    }
    elsif ($ARGV[0] eq "-r") {
        $version =$ARGV[1];
        shift @ARGV;
    }
    elsif ($ARGV[0] eq "-f") {
        $filename = $ARGV[1];
        shift @ARGV;
    }
    else {
        push(@doonly,$ARGV[0]);
    }
    shift @ARGV;
}

if($update) {
    # svn update!
    system("svn -q up");
}

$test = `sdl-config --libs`;
if ($test eq "") {
    printf("You don't appear to have sdl-config\n");
    die();
}

$rev = `tools/version.sh .`;
chomp $rev;
print "rev $rev\n" if($verbose);

if (@doonly) {
    printf("Build only %s\n", join(', ', @doonly)) if($verbose);
}

if (!defined($version)) {
    $version = $rev;
}

# made once for all targets
sub runone {
    my ($dir, $extra)=@_;
    my $a;

    if(@doonly > 0 && !grep(/^$dir$/, @doonly)) {
        return;
    }

    mkdir "build-$dir";
    chdir "build-$dir";
    print "Build in build-$dir\n" if($verbose);

    # build the target
    $a = buildit($dir, $extra);

    # Do not continue if the rockboxui executable is not created. This will
    #    prevent a good build getting overwritten by a bad build when
    #    uploaded to the web server.
    unless ( (-e "rockboxui") || (-e "rockboxui.exe") ) {
        print "No rockboxui, clean up and return\n" if($verbose);
        chdir "..";
        system("rm -rf build-$dir");
        return $a;
    }

    if ($strip) {
        print "Stripping binaries\n" if ($verbose);
        # find \( -name "*.exe" -o -name "*.rock" -o -name "*.codec" \) -exec ls -l "{}" ";"
        open(MAKE, "Makefile");
        my $AS=(grep(/^export AS=/, <MAKE>))[0];
        chomp($AS);
        (my $striptool = $AS) =~ s/^export AS=(.*)as$/$1strip/;
        
        $cmd = "find \\( -name 'rockboxui*' -o -iname '*dll' -o -name '*.rock' -o -name '*.codec' \\) -exec $striptool '{}' ';'";
        print("$cmd\n") if ($verbose);
        `$cmd`;
        close(MAKE);
    }

    chdir "..";

    my $newo=$filename;

    $newo =~ s/<version>/$version/g;
    $newo =~ s/<target>/$dir/g;
    $newo =~ s/<YYYY>/$year/g;
    $newo =~ s/<MM>/$mon/g;
    $newo =~ s/<DD>/$mday/g;
    $newo =~ s/<HH>/$hour/g;
    $newo =~ s/<mm>/$min/g;


    print "Zip up the sim and associated files\n" if ($verbose);
    mkpath(dirname($newo));
    system("mv build-$dir $newo");
    if ($cross) {
        print "Find and copy SDL.dll\n" if ($verbose);
        open(MAKE, "$newo/Makefile");
        my $GCCOPTS=(grep(/^export GCCOPTS=/, <MAKE>))[0];
        chomp($GCCOPTS);
        (my $sdldll = $GCCOPTS) =~ s/^export GCCOPTS=.*-I([^ ]+)\/include\/SDL.*$/$1\/bin\/SDL.dll/;
        print "Found $sdldll\n" if ($verbose);
        `cp $sdldll ./$newo/`;
        close(MAKE);
    }
    my $toplevel = getcwd();
    chdir(dirname($newo));
    $cmd = "zip -9 -r -q \"".basename($newo)."\" "
       . "\"".basename($newo)."\"/rockboxui* "
       . "\"".basename($newo)."\"/UI256.bmp "
       . "\"".basename($newo)."\"/SDL.dll "
       . "\"".basename($newo)."\"/simdisk ";
    print("$cmd\n") if($verbose);
    `$cmd`;
    chdir($toplevel);

    print "remove all contents in $newo\n" if($verbose);
    system("rm -rf $newo");

    return $a;
};

sub buildit {
    my ($dir, $extra)=@_;

    `rm -rf * >/dev/null 2>&1`;

    if ($cross) {
        $simstring = 'a\nw\ns\n\n';
    }
    else {
        $simstring = 's\n';
    }

    my $c = sprintf('printf "%s\n%s%s" | ../tools/configure',
                    $dir, $extra, $simstring);

    print "C: $c\n" if($verbose);
    `$c`;

    print "Run 'make'\n" if($verbose);
    `make 2>/dev/null`;

    print "Run 'make install'\n" if($verbose);
    `make install 2>/dev/null`;
}

for my $b (sort byname keys %builds) {
    if ($builds{$b}{status} >= 2)
    {
        # ipodvideo64mb uses the ipodvideo simulator
        # sansae200r uses the sansae200 simulator
        if ($b ne 'ipodvideo64mb' && $b ne 'sansae200r')
        {
            if ($builds{$b}{ram} ne '')
            {
                # These builds need the ram size sent to configure
                runone($b, $builds{$b}{ram} . '\n');
            }
            else
            {
                runone($b);
            }
        }
    }
}

#The following ports are in the unusable category, but the simulator does build
runone("gogearhdd1630");
runone("gogearsa9200");
runone("mini2440");
runone("ondavx747");
runone("ondavx747p");
runone("ondavx777");
runone("sansam200v4");
runone("zenvision");
runone("zenvisionm30gb");
runone("zenvisionm60gb");
runone("creativezenxfi2");
runone("creativezenxfi3");
runone("sonynwze360");
runone("sonynwze370");
runone("samsungypr0");
runone("creativezenxfi");
runone("creativezen");
runone("creativezenmozaic");
="hl opt">) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return -1; } if (fread(image, sizeof(image_t), 1, fw) != 1) { if (ferror(fw)) fprintf(stderr, "fread error (%s), ", strerror(errno)); else if (feof(fw)) fprintf(stderr, "fread length %lu at offset %lu hit EOF, ", sizeof(image_t), offset + entry * sizeof(image_t)); fprintf(stderr, "unable to load boot entry.\n"); return -1; } switch_endian(image); /* If we find an "osos" image with devOffset 0x4800, we have 2048-byte sectors. This isn't 100% future-proof, but works as of December 2006. We display this information so users can spot any false-positives that may occur in the future (although this is unlikely). */ if ((image->id==0x6f736f73) && (image->devOffset==0x4800)) { sectorsize=2048; fprintf(stderr,"Detected 2048-byte sectors\n"); } return 0; } /* store the boot entry to * boot table at offset, * entry number entry * file fw */ int write_entry(image_t *image, FILE *fw, unsigned offset, int entry) { if (fseek(fw, offset + entry * sizeof(image_t), SEEK_SET) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return -1; } switch_endian(image); if (fwrite(image, sizeof(image_t), 1, fw) != 1) { fprintf(stderr, "fwrite error (%s), unable to write boot entry\n", strerror(errno)); switch_endian(image); return -1; } switch_endian(image); return 0; } /* extract a single image from the fw * the first 40 bytes contain a boot table entry, * padded to one block (512 bytes */ int extract(FILE *f, int idx, FILE *out) { image_t *image; unsigned char buf[512]; unsigned off; fseek(f, 0x100 + 10, SEEK_SET); fread(&fw_version, sizeof(fw_version), 1, f); fw_version = switch_16(fw_version); image = (image_t *)buf; /* We need to detect sector size, so always load image 0 directory entry first */ if (load_entry(image, f, TBL, 0) == -1) return -1; if (idx > 0) { /* Now read the real image (if it isn't 0) */ if (load_entry(image, f, TBL, idx) == -1) return -1; } off = image->devOffset + IMAGE_PADDING; if (fseek(f, off, SEEK_SET) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return -1; } if (write_entry(image, out, 0x0, 0) == -1) return -1; if (fseek(out, 512, SEEK_SET) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return -1; } if (copysum(f, out, image->len, off) == -1) return -1; return 0; } /* list all images */ int listall(FILE *f) { image_t *image; unsigned char buf[512]; int idx; fseek(f, 0x100 + 10, SEEK_SET); fread(&fw_version, sizeof(fw_version), 1, f); fw_version = switch_16(fw_version); image = (image_t *)buf; idx = 0; while (idx < 20) { char prefix[32]; if (load_entry(image, f, TBL, idx) < 0) return -1; if (!image->id) break; sprintf (prefix, "%2d: ", idx); print_image (image, prefix); ++idx; } return 0; } /* return the size of f */ unsigned lengthof(FILE *f) { unsigned ret; if (fseek(f, 0, SEEK_END) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return -1; } if ((ret = ftell(f)) == -1) { fprintf(stderr, "ftell failed: %s\n", strerror(errno)); return -1; } return ret; } void test_endian(void) { char ch[4] = { '\0', '\1', '\2', '\3' }; unsigned i = 0x00010203; if (*((int *)ch) == i) be = 1; else be = 0; return; } int main(int argc, char **argv) { int c; int verbose = 0, i, ext = 0; FILE *in = NULL, *out = NULL; char gen_set = 0; image_t images[5]; image_t image = { { '!', 'A', 'T', 'A' }, // magic 0x6f736f73, // id { '\0', '\0', '\0', '\0' }, // pad 0x4400, // devOffset 0, // len 0x28000000, // addr 0, // entry 0, // chksum 0, // vers 0xffffffff // loadAddr }; int images_done = 0; unsigned version = 0, offset = 0, len = 0; int needs_rcsc = 0; test_endian(); /* parse options */ opterr = 0; while ((c = getopt(argc, argv, "3hve:o:i:l:r:g:")) != -1) switch (c) { case 'h': if (verbose || in || out || images_done || ext) { fprintf(stderr, "-[?h] is exclusive with other arguments\n"); usage(); return 1; } usage(); return 0; case 'v': if (verbose++) fprintf(stderr, "Warning: multiple -v options specified\n"); break; case '3': gen_set = 1; fw_version = 3; image.addr = 0x10000000; break; case 'g': gen_set = 1; if ((strcasecmp(optarg, "4g") == 0) || (strcasecmp(optarg, "mini") == 0) || (strcasecmp(optarg, "nano") == 0) || (strcasecmp(optarg, "photo") == 0) || (strcasecmp(optarg, "color") == 0) || (strcasecmp(optarg, "video") == 0) || (strcasecmp(optarg, "5g") == 0)) { fw_version = 3; image.addr = 0x10000000; if ((strcasecmp(optarg, "5g") == 0) || (strcasecmp(optarg, "video") == 0)) { needs_rcsc = 1; } } else if ((strcasecmp(optarg, "1g") != 0) && (strcasecmp(optarg, "2g") != 0) && (strcasecmp(optarg, "3g") != 0) && (strcasecmp(optarg, "scroll") != 0) && (strcasecmp(optarg, "touch") != 0) && (strcasecmp(optarg, "dock") != 0)) { fprintf(stderr, "%s: bad gen. Valid options are: 1g, 2g," " 3g, 4g, 5g, scroll, touch, dock, mini, nano," " photo, color, and video\n", optarg); return 1; } break; case 'o': if (out) { fprintf(stderr, "output already opened\n"); usage(); return 1; } if ((out = fopen(optarg, "wb+")) == NULL) { fprintf(stderr, "Cannot open output file %s\n", optarg); return 1; } break; case 'e': if (!out || images_done || ext) { usage(); return 1; } ext = atoi(optarg) + 1; break; case 'i': if (!out || ext) { usage(); return 1; } if (images_done == 5) { fprintf(stderr, "Only 5 images supported\n"); return 1; } if ((in = fopen(optarg, "rb")) == NULL) { fprintf(stderr, "Cannot open firmware image file %s\n", optarg); return 1; } if (load_entry(images + images_done, in, 0, 0) == -1) return 1; if (!offset) offset = FIRST_OFFSET; else offset = (offset + 0x1ff) & ~0x1ff; images[images_done].devOffset = offset; if (fseek(out, offset, SEEK_SET) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return 1; } if ((images[images_done].chksum = copysum(in, out, images[images_done].len, 0x200)) == -1) return 1; offset += images[images_done].len; if (verbose) print_image(images + images_done, "Apple image added: "); images_done++; fclose(in); break; case 'l': if (!out || ext) { usage(); return 1; } if (images_done == 5) { fprintf(stderr, "Only 5 images supported\n"); return 1; } if ((in = fopen(optarg, "rb")) == NULL) { fprintf(stderr, "Cannot open linux image file %s\n", optarg); return 1; } if (!offset) offset = FIRST_OFFSET; else offset = (offset + 0x1ff) & ~0x1ff; images[images_done] = image; images[images_done].devOffset = offset; if ((images[images_done].len = lengthof(in)) == -1) return 1; if (fseek(out, offset, SEEK_SET) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return 1; } if ((images[images_done].chksum = copysum(in, out, images[images_done].len, 0)) == -1) return 1; offset += images[images_done].len; if (verbose) print_image(images + images_done, "Linux image added: "); images_done++; fclose(in); break; case 'r': if (ext) { usage(); return 1; } version = strtol(optarg, NULL, 16); break; case '?': fprintf(stderr, "invalid option -%c specified\n", optopt); usage(); return 1; case ':': fprintf(stderr, "option -%c needs an argument\n", optopt); usage(); return 1; } if (argc - optind != 1) { usage(); return 1; } if (ext) { if ((in = fopen(argv[optind], "rb")) == NULL) { fprintf(stderr, "Cannot open firmware image file %s\n", argv[optind]); return 1; } if (extract(in, ext-1, out) == -1) return 1; fclose(in); fclose(out); return 0; } else if (!gen_set) { if ((in = fopen(argv[optind], "rb")) != NULL) { // just list all available entries listall (in); fclose(in); return 0; } usage(); return 1; } printf("Generating firmware image compatible with "); if (fw_version == 3) { if (needs_rcsc) { printf("iPod video\n"); } else { printf("iPod mini, 4g and iPod photo/color...\n"); } } else { printf("1g, 2g and 3g iPods...\n"); } if (!images_done) { fprintf(stderr, "no images specified!\n"); return 1; } if ((in = fopen(argv[optind], "rb")) == NULL) { fprintf(stderr, "Cannot open loader image file %s\n", argv[optind]); return 1; } offset = (offset + 0x1ff) & ~0x1ff; if ((len = lengthof(in)) == -1) return 1; if (fseek(out, offset, SEEK_SET) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return 1; } if (copysum(in, out, len, 0) == -1) return 1; for (i=0; i < images_done; i++) { if (images[i].vers > image.vers) image.vers = images[i].vers; if (write_entry(images+i, out, offset+0x0100, i) == -1) return 1; } if (version) image.vers = version; image.len = offset + len - FIRST_OFFSET; image.entryOffset = offset - FIRST_OFFSET; image.devOffset = (sectorsize==512 ? 0x4400 : 0x4800); if ((image.chksum = copysum(out, NULL, image.len, FIRST_OFFSET)) == -1) return 1; if (fseek(out, 0x0, SEEK_SET) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return 1; } if (fwrite(apple_copyright, 0x100, 1, out) != 1) { fprintf(stderr, "fwrite error (%s) while writing copyright\n", strerror(errno)); return 1; } version = switch_32(0x5b68695d); /* magic */ if (fwrite(&version, 4, 1, out) != 1) { fprintf(stderr, "fwrite error (%s) while writing version magic\n", strerror(errno)); return 1; } version = switch_32(0x00004000); /* magic */ if (fwrite(&version, 4, 1, out) != 1) { fprintf(stderr, "fwrite error (%s) while writing version magic\n", strerror(errno)); return 1; } if (fw_version == 3) { version = switch_32(0x0003010c); /* magic */ } else { version = switch_32(0x0002010c); /* magic */ } if (fwrite(&version, 4, 1, out) != 1) { fprintf(stderr, "fwrite error (%s) while writing version magic\n", strerror(errno)); return 1; } if (write_entry(&image, out, TBL, 0) == -1) return 1; if (verbose) print_image(&image, "Master image: "); if (needs_rcsc) { image_t rsrc; if ((in = fopen("apple_sw_5g_rcsc.bin", "rb")) == NULL) { fprintf(stderr, "Cannot open firmware image file %s\n", "apple_sw_5g_rcsc.bin"); return 1; } if (load_entry(&rsrc, in, 0, 0) == -1) { return 1; } rsrc.devOffset = image.devOffset + image.len; rsrc.devOffset = ((rsrc.devOffset + 0x1ff) & ~0x1ff) + 0x200; if (fseek(out, rsrc.devOffset + IMAGE_PADDING, SEEK_SET) == -1) { fprintf(stderr, "fseek failed: %s\n", strerror(errno)); return 1; } if ((rsrc.chksum = copysum(in, out, rsrc.len, 0x200)) == -1) { return 1; } fclose(in); if (write_entry(&rsrc, out, TBL, 1) == -1) { return 1; } if (verbose) print_image(&rsrc, "rsrc image: "); } return 0; }