static int dd_writeblk(struct dd_s *dd) { ssize_t nbytes; off_t offset = (dd->sector - dd->skip) * dd->sectsize; /* Write the sector at the specified offset */ nbytes = bchlib_write(DD_OUTHANDLE, (char*)dd->buffer, offset, dd->sectsize); if (nbytes < 0) { /* bchlib_write return -EFBIG on attempts to write past the end of * the device. */ if (nbytes == -EFBIG) { dd->eof = true; /* Set end-of-file */ } else { FAR struct nsh_vtbl_s *vtbl = dd->vtbl; nsh_output(vtbl, g_fmtcmdfailed, g_dd, "bshlib_write", NSH_ERRNO_OF(-nbytes)); return ERROR; } } return OK; }
static int blk_write(const void *buf, int len, const char *path, int offset) { void *handle; int ret; ret = bchlib_setup(path, true, &handle); if (ret) { return ret; } ret = bchlib_write(handle, buf, offset, len); bchlib_teardown(handle); return ret; }
static ssize_t bch_write(FAR struct file *filep, FAR const char *buffer, size_t len) { FAR struct inode *inode = filep->f_inode; FAR struct bchlib_s *bch; int ret = -EACCES; DEBUGASSERT(inode && inode->i_private); bch = (FAR struct bchlib_s *)inode->i_private; if (!bch->readonly) { bchlib_semtake(bch); ret = bchlib_write(bch, buffer, filep->f_pos, len); if (ret > 0) { filep->f_pos += len; } bchlib_semgive(bch); } return ret; }
static int install_recovery(const char *srcpath) { int rfd, i, len, rem; int ret = 0; void *handle = NULL; if (bchlib_setup(CONFIG_MTD_RECOVERY_DEVPATH, false, &handle)) { return -1; } rfd = open(srcpath, O_RDONLY, 0444); if (read(rfd, &upg_image, sizeof(upg_image)) != sizeof(upg_image)) { _info("read head"); ret = -EIO; goto err; } #ifdef IMG_SIGNATURE if (upg_image.sig != IMG_SIGNATURE) { _info("image signature missmatch. IPL2=%u, UPG=%u\n", IMG_SIGNATURE, upg_image.sig); _info("go normal boot\n"); memset(copybuf, 0, sizeof(copybuf)); snprintf(copybuf, sizeof(copybuf), "normal"); set_config(1, copybuf); sysreset(); /* NOT REACHED */ } #endif for (i = 0; i < upg_image.chunknum; i++) { if (!strcmp(basename(upg_image.chunk[i].fname), "recovery")) { break; } } if (i == upg_image.chunknum) { _info("recovery not found\n"); ret = -ENOENT; goto err; } lseek(rfd, upg_image.chunk[i].offset + ((void *)&upg_image.chunk[upg_image.chunknum] - (void *)&upg_image), SEEK_SET); rem = upg_image.chunk[i].size; while (rem > 0) { len = read(rfd, copybuf, rem > 512 ? 512 : rem); if (len < 0) { _info("read image"); ret = -EIO; goto err; } bchlib_write(handle, copybuf, upg_image.chunk[i].size - rem, len); rem -= len; } err: if (handle) { bchlib_teardown(handle); } close(rfd); _info("DONE\n"); return ret; }