2 * Copyright (C) 2014 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 static int dump_or_load(int fd, size_t *len, int pgsz, char *fname, int dump)
40 cfd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0644);
42 cfd = open(fname, O_RDONLY);
58 npages = (*len + pgsz - 1) / pgsz;
59 for (i = 1; i <= npages; i++) {
61 rem = *len & (pgsz - 1);
63 r = read(ifd, page, rem);
66 r = write(ofd, page, rem);
71 memset(page, 0, pgsz - rem);
72 write(ofd, page, pgsz - rem);
76 r = read(ifd, page, pgsz);
79 r = write(ofd, page, pgsz);
95 static int dump(int fd, size_t len, int pgsz, char *fname)
97 return dump_or_load(fd, &len, pgsz, fname, 1);
100 static int load(int fd, size_t *len, int pgsz, char *fname)
102 return dump_or_load(fd, len, pgsz, fname, 0);
105 int main(int argc, char **argv)
108 uint32_t kernel, initrd, second, pgsz;
115 fd = open(argv[1], O_RDONLY);
119 r = read(fd, buffer, sizeof(buffer));
123 if (strncmp(buffer, "ANDROID!", 8)) {
126 kernel = *(uint32_t *) p;
128 initrd = *(uint32_t *) p;
130 second = *(uint32_t *) p;
132 pgsz = *(uint32_t *) p;
135 lseek(fd, offset, SEEK_SET);
136 dump(fd, kernel, pgsz, "kernel");
138 offset += ((kernel + pgsz - 1) / pgsz) * pgsz;
140 lseek(fd, offset, SEEK_SET);
141 dump(fd, initrd, pgsz, "initrd");
143 offset += ((initrd + pgsz - 1) / pgsz) * pgsz;
145 lseek(fd, offset, SEEK_SET);
146 dump(fd, second, pgsz, "second");