- return len;
-}
-
-static int write_fw(const char *data, int len)
-{
- FILE *f;
- int ret = EXIT_FAILURE;
-
- f = fopen(ofname, "w");
- if (f == NULL) {
- ERRS("could not open \"%s\" for writing", ofname);
- goto out;
- }
-
- errno = 0;
- fwrite(data, len, 1, f);
- if (errno) {
- ERRS("unable to write output file");
- goto out_flush;
- }
-
- DBG("firmware file \"%s\" completed", ofname);
-
- ret = EXIT_SUCCESS;
-
- out_flush:
- fflush(f);
- fclose(f);
- if (ret != EXIT_SUCCESS) {
- unlink(ofname);
- }
- out:
- return ret;
-}
-
-static int build_fw(void)
-{
- int buflen;
- char *buf;
- char *p;
- int ret = EXIT_FAILURE;
- int writelen = 0;
-
- buflen = layout->fw_max_len;
-
- buf = malloc(buflen);
- if (!buf) {
- ERR("no memory for buffer\n");
- goto out;
- }
-
- memset(buf, 0xff, buflen);
- p = buf + sizeof(struct fw_header);
- ret = read_to_buf(&kernel_info, p);
- if (ret)
- goto out_free_buf;
-
- writelen = sizeof(struct fw_header) + kernel_len;
-
- if (!combined) {
- if (rootfs_align)
- p = buf + writelen;
- else
- p = buf + rootfs_ofs;
-
- ret = read_to_buf(&rootfs_info, p);
- if (ret)
- goto out_free_buf;
-
- if (rootfs_align)
- writelen += rootfs_info.file_size;
- else
- writelen = rootfs_ofs + rootfs_info.file_size;
-
- if (add_jffs2_eof)
- writelen = pad_jffs2(buf, writelen);