[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

quick chkjffs2 program



Hi,

I wrote a small hack to check whether there's an ext2 filesystem
on the flash (so that I can remove it automatically at system
start)
Now I changed it so that it detects an old jffs2 too.
Add it to the util directory and add a new target in the Makefile

Hope you find it usefull too.

  -- Barney

--------------------------------------
++ axel (barney) barnitzke
++ it consultant
++ lisa systems


email :: mailto:barney@xxxxxxx.de

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <getopt.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/mount.h>

#include <linux/mtd/mtd.h>
#include <linux/jffs2.h>

int vflag = 0;

#define JFFS2_MAX_FILE_SIZE 0xFFFFFFFF

/** check headers on erase blocksize boundaries) **/
int check_headers(int fd, unsigned int erase_block_size, unsigned int size)
{
  struct jffs2_raw_inode ri;
  off_t offset;
  for(offset=0; offset < size; offset += erase_block_size) {
    if(lseek(fd, offset, SEEK_SET) != offset) {
      perror("can't seek");
      return -1;
    }
    if (read(fd, &ri, sizeof(ri)) != sizeof(ri)) {
      perror("read inode");
      return -1;
    }
    /** every block boundary has to be empty or a valid inode **/
    switch(ri.magic) {
    case JFFS2_MAGIC_BITMASK:
    case JFFS2_EMPTY_BITMASK:
    case KSAMTIB_CIGAM_2SFFJ:
    case JFFS2_DIRTY_BITMASK:
      if(vflag) {
        printf("inode at 0x%08lx is valid (0x%0x)\n", offset,ri.magic);
      }
      break;
    case JFFS2_OLD_MAGIC_BITMASK:
      fprintf(stderr, "old jffs2 inode found at: 0x%0lx (0x%0x)\n", offset,ri.magic);
      return -1;
      break;
    default:
      fprintf(stderr, "invalid inode found at: 0x%0lx (0x%0x)\n", offset,ri.magic);
      return -1;
      break;
    }
  }
  return 0;
}

int main(int argc,char *argv[])
{
  mtd_info_t meminfo;
  int Fd;
  unsigned long Length = 0;
  extern char *optarg;
  int c,opterr = 0;

  while ( (c = getopt(argc, argv, "v")) != EOF) {
    switch(c) {
      case 'v':
        vflag++;
        break;
      default:
	opterr++;
        break;
    }
  }
  
  if ((argc - optind) != 1 )
    {
      fprintf(stderr,"You must specify a device\n");
      return 16;
    }
   
  // Open and size the device
  if ((Fd = open(argv[optind],O_RDWR)) < 0)
    {
      fprintf(stderr,"File open error\n");
      return 8;
    }

  if (ioctl(Fd,MEMGETINFO,&meminfo) == 0)
    {
      /**
      Length = lseek(Fd,0,SEEK_END);
      lseek(Fd,0,SEEK_SET);
      **/
      Length = meminfo.size;

      if(vflag) {
        printf(" type: %0x\n", meminfo.type);
        printf(" flags: %0x\n", meminfo.flags);
        printf(" size: %0x\n", meminfo.size);
        printf(" erasesize: %0x\n", meminfo.erasesize);
        printf(" oobblock: %0x\n", meminfo.oobblock);
        printf(" oobsize: %0x\n", meminfo.oobsize);
        printf(" ecctype: %0x\n", meminfo.ecctype);
        printf(" eccsize: %0x\n", meminfo.eccsize);
      }
    } else {
      fprintf(stderr, "can't ioctl device\n");
      return 15;
    }

  return check_headers(Fd, meminfo.erasesize, Length) ;
}