Ejemplo n.º 1
0
int bchdev_register(FAR const char *blkdev, FAR const char *chardev,
                    bool readonly)
{
  FAR void *handle;
  int ret;

  /* Setup the BCH lib functions */

  ret = bchlib_setup(blkdev, readonly, &handle);
  if (ret < 0)
    {
      fdbg("bchlib_setup failed: %d\n", -ret);
      return ret;
    }

  /* Then setup the character device */

  ret = register_driver(chardev, &bch_fops, 0666, handle);
  if (ret < 0)
    {
      fdbg("register_driver failed: %d\n", -ret);
      bchlib_teardown(handle);
      handle = NULL;
    }

  return ret;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
static inline int dd_infopen(const char *name, struct dd_s *dd)
{
  FAR struct nsh_vtbl_s *vtbl = dd->vtbl;
  int ret;
  int type;

  /* Get the type of the input file */

  type = dd_filetype(name);
  if (type < 0)
    {
      nsh_output(vtbl, g_fmtcmdfailed, g_dd, "stat", NSH_ERRNO_OF(-type));
      return type;
    }

  /* Open the input file */

  if (!type)
    {
      DD_INFD = open(name, O_RDONLY);
      if (DD_INFD < 0)
        {
          nsh_output(vtbl, g_fmtcmdfailed, g_dd, "open", NSH_ERRNO);
          return ERROR;
        }

      dd->infread  = dd_readch;  /* Character oriented read */
      dd->infclose = dd_infclosech;
    }
  else
    {
      ret = bchlib_setup(name, true, &DD_INHANDLE);
      if (ret < 0)
        {
          return ERROR;
        }

      dd->infread  = dd_readblk;
      dd->infclose = dd_infcloseblk;
    }

  return OK;
}
Ejemplo n.º 4
0
static inline int dd_outfopen(const char *name, struct dd_s *dd)
{
  int type;
  int ret = OK;

  /* Get the type of the output file */

  type = dd_filetype(name);

  /* Open the block driver for input */

  if (type == true)
    {
      ret = bchlib_setup(name, true, &DD_OUTHANDLE);
      if (ret < 0)
        {
          return ERROR;
        }

      dd->outfwrite = dd_writeblk;  /* Block oriented write */
      dd->outfclose = dd_outfcloseblk;
    }

  /* Otherwise, the file is character oriented or does not exist */

  else
    {
      DD_OUTFD = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
      if (DD_OUTFD < 0)
        {
          FAR struct nsh_vtbl_s *vtbl = dd->vtbl;
          nsh_output(vtbl, g_fmtcmdfailed, g_dd, "open", NSH_ERRNO);
          return ERROR;
        }

      dd->outfwrite = dd_writech;  /* Character oriented write */
      dd->outfclose = dd_outfclosech;
    }

  return OK;
}
Ejemplo n.º 5
0
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;
}