static int do_mkdir(struct command *cmdtp, int argc, char *argv[]) { int opt, parent = 0, ret; while((opt = getopt(argc, argv, "p")) > 0) { switch(opt) { case 'p': parent = 1; break; default: return 1; } } if (optind == argc) return COMMAND_ERROR_USAGE; while (optind < argc) { if (parent) { ret = make_directory(argv[optind]); if (ret == -EEXIST) ret = 0; } else { ret = mkdir(argv[optind], 0); } if (ret) { printf("could not create %s: %s\n", argv[optind], errno_str()); return 1; } optind++; } return 0; }
static int save_file(const char *path) { struct line *line, *tmp; int fd; int ret = 0; fd = open(path, O_WRONLY | O_TRUNC | O_CREAT); if (fd < 0) { printf("could not open file for writing: %s\n", errno_str()); return fd; } line = buffer; while(line) { tmp = line->next; ret = write_full(fd, line->data, strlen(line->data)); if (ret < 0) goto out; ret = write_full(fd, "\n", 1); if (ret < 0) goto out; line = tmp; } ret = 0; out: close(fd); return ret; }
static int do_rm(int argc, char *argv[]) { int i, opt, recursive = 0; while ((opt = getopt(argc, argv, "r")) > 0) { switch (opt) { case 'r': recursive = 1; break; default: return COMMAND_ERROR_USAGE; } } if (argc < 2) return COMMAND_ERROR_USAGE; i = optind; while (i < argc) { int ret; if (recursive) ret = unlink_recursive(argv[i], NULL); else ret = unlink(argv[i]); if (ret) { printf("could not remove %s: %s\n", argv[i], errno_str()); return 1; } i++; } return 0; }
void perror(const char *s) { #ifdef CONFIG_ERRNO_MESSAGES printf("%s: %s\n", s, errno_str()); #else printf("%s returned with %d\n", s, errno); #endif }
int coreconnector_send(struct core_connector *co, void *param) { corebuf* buf = (corebuf*)param; int err = start_send(co->socket, buf); if (err) { fprintf(stderr, "coreconnector_send failure [%s]\n", errno_str(err)); } return err; }
/** * Make the current environment persistent * @param[in] filename where to store * @param[in] dirname what to store (all files in this dir) * @return 0 on success, anything else in case of failure * * Note: This function will also be used on the host! See note in the header * of this file. */ int envfs_save(char *filename, char *dirname) { struct envfs_super *super; int envfd, size, ret; struct action_data data; void *buf = NULL; data.writep = NULL; data.base = dirname; /* first pass: calculate size */ recursive_action(dirname, ACTION_RECURSE, file_size_action, NULL, &data, 0); size = (unsigned long)data.writep; buf = xzalloc(size + sizeof(struct envfs_super)); data.writep = buf + sizeof(struct envfs_super); super = (struct envfs_super *)buf; super->magic = ENVFS_32(ENVFS_MAGIC); super->major = ENVFS_MAJOR; super->minor = ENVFS_MINOR; super->size = ENVFS_32(size); /* second pass: copy files to buffer */ recursive_action(dirname, ACTION_RECURSE, file_save_action, NULL, &data, 0); super->crc = ENVFS_32(crc32(0, buf + sizeof(struct envfs_super), size)); super->sb_crc = ENVFS_32(crc32(0, buf, sizeof(struct envfs_super) - 4)); envfd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if (envfd < 0) { printf("Open %s %s\n", filename, errno_str()); ret = envfd; goto out1; } if (write(envfd, buf, size + sizeof(struct envfs_super)) < sizeof(struct envfs_super)) { perror("write"); ret = -1; /* FIXME */ goto out; } ret = 0; out: close(envfd); out1: free(buf); return ret; }
static int edit_read_file(const char *path) { struct line *line; struct line *lastline = NULL; char *filebuffer; char *linestr, *lineend; struct stat s; if (!stat(path, &s)) { filebuffer = read_file(path, NULL); if (!filebuffer) { printf("could not read %s: %s\n", path, errno_str()); return -1; } linestr = filebuffer; while (1) { if (!*linestr) break; lineend = strchr(linestr, '\n'); if (!lineend && !*linestr) break; if (lineend) *lineend = 0; line = line_realloc(strlen(linestr) + 1, NULL); if (!buffer) buffer = line; memcpy(line->data, linestr, strlen(linestr) + 1); line->prev = lastline; if (lastline) lastline->next = line; line->next = NULL; lastline = line; if (!lineend) break; linestr = lineend + 1; } free(filebuffer); } if (!buffer) { buffer = line_realloc(0, NULL); buffer->data[0] = 0; } return 0; }
static void THREAD_START_ROUTINE(void* param) { DWORD bytes; DWORD last_error; DWORD completion_key; LPOVERLAPPED op; BOOL ret; struct msghead* msg; struct core_poller* io = (struct core_poller*)param; assert(io); for (;;) { if (InterlockedCompareExchange(&io->in_timer, 1, 0) == 0) { coretimer_update(io->timer, io); InterlockedExchange(&io->in_timer, 0); } completion_key = 0; SetLastError(0); ret = GetQueuedCompletionStatus(io->fd, &bytes, &completion_key, &op, 500); last_error = GetLastError(); if (ret != TRUE) { if (last_error == WAIT_TIMEOUT) { continue; } } if (op == &io->quit) { if (--bytes > 0) { PostQueuedCompletionStatus(io->fd, bytes, (ULONG_PTR)io, &io->quit); } else { io->quited = 1; } break; } else if (op) { msg = (struct msghead*)op; msg->call(io, (void*)completion_key, msg, bytes, last_error); } else { fprintf(stderr, "GetQueuedCompletionStatus %s\n", errno_str(last_error)); assert(FALSE); } } }
static int do_rmdir(struct command *cmdtp, int argc, char *argv[]) { int i = 1; if (argc < 2) return COMMAND_ERROR_USAGE; while (i < argc) { if (rmdir(argv[i])) { printf("could not remove %s: %s\n", argv[i], errno_str()); return 1; } i++; } return 0; }
/** * @param[in] cmdtp FIXME * @param[in] argc Argument count from command line * @param[in] argv List of input arguments */ static int do_cat(struct command *cmdtp, int argc, char *argv[]) { int ret; int fd, i; char *buf; int err = 0; int args = 1; if (argc < 2) { perror("cat"); return 1; } buf = xmalloc(BUFSIZE); while (args < argc) { fd = open(argv[args], O_RDONLY); if (fd < 0) { err = 1; printf("could not open %s: %s\n", argv[args], errno_str()); goto out; } while((ret = read(fd, buf, BUFSIZE)) > 0) { for(i = 0; i < ret; i++) { if (isprint(buf[i]) || buf[i] == '\n' || buf[i] == '\t') putchar(buf[i]); else putchar('.'); } if(ctrlc()) { err = 1; close(fd); goto out; } } close(fd); args++; } out: free(buf); return err; }
static int save_file(const char *path) { struct line *line, *tmp; int fd; fd = open(path, O_WRONLY | O_TRUNC | O_CREAT); if (fd < 0) { printf("could not open file for writing: %s\n", errno_str()); return -1; } line = buffer; while(line) { tmp = line->next; write(fd, line->data, strlen(line->data)); write(fd, "\n", 1); line = tmp; } close(fd); return 0; }
struct core_poller* corepoller_new(void) { HANDLE h; struct core_poller* ret; DWORD last_error; SetLastError(0); h= CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0); if (h == NULL) { last_error = GetLastError(); fprintf(stderr, "corepoller_new CreateIoCompletionPort failure %s\n", errno_str(last_error)); return NULL; } ret = (struct core_poller*)malloc(sizeof(*ret)); if (ret == NULL) { fprintf(stderr, "corepoller_new malloc failure\n"); return NULL; } ret->fd = h; ret->thr = 0; ret->quited = 1; ret->timer = coretimer_new(); return ret; }
int file_save_action(const char *filename, struct stat *statbuf, void *userdata, int depth) { struct action_data *data = userdata; struct envfs_inode *inode; struct envfs_inode_end *inode_end; int fd; int namelen = strlen(filename) + 1 - strlen(data->base); inode = (struct envfs_inode*)data->writep; inode->magic = ENVFS_32(ENVFS_INODE_MAGIC); inode->headerlen = ENVFS_32(PAD4(namelen + sizeof(struct envfs_inode_end))); data->writep += sizeof(struct envfs_inode); strcpy(data->writep, filename + strlen(data->base)); data->writep += PAD4(namelen); inode_end = (struct envfs_inode_end*)data->writep; data->writep += sizeof(struct envfs_inode_end); inode_end->magic = ENVFS_32(ENVFS_INODE_END_MAGIC); inode_end->mode = ENVFS_32(S_IRWXU | S_IRWXG | S_IRWXO); if (S_ISLNK(statbuf->st_mode)) { char path[PATH_MAX]; int len; memset(path, 0, PATH_MAX); if (readlink(filename, path, PATH_MAX - 1) < 0) { perror("read"); goto out; } len = strlen(path) + 1; inode_end->mode |= ENVFS_32(S_IFLNK); memcpy(data->writep, path, len); inode->size = ENVFS_32(len); data->writep += PAD4(len); debug("handling symlink %s size %ld namelen %d headerlen %d\n", filename + strlen(data->base), len, namelen, ENVFS_32(inode->headerlen)); } else { debug("handling file %s size %ld namelen %d headerlen %d\n", filename + strlen(data->base), statbuf->st_size, namelen, ENVFS_32(inode->headerlen)); inode->size = ENVFS_32(statbuf->st_size); fd = open(filename, O_RDONLY); if (fd < 0) { printf("Open %s %s\n", filename, errno_str()); goto out; } if (read(fd, data->writep, statbuf->st_size) < statbuf->st_size) { perror("read"); goto out; } close(fd); data->writep += PAD4(statbuf->st_size); } out: return 1; }
/** * Make the current environment persistent * @param[in] filename where to store * @param[in] dirname what to store (all files in this dir) * @param[in] flags superblock flags (refer ENVFS_FLAGS_* macros) * @return 0 on success, anything else in case of failure * * Note: This function will also be used on the host! See note in the header * of this file. */ int envfs_save(const char *filename, const char *dirname, unsigned flags) { struct envfs_super *super; int envfd, size, ret; struct action_data data = {}; void *buf = NULL, *wbuf; struct envfs_entry *env; if (!filename) filename = default_environment_path_get(); if (!dirname) dirname = "/env"; data.writep = NULL; data.base = dirname; #ifdef __BAREBOX__ defaultenv_load(TMPDIR, 0); #endif if (flags & ENVFS_FLAGS_FORCE_BUILT_IN) { size = 0; /* force no content */ } else { /* first pass: calculate size */ recursive_action(dirname, ACTION_RECURSE, file_action, NULL, &data, 0); recursive_action("/.defaultenv", ACTION_RECURSE, file_remove_action, NULL, &data, 0); size = 0; for (env = data.env; env; env = env->next) { size += PAD4(env->size); size += sizeof(struct envfs_inode); size += PAD4(strlen(env->name) + 1); size += sizeof(struct envfs_inode_end); } } buf = xzalloc(size + sizeof(struct envfs_super)); data.writep = buf + sizeof(struct envfs_super); super = buf; super->magic = ENVFS_32(ENVFS_MAGIC); super->major = ENVFS_MAJOR; super->minor = ENVFS_MINOR; super->size = ENVFS_32(size); super->flags = ENVFS_32(flags); if (!(flags & ENVFS_FLAGS_FORCE_BUILT_IN)) { /* second pass: copy files to buffer */ env = data.env; while (env) { struct envfs_entry *next = env->next; envfs_save_inode(&data, env); free(env->buf); free(env->name); free(env); env = next; } } super->crc = ENVFS_32(crc32(0, buf + sizeof(struct envfs_super), size)); super->sb_crc = ENVFS_32(crc32(0, buf, sizeof(struct envfs_super) - 4)); envfd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if (envfd < 0) { printf("could not open %s: %s\n", filename, errno_str()); ret = -errno; goto out1; } ret = protect(envfd, ~0, 0, 0); /* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */ if (ret && errno != ENOSYS && errno != EOPNOTSUPP) { printf("could not unprotect %s: %s\n", filename, errno_str()); goto out; } ret = erase(envfd, ~0, 0); /* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */ if (ret && errno != ENOSYS && errno != EOPNOTSUPP) { printf("could not erase %s: %s\n", filename, errno_str()); goto out; } size += sizeof(struct envfs_super); wbuf = buf; while (size) { ssize_t now = write(envfd, wbuf, size); if (now < 0) { ret = -errno; goto out; } wbuf += now; size -= now; } ret = protect(envfd, ~0, 0, 1); /* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */ if (ret && errno != ENOSYS && errno != EOPNOTSUPP) { printf("could not protect %s: %s\n", filename, errno_str()); goto out; } ret = 0; out: close(envfd); out1: free(buf); #ifdef __BAREBOX__ unlink_recursive(TMPDIR, NULL); #endif return ret; }
static int do_ls(struct command *cmdtp, int argc, char *argv[]) { int ret, opt, o; struct stat s; ulong flags = LS_COLUMN; struct string_list sl; while((opt = getopt(argc, argv, "RCl")) > 0) { switch(opt) { case 'R': flags |= LS_RECURSIVE | LS_SHOWARG; break; case 'C': flags |= LS_COLUMN; break; case 'l': flags &= ~LS_COLUMN; break; } } if (argc - optind > 1) flags |= LS_SHOWARG; if (optind == argc) { ret = ls(getcwd(), flags); return ret ? 1 : 0; } string_list_init(&sl); o = optind; /* first pass: all files */ while (o < argc) { ret = stat(argv[o], &s); if (ret) { printf("%s: %s: %s\n", argv[0], argv[o], errno_str()); o++; continue; } if (!(s.st_mode & S_IFDIR)) { if (flags & LS_COLUMN) string_list_add_sorted(&sl, argv[o]); else ls_one(argv[o], &s); } o++; } if (flags & LS_COLUMN) string_list_print_by_column(&sl); string_list_free(&sl); o = optind; /* second pass: directories */ while (o < argc) { ret = stat(argv[o], &s); if (ret) { o++; continue; } if (s.st_mode & S_IFDIR) { ret = ls(argv[o], flags); if (ret) { perror("ls"); o++; continue; } } o++; } return 0; }
/** * Restore the last environment into the current one * @param[in] filename from where to restore * @param[in] dir where to store the last content * @return 0 on success, anything else in case of failure * * Note: This function will also be used on the host! See note in the header * of this file. */ int envfs_load(char *filename, char *dir) { struct envfs_super super; void *buf = NULL, *buf_free = NULL; int envfd; int fd, ret = 0; char *str, *tmp; int headerlen_full; unsigned long size; /* for envfs < 1.0 */ struct envfs_inode_end inode_end_dummy; inode_end_dummy.mode = ENVFS_32(S_IRWXU | S_IRWXG | S_IRWXO); inode_end_dummy.magic = ENVFS_32(ENVFS_INODE_END_MAGIC); envfd = open(filename, O_RDONLY); if (envfd < 0) { printf("Open %s %s\n", filename, errno_str()); return -1; } /* read superblock */ ret = read(envfd, &super, sizeof(struct envfs_super)); if ( ret < sizeof(struct envfs_super)) { perror("read"); ret = -errno; goto out; } if ( ENVFS_32(super.magic) != ENVFS_MAGIC) { printf("envfs: wrong magic on %s\n", filename); ret = -EIO; goto out; } if (crc32(0, (unsigned char *)&super, sizeof(struct envfs_super) - 4) != ENVFS_32(super.sb_crc)) { printf("wrong crc on env superblock\n"); ret = -EIO; goto out; } size = ENVFS_32(super.size); buf = xmalloc(size); buf_free = buf; ret = read(envfd, buf, size); if (ret < size) { perror("read"); ret = -errno; goto out; } if (crc32(0, (unsigned char *)buf, size) != ENVFS_32(super.crc)) { printf("wrong crc on env\n"); ret = -EIO; goto out; } if (super.major < ENVFS_MAJOR) printf("envfs version %d.%d loaded into %d.%d\n", super.major, super.minor, ENVFS_MAJOR, ENVFS_MINOR); while (size) { struct envfs_inode *inode; struct envfs_inode_end *inode_end; uint32_t inode_size, inode_headerlen, namelen; inode = (struct envfs_inode *)buf; buf += sizeof(struct envfs_inode); if (ENVFS_32(inode->magic) != ENVFS_INODE_MAGIC) { printf("envfs: wrong magic on %s\n", filename); ret = -EIO; goto out; } inode_size = ENVFS_32(inode->size); inode_headerlen = ENVFS_32(inode->headerlen); namelen = strlen(inode->data) + 1; if (super.major < 1) inode_end = &inode_end_dummy; else inode_end = (struct envfs_inode_end *)(buf + PAD4(namelen)); debug("loading %s size %d namelen %d headerlen %d\n", inode->data, inode_size, namelen, inode_headerlen); str = concat_path_file(dir, inode->data); tmp = strdup(str); make_directory(dirname(tmp)); free(tmp); headerlen_full = PAD4(inode_headerlen); buf += headerlen_full; if (ENVFS_32(inode_end->magic) != ENVFS_INODE_END_MAGIC) { printf("envfs: wrong inode_end_magic on %s\n", filename); ret = -EIO; goto out; } if (S_ISLNK(ENVFS_32(inode_end->mode))) { debug("symlink: %s -> %s\n", str, (char*)buf); if (symlink((char*)buf, str) < 0) { printf("symlink: %s -> %s :", str, (char*)buf); perror(""); } free(str); } else { fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644); free(str); if (fd < 0) { printf("Open %s\n", errno_str()); ret = fd; goto out; } ret = write(fd, buf, inode_size); if (ret < inode_size) { perror("write"); ret = -errno; close(fd); goto out; } close(fd); } buf += PAD4(inode_size); size -= headerlen_full + PAD4(inode_size) + sizeof(struct envfs_inode); } ret = 0; out: close(envfd); if (buf_free) free(buf_free); return ret; }
int coreconnector_start(struct core_connector *co, void *param) { int err; if (co->socket) { assert(0); return 0; } err = start_connet(UB2LOGIC(co)->logic_id, co->ip, co->port, NULL, &co->socket); fprintf(stdout, "|start_connet|%d|%s:%d|%d\n", UB2LOGIC(co)->logic_id, co->ip, co->port, co->socket); if (!err && socket > 0) { return 0; } fprintf(stderr, "|start_connet|%d failure|%s:%d|%d %s\n", UB2LOGIC(co)->logic_id, co->ip, co->port, co->socket, errno_str(err)); return err; }
/* * AM35xx, AM33xx chips use big endian MLO for SPI NOR flash * This handler converting MLO to big endian and write to SPI NOR */ static int spi_nor_mlo_handler(struct bbu_handler *handler, struct bbu_data *data) { int dstfd = 0; int ret = 0; uint32_t readbuf; int size = data->len; void *image = data->image; uint32_t *header; int swap = 0; struct stat s; header = data->image; if (header[5] == 0x43485345 || header[10] == 0x62617265) { swap = 0; } else if (header[5] == 0x45534843 || header[10] == 0x65726142) { swap = 1; } else { if (!bbu_force(data, "Not a MLO image")) return -EINVAL; } ret = stat(data->devicefile, &s); if (ret) { printf("could not open %s: %s", data->devicefile, errno_str()); return ret; } if (size > s.st_size) { printf("Image too big, need %d, have %lld\n", size, s.st_size); return -ENOSPC; } ret = bbu_confirm(data); if (ret != 0) return ret; dstfd = open(data->devicefile, O_WRONLY); if (dstfd < 0) { printf("could not open %s: %s", data->devicefile, errno_str()); ret = dstfd; goto out; } ret = erase(dstfd, ERASE_SIZE_ALL, 0); if (ret < 0) { printf("could not erase %s: %s", data->devicefile, errno_str()); goto out1; } for (; size >= 0; size -= 4) { memcpy((char *)&readbuf, image, 4); if (swap) readbuf = cpu_to_be32(readbuf); ret = write(dstfd, &readbuf, 4); if (ret < 0) { perror("write"); goto out1; } image = image + 4; } ret = 0; out1: close(dstfd); out: return ret; }
void FILEWrapper::write(const void* src, size_t nbytes) { if (fwrite(src, 1, nbytes, _fp) != nbytes) { throw_exception("Failed to write " + std::to_string(nbytes) + " bytes: " + errno_str()); } }
void FILEWrapper::read_or_die(void* dest, size_t nbytes) { if (fread(dest, 1, nbytes, _fp) != nbytes) { throw_exception("Failed to read " + std::to_string(nbytes) + " bytes: " + errno_str()); } }
static int do_saveenv(int argc, char *argv[]) { int ret, fd; char *filename, *dirname; printf("saving environment\n"); if (argc < 3) dirname = "/env"; else dirname = argv[2]; if (argc < 2) filename = default_environment_path_get(); else filename = argv[1]; fd = open(filename, O_WRONLY | O_CREAT); if (fd < 0) { printf("could not open %s: %s\n", filename, errno_str()); return 1; } ret = protect(fd, ~0, 0, 0); /* ENOSYS is no error here, many devices do not need it */ if (ret && errno != ENOSYS) { printf("could not unprotect %s: %s\n", filename, errno_str()); close(fd); return 1; } ret = erase(fd, ~0, 0); /* ENOSYS is no error here, many devices do not need it */ if (ret && errno != ENOSYS) { printf("could not erase %s: %s\n", filename, errno_str()); close(fd); return 1; } close(fd); ret = envfs_save(filename, dirname); if (ret) { printf("saveenv failed\n"); goto out; } fd = open(filename, O_WRONLY | O_CREAT); ret = protect(fd, ~0, 0, 1); /* ENOSYS is no error here, many devices do not need it */ if (ret && errno != ENOSYS) { printf("could not protect %s: %s\n", filename, errno_str()); close(fd); return 1; } ret = 0; out: close(fd); return ret; }
/** * copy_file - Copy a file * @src: The source filename * @dst: The destination filename * @verbose: if true, show a progression bar * * Return: 0 for success or negative error code */ int copy_file(const char *src, const char *dst, int verbose) { char *rw_buf = NULL; int srcfd = 0, dstfd = 0; int r, w; int ret = 1, err1 = 0; int mode; void *buf; int total = 0; struct stat srcstat, dststat; rw_buf = xmalloc(RW_BUF_SIZE); srcfd = open(src, O_RDONLY); if (srcfd < 0) { printf("could not open %s: %s\n", src, errno_str()); goto out; } mode = O_WRONLY | O_CREAT; ret = stat(dst, &dststat); if (ret && ret != -ENOENT) goto out; /* Set O_TRUNC only if file exist and is a regular file */ if (!ret && S_ISREG(dststat.st_mode)) mode |= O_TRUNC; dstfd = open(dst, mode); if (dstfd < 0) { printf("could not open %s: %s\n", dst, errno_str()); goto out; } if (verbose) { if (stat(src, &srcstat) < 0) srcstat.st_size = 0; init_progression_bar(srcstat.st_size); } while (1) { r = read(srcfd, rw_buf, RW_BUF_SIZE); if (r < 0) { perror("read"); goto out; } if (!r) break; buf = rw_buf; while (r) { w = write(dstfd, buf, r); if (w < 0) { perror("write"); goto out; } buf += w; r -= w; total += w; } if (verbose) { if (srcstat.st_size && srcstat.st_size != FILESIZE_MAX) show_progress(total); else show_progress(total / 16384); } } ret = 0; out: if (verbose) putchar('\n'); free(rw_buf); if (srcfd > 0) close(srcfd); if (dstfd > 0) err1 = close(dstfd); return ret ?: err1; }
/** * @param[in] src FIXME * @param[out] dst FIXME * @param[in] verbose FIXME */ int copy_file(const char *src, const char *dst, int verbose) { char *rw_buf = NULL; int srcfd = 0, dstfd = 0; int r, w; int ret = 1; void *buf; int total = 0; struct stat statbuf; rw_buf = xmalloc(RW_BUF_SIZE); srcfd = open(src, O_RDONLY); if (srcfd < 0) { printf("could not open %s: %s\n", src, errno_str()); goto out; } dstfd = open(dst, O_WRONLY | O_CREAT); if (dstfd < 0) { printf("could not open %s: %s\n", dst, errno_str()); goto out; } if (verbose) { if (stat(src, &statbuf) < 0) statbuf.st_size = 0; init_progression_bar(statbuf.st_size); } while(1) { r = read(srcfd, rw_buf, RW_BUF_SIZE); if (r < 0) { perror("read"); goto out; } if (!r) break; buf = rw_buf; while (r) { w = write(dstfd, buf, r); if (w < 0) { perror("write"); goto out; } buf += w; r -= w; total += w; } if (verbose) show_progress(statbuf.st_size ? total : total / 16384); } ret = 0; out: if (verbose) putchar('\n'); free(rw_buf); if (srcfd > 0) close(srcfd); if (dstfd > 0) close(dstfd); return ret; }
/* * open a uimage. This will check the header contents and * return a handle to the uImage */ struct uimage_handle *uimage_open(const char *filename) { int fd; uint32_t checksum; struct uimage_handle *handle; struct image_header *header; int i; int ret; struct stat s; again: fd = open(filename, O_RDONLY); if (fd < 0) { printf("could not open: %s\n", errno_str()); return NULL; } /* * Hack around tftp fs. We need lseek for uImage support, but * this cannot be implemented in tftp fs, so we detect this * by doing a test lseek and copy the file to ram if it fails */ if (IS_BUILTIN(CONFIG_FS_TFTP) && lseek(fd, 0, SEEK_SET)) { close(fd); ret = copy_file(filename, uimage_tmp, 0); if (ret) return NULL; filename = uimage_tmp; goto again; } handle = xzalloc(sizeof(struct uimage_handle)); header = &handle->header; if (read(fd, header, sizeof(*header)) < 0) { printf("could not read: %s\n", errno_str()); goto err_out; } if (uimage_to_cpu(header->ih_magic) != IH_MAGIC) { printf("Bad Magic Number\n"); goto err_out; } checksum = uimage_to_cpu(header->ih_hcrc); header->ih_hcrc = 0; if (crc32(0, header, sizeof(*header)) != checksum) { printf("Bad Header Checksum\n"); goto err_out; } /* convert header to cpu native endianess */ header->ih_magic = uimage_to_cpu(header->ih_magic); header->ih_hcrc = uimage_to_cpu(header->ih_hcrc); header->ih_time = uimage_to_cpu(header->ih_time); header->ih_size = uimage_to_cpu(header->ih_size); header->ih_load = uimage_to_cpu(header->ih_load); header->ih_ep = uimage_to_cpu(header->ih_ep); header->ih_dcrc = uimage_to_cpu(header->ih_dcrc); if (header->ih_name[0]) { handle->name = xzalloc(IH_NMLEN + 1); strncpy(handle->name, header->ih_name, IH_NMLEN); } else { handle->name = xstrdup(filename); } if (uimage_is_multi_image(handle)) { size_t offset; for (i = 0; i < MAX_MULTI_IMAGE_COUNT; i++) { u32 size; ret = read(fd, &size, sizeof(size)); if (ret < 0) goto err_out; if (!size) break; handle->ihd[i].len = uimage_to_cpu(size); } handle->nb_data_entries = i; /* offset of the first image in a multifile image */ offset = 0; for (i = 0; i < handle->nb_data_entries; i++) { handle->ihd[i].offset = offset; offset += (handle->ihd[i].len + 3) & ~3; } handle->data_offset = sizeof(struct image_header) + sizeof(u32) * (handle->nb_data_entries + 1); } else { handle->ihd[0].offset = 0; handle->ihd[0].len = header->ih_size; handle->nb_data_entries = 1; handle->data_offset = sizeof(struct image_header); } /* * fd is now at the first data word */ handle->fd = fd; return handle; err_out: close(fd); free(handle); if (IS_BUILTIN(CONFIG_FS_TFTP) && !stat(uimage_tmp, &s)) unlink(uimage_tmp); return NULL; }
static int envfs_load_data(struct envfs_super *super, void *buf, size_t size, const char *dir, unsigned flags) { int fd, ret = 0; char *str, *tmp; int headerlen_full; /* for envfs < 1.0 */ struct envfs_inode_end inode_end_dummy; inode_end_dummy.mode = ENVFS_32(S_IRWXU | S_IRWXG | S_IRWXO); inode_end_dummy.magic = ENVFS_32(ENVFS_INODE_END_MAGIC); while (size) { struct envfs_inode *inode; struct envfs_inode_end *inode_end; uint32_t inode_size, inode_headerlen, namelen; inode = buf; buf += sizeof(struct envfs_inode); if (ENVFS_32(inode->magic) != ENVFS_INODE_MAGIC) { printf("envfs: wrong magic\n"); ret = -EIO; goto out; } inode_size = ENVFS_32(inode->size); inode_headerlen = ENVFS_32(inode->headerlen); namelen = strlen(inode->data) + 1; if (super->major < 1) inode_end = &inode_end_dummy; else inode_end = buf + PAD4(namelen); debug("loading %s size %d namelen %d headerlen %d\n", inode->data, inode_size, namelen, inode_headerlen); str = concat_path_file(dir, inode->data); headerlen_full = PAD4(inode_headerlen); buf += headerlen_full; if (ENVFS_32(inode_end->magic) != ENVFS_INODE_END_MAGIC) { printf("envfs: wrong inode_end_magic\n"); ret = -EIO; goto out; } tmp = strdup(str); make_directory(dirname(tmp)); free(tmp); if (S_ISLNK(ENVFS_32(inode_end->mode))) { debug("symlink: %s -> %s\n", str, (char*)buf); if (!strcmp(buf, basename(str))) { unlink(str); } else { ret = symlink(buf, str); if (ret < 0) printf("symlink: %s -> %s : %s\n", str, (char*)buf, strerror(-errno)); } free(str); } else { struct stat s; if (flags & ENV_FLAG_NO_OVERWRITE && !stat(str, &s)) { printf("skip %s\n", str); goto skip; } fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644); free(str); if (fd < 0) { printf("Open %s\n", errno_str()); ret = fd; goto out; } ret = write(fd, buf, inode_size); if (ret < inode_size) { perror("write"); ret = -errno; close(fd); goto out; } close(fd); } skip: buf += PAD4(inode_size); size -= headerlen_full + PAD4(inode_size) + sizeof(struct envfs_inode); } recursive_action(dir, ACTION_RECURSE | ACTION_DEPTHFIRST, NULL, dir_remove_action, NULL, 0); ret = 0; out: return ret; }
static int do_at91_boot_test(int argc, char *argv[]) { int opt; u32 *buf32; void *buf; void (*jump)(void) = NULL; int fd; int ret = 1; char *sram = "/dev/sram0"; u32 read_size, write_size; u32 tmp = 0; while ((opt = getopt(argc, argv, "j:s:")) > 0) { switch (opt) { case 'j': jump = (void*)simple_strtoul(optarg, NULL, 0); break; case 's': sram = optarg; break; default: return COMMAND_ERROR_USAGE; } } if (argc < optind + 1) return COMMAND_ERROR_USAGE; buf32 = buf = read_file(argv[optind], &read_size); if (!buf) return -EINVAL; write_size = buf32[5]; printf("size of the size %d\n", read_size); printf("size to load in sram %d\n", write_size); if (write_size > read_size) { printf("file smaller than requested sram loading size (%d < %d)\n", write_size, read_size); goto err; } fd = open(sram, O_WRONLY); if (fd < 0) { printf("could not open %s: %s\n", sram, errno_str()); ret = fd; goto err; } while (write_size) { tmp = write(fd, buf, write_size); if (tmp < 0) { perror("write"); goto err_open; } buf += tmp; write_size -= tmp; } shutdown_barebox(); jump(); err_open: close(fd); err: free(buf); return ret; }
/** * Restore the last environment into the current one * @param[in] filename from where to restore * @param[in] dir where to store the last content * @return 0 on success, anything else in case of failure * * Note: This function will also be used on the host! See note in the header * of this file. */ int envfs_load(const char *filename, const char *dir, unsigned flags) { struct envfs_super super; void *buf = NULL, *rbuf; int envfd; int ret = 0; size_t size, rsize; if (!filename) filename = default_environment_path_get(); if (!dir) dir = "/env"; envfd = open(filename, O_RDONLY); if (envfd < 0) { printf("environment load %s: %s\n", filename, errno_str()); if (errno == ENOENT) printf("Maybe you have to create the partition.\n"); return -1; } /* read superblock */ ret = read(envfd, &super, sizeof(struct envfs_super)); if ( ret < sizeof(struct envfs_super)) { perror("read"); ret = -errno; goto out; } ret = envfs_check_super(&super, &size); if (ret) goto out; if (super.flags & ENVFS_FLAGS_FORCE_BUILT_IN) { printf("found force-builtin environment, using defaultenv\n"); ret = defaultenv_load(dir, 0); if (ret) printf("failed to load default environment: %s\n", strerror(-ret)); goto out; } buf = xmalloc(size); rbuf = buf; rsize = size; while (rsize) { ssize_t now; now = read(envfd, rbuf, rsize); if (now < 0) { perror("read"); ret = -errno; goto out; } if (!now) { printf("%s: premature end of file\n", filename); ret = -EINVAL; goto out; } rbuf += now; rsize -= now; } ret = envfs_check_data(&super, buf, size); if (ret) goto out; ret = envfs_load_data(&super, buf, size, dir, flags); if (ret) goto out; ret = 0; out: close(envfd); free(buf); return ret; }