+static int copy_file(struct image_tool_params *params, int ifd,
+ const char *file, int padded_size)
+{
+ int dfd;
+ struct stat sbuf;
+ unsigned char *ptr;
+ int size;
+
+ if (params->vflag)
+ fprintf(stderr, "Adding Image %s\n", file);
+
+ dfd = open(file, O_RDONLY | O_BINARY);
+ if (dfd < 0) {
+ fprintf(stderr, "%s: Can't open %s: %s\n",
+ params->cmdname, file, strerror(errno));
+ return -1;
+ }
+
+ if (fstat(dfd, &sbuf) < 0) {
+ fprintf(stderr, "%s: Can't stat %s: %s\n",
+ params->cmdname, file, strerror(errno));
+ goto err_close;
+ }
+
+ if (params->vflag)
+ fprintf(stderr, "Size %u(pad to %u)\n",
+ (int)sbuf.st_size, padded_size);
+
+ ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
+ if (ptr == MAP_FAILED) {
+ fprintf(stderr, "%s: Can't read %s: %s\n",
+ params->cmdname, file, strerror(errno));
+ goto err_munmap;
+ }
+
+ size = sbuf.st_size;
+ if (write(ifd, ptr, size) != size) {
+ fprintf(stderr, "%s: Write error on %s: %s\n",
+ params->cmdname, params->imagefile, strerror(errno));
+ goto err_munmap;
+ }
+
+ munmap((void *)ptr, sbuf.st_size);
+ close(dfd);
+ return pad_file(params, ifd, padded_size - size);
+
+err_munmap:
+ munmap((void *)ptr, sbuf.st_size);
+err_close:
+ close(dfd);
+ return -1;
+}
+
+int rockchip_copy_image(int ifd, struct image_tool_params *params)
+{
+ int ret;
+
+ ret = copy_file(params, ifd, spl_params.init_file,
+ spl_params.init_size);
+ if (ret)
+ return ret;
+
+ if (spl_params.boot_file) {
+ ret = copy_file(params, ifd, spl_params.boot_file,
+ spl_params.boot_size);
+ if (ret)
+ return ret;
+ }