⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fsinit.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    goto FlashCheckSectTbl_exit;
  }

  /*-------------------------------------------------------------------*/
  /* Check blocks num used against total used.                         */
  /*-------------------------------------------------------------------*/
  for (num_used = 0, i = 0; i < vol->num_blocks; ++i)
    if (!vol->blocks[i].ctrl_block)
      num_used += vol->blocks[i].used_sects;
  if (num_used != vol->used_sects)
  {
    printf("Vol total blocks used = %u, actual used = %u\n",
           vol->used_sects, num_used);
    goto FlashCheckSectTbl_exit;
  }

  /*-------------------------------------------------------------------*/
  /* Check the control sectors.                                        */
  /*-------------------------------------------------------------------*/
  for (num_used = 0, i = vol->frst_ctrl_sect; i != FLAST_SECT;
       i = vol->sect_tbl[i].next, ++num_used)
  {
    /*-----------------------------------------------------------------*/
    /* Check sector is used.                                           */
    /*-----------------------------------------------------------------*/
    if (!used_sect(i))
    {
      printf("Contorl sector = %u not used\n", i);
      goto FlashCheckSectTbl_exit;
    }

    /*-----------------------------------------------------------------*/
    /* Check for loops in sector chain.                                */
    /*-----------------------------------------------------------------*/
    if (bitmap1[i / 8] & (1 << (i % 8)))
    {
      printf("Contro info has loop in sector chain\n");
      goto FlashCheckSectTbl_exit;
    }

    /*-----------------------------------------------------------------*/
    /* Check sector does not belong to a different file.               */
    /*-----------------------------------------------------------------*/
    if (bitmap2[i / 8] & (1 << (i % 8)))
    {
      printf("File = %s has loop file data in sector chain\n");
      goto FlashCheckSectTbl_exit;
    }

    /*-----------------------------------------------------------------*/
    /* Mark bitmap.                                                    */
    /*-----------------------------------------------------------------*/
    bitmap1[i / 8] |= (1 << (i % 8));

    /*-----------------------------------------------------------------*/
    /* Check next and prev pointers.                                   */
    /*-----------------------------------------------------------------*/
    if (vol->sect_tbl[i].prev == FLAST_SECT)
    {
      if (i != vol->frst_ctrl_sect)
      {
        printf("Control info has inconsistent first sector\n");
        goto FlashCheckSectTbl_exit;
      }
    }
    else
    {
      if (vol->sect_tbl[vol->sect_tbl[i].prev].next != i)
      {
        printf("Control info inconsistent chain:\n");
        printf("   * sect[%u].prev = %u, sect[%u].next = %u\n",
               i, vol->sect_tbl[i].prev, vol->sect_tbl[i].prev,
               vol->sect_tbl[vol->sect_tbl[i].prev].next);
        goto FlashCheckSectTbl_exit;
      }
    }
    if (vol->sect_tbl[i].next == FLAST_SECT)
    {
      if (i != vol->last_ctrl_sect)
      {
        printf("Control info has inconsistent last sector\n");
        goto FlashCheckSectTbl_exit;
      }
    }
    else
    {
      if (vol->sect_tbl[vol->sect_tbl[i].next].prev != i)
      {
        printf("Control info inconsistent chain:\n");
        printf("   * sect[%u].next = %u, sect[%u].prev = %u\n",
               i, vol->sect_tbl[i].next, vol->sect_tbl[i].next,
               vol->sect_tbl[vol->sect_tbl[i].next].prev);
        goto FlashCheckSectTbl_exit;
      }
    }
  }

  /*------------------------------------------------------------------*/
  /* Check total number of control sectors.                           */
  /*------------------------------------------------------------------*/
  if (num_used != vol->ctrl_sects)
  {
    printf("Vol total ctrl sects thought = %u, actual = %u\n",
           vol->ctrl_sects, num_used);
  }
  memset(bitmap2, 0, bitmap_sz);

  /*-------------------------------------------------------------------*/
  /* Check the free sectors.                                           */
  /*-------------------------------------------------------------------*/
  for (j = vol->free_sect, size = 0;; j = vol->sect_tbl[j].next, ++size)
  {
    /*-----------------------------------------------------------------*/
    /* Check sector is free.                                           */
    /*-----------------------------------------------------------------*/
    if (vol->sect_tbl[j].prev != FFREE_SECT)
    {
      printf("Sector = %u in free sectors not free!\n", j);
      goto FlashCheckSectTbl_exit;
    }

    /*-----------------------------------------------------------------*/
    /* Check for loops in free list.                                   */
    /*-----------------------------------------------------------------*/
    if (bitmap2[j / 8] & (1 << (j % 8)))
    {
      printf("Loop in free sectors list!\n");
      goto FlashCheckSectTbl_exit;
    }
    bitmap2[j / 8] |= (1 << (j % 8));

    /*-----------------------------------------------------------------*/
    /* Check last sector.                                              */
    /*-----------------------------------------------------------------*/
    if (vol->sect_tbl[j].next == FLAST_SECT)
    {
      if (j != vol->last_free_sect)
      {
        printf("Free list thought last = %u, actual last = %u\n",
               vol->last_free_sect, j);
        goto FlashCheckSectTbl_exit;
      }
      ++size;
      break;
    }
  }

  /*-------------------------------------------------------------------*/
  /* Successfully passed check.                                        */
  /*-------------------------------------------------------------------*/
  r_val = 0;

FlashCheckSectTbl_exit:
  free(bitmap1);
  free(bitmap2);
  return r_val;
}
#endif /* FFS_DEBUG */

/***********************************************************************/
/* FlashGetTbl: Create a new table to be added to the list of tables   */
/*                                                                     */
/*     Returns: pointer to the newly created table                     */
/*                                                                     */
/***********************************************************************/
FFSEnts *FlashGetTbl(void)
{
  FFSEnts *r_value = calloc(1, sizeof(FFSEnts));
  int i;

  if (r_value)
  {
    r_value->next_tbl = r_value->prev_tbl = NULL;
    r_value->free = FNUM_ENT - 1;

    /*-----------------------------------------------------------------*/
    /* Empty out all the entries in the table.                         */
    /*-----------------------------------------------------------------*/
    for (i = 0; i < FNUM_ENT; ++i)
      r_value->tbl[i].type = FEMPTY;

    /*-----------------------------------------------------------------*/
    /* Add size of table entries to the total entries.                 */
    /*-----------------------------------------------------------------*/
    Flash->total += FNUM_ENT;
  }
  else
    set_errno(ENOMEM);

  return r_value;
}

/***********************************************************************/
/*  FlashClean: Erase a flash volume                                   */
/*                                                                     */
/*     Returns: 0 on success, -1 on failure                            */
/*                                                                     */
/***********************************************************************/
int FlashClean(void)
{
  ui32 block, hdr, i, j, sect, addr = Flash->mem_base;
  int rc;
  FlashGlob *vol;

  /*-------------------------------------------------------------------*/
  /* Loop over every block in the device.                              */
  /*-------------------------------------------------------------------*/
  block = 0;
  for (; block < Flash->num_blocks; ++block, addr += Flash->block_size)
  {
    /*-----------------------------------------------------------------*/
    /* Skip control blocks and bad blocks.                             */
    /*-----------------------------------------------------------------*/
    if (Flash->blocks[block].ctrl_block ||
        (Flash->type == FFS_NAND && Flash->blocks[block].bad_block))
      continue;

    /*-----------------------------------------------------------------*/
    /* Loop over every sector in this block.                           */
    /*-----------------------------------------------------------------*/
    sect = block * Flash->block_sects;
    for (i = 0;; ++i)
    {
      /*---------------------------------------------------------------*/
      /* Check if every sector has been verified empty.                */
      /*---------------------------------------------------------------*/
      if (i == Flash->block_sects)
      {
        /*-------------------------------------------------------------*/
        /* If NOR device, write the signature bytes.                   */
        /*-------------------------------------------------------------*/
        if (Flash->type == FFS_NOR)
        {
          hdr = addr + Flash->hdr_sects * Flash->sect_sz - RES_BYTES;

          /*-----------------------------------------------------------*/
          /* Release exclusive access to the flash file system.        */
          /*-----------------------------------------------------------*/
          vol = Flash;
          semPost(FlashSem);

          /*-----------------------------------------------------------*/
          /* Call flash driver write_byte() routine.                   */
          /*-----------------------------------------------------------*/
          rc = vol->driver.nor.write_byte(hdr++, NOR_BYTE1, vol->vol) ||
               vol->driver.nor.write_byte(hdr++, NOR_BYTE2, vol->vol) ||
               vol->driver.nor.write_byte(hdr++, NOR_BYTE3, vol->vol) ||
               vol->driver.nor.write_byte(hdr,   NOR_BYTE4, vol->vol);

          /*-----------------------------------------------------------*/
          /* Acquire exclusive access to the flash file system.        */
          /*-----------------------------------------------------------*/
          semPend(FlashSem, WAIT_FOREVER);
          Flash = vol;

          /*-----------------------------------------------------------*/
          /* Return error if any write_byte() failed.                  */
          /*-----------------------------------------------------------*/
          if (rc)
          {
            set_errno(EIO);
            return -1;
          }
        }

        /*-------------------------------------------------------------*/
        /* Break to continue to next block.                            */
        /*-------------------------------------------------------------*/
        break;
      }

      /*---------------------------------------------------------------*/
      /* For NAND/MLC check that all sectors are free.                 */
      /*---------------------------------------------------------------*/
      if (Flash->type != FFS_NOR)
      {
        if (Flash->empty_sect(sect + i) == FALSE)
        {
          if (Flash->erase_block_wrapper(addr))
          {
            set_errno(EIO);
            return -1;
          }
          break;
        }
      }

      /*---------------------------------------------------------------*/
      /* For NOR, check non-header sectors are empty and header ones   */
      /* contain at most the signature bytes.                          */
      /*---------------------------------------------------------------*/
      else if (Flash->empty_sect(sect + i) == FALSE)
      {
        /*-------------------------------------------------------------*/
        /* If non-header sector not empty, erase block.                */
        /*-------------------------------------------------------------*/
        if (i != Flash->hdr_sects - 1)
        {
          if (Flash->erase_block_wrapper(addr))
          {
            set_errno(EIO);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -