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

📄 fsinit.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
  for (i = 0; i < Flash->max_set_blocks; ++i)
    Flash->erase_set[i] = -1;

  /*-------------------------------------------------------------------*/
  /* Unmark the flag marking that was necessary for next_free_block(). */
  /*-------------------------------------------------------------------*/
  for (block = 0; block < Flash->num_blocks; ++block)
    if (!Flash->blocks[block].bad_block)
      Flash->blocks[block].ctrl_block = FALSE;

  /*-------------------------------------------------------------------*/
  /* Save the state of the system.                                     */
  /*-------------------------------------------------------------------*/
  strcpy(Flash->files_tbl->tbl[0].entry.dir.name, Flash->sys.name);
  if (FlashSync(DO_SYNC) != NULL)
    return -1;

  /*-------------------------------------------------------------------*/
  /* Clean old ctrl blocks.                                            */
  /*-------------------------------------------------------------------*/
  if (old_1st_ctrl_block != -1)
  {
    PfAssert(!Flash->blocks[old_1st_ctrl_block].bad_block &&
             Flash->blocks[old_1st_ctrl_block].used_sects == 0);

    /*-----------------------------------------------------------------*/
    /* Erase 1st old control block.                                    */
    /*-----------------------------------------------------------------*/
    addr = Flash->mem_base + old_1st_ctrl_block * Flash->block_size;
    if (Flash->erase_block_wrapper(addr))
    {
      set_errno(EIO);
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Write 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.                       */
      /*---------------------------------------------------------------*/
      status = 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;
      if (status)
      {
        set_errno(EIO);
        return -1;
      }
    }
  }
  if (old_2nd_ctrl_block != -1)
  {
    PfAssert(!Flash->blocks[old_2nd_ctrl_block].bad_block &&
             Flash->blocks[old_2nd_ctrl_block].used_sects == 0);

    /*-----------------------------------------------------------------*/
    /* Erase 2nd old control block.                                    */
    /*-----------------------------------------------------------------*/
    addr = Flash->mem_base + old_2nd_ctrl_block * Flash->block_size;
    if (Flash->erase_block_wrapper(addr))
    {
      set_errno(EIO);
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Write 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.                       */
      /*---------------------------------------------------------------*/
      status = 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;
      if (status)
      {
        set_errno(EIO);
        return -1;
      }
    }
  }

  FlashChooseEraseSet(USE_WEAR);
  return 0;
}

#if FFS_DEBUG
/***********************************************************************/
/*   find_file: Find file name based on comm entry                     */
/*                                                                     */
/*      Inputs: vol = pointer to flash volume                          */
/*              comm = pointer to comm entry                           */
/*                                                                     */
/*     Returns: file name, "NO FILE" if no such file                   */
/*                                                                     */
/***********************************************************************/
static char *find_file(FlashGlob *vol, FFSEnt *comm)
{
  FFSEnts *tbl;
  FFSEnt *entry;
  ui32 i;

  for (tbl = vol->files_tbl; tbl; tbl = tbl->next_tbl)
  {
    entry = &tbl->tbl[0];
    for (i = 0; i < FNUM_ENT; ++i, ++entry)
      if (entry->type == FFILEN)
      {
        if ((void *)entry->entry.file.comm == comm)
          return entry->entry.file.name;
      }
  }
  return "NO FILE";
}
#endif

/***********************************************************************/
/* Global Function Definitions                                         */
/***********************************************************************/

#if FFS_DEBUG
/***********************************************************************/
/* FlashCheckSectTbl: Check the sectors table for consistency          */
/*                                                                     */
/*       Input: vol = pointer to file system volume                    */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
int FlashCheckSectTbl(FlashGlob *vol)
{
  ui32 i, j, size, bitmap_sz = (vol->num_sects + 7) / 8, num_used = 0;
  ui8 *bitmap1 = calloc(bitmap_sz, sizeof(ui8));
  ui8 *bitmap2 = calloc(bitmap_sz, sizeof(ui8));
  FFSEnts *tbl;
  FFSEnt *entry;
  int r_val = -1;

  if (bitmap1 == NULL || bitmap2 == NULL)
  {
    set_errno(ENOMEM);
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Check the used sectors for each file.                             */
  /*-------------------------------------------------------------------*/
  for (tbl = vol->files_tbl; tbl; tbl = tbl->next_tbl)
  {
    entry = &tbl->tbl[0];
    for (i = 0; i < FNUM_ENT; ++i, ++entry)
      if (entry->type == FCOMMN && entry->entry.comm.size)
      {
        size = 0;
        for (j = entry->entry.comm.frst_sect; j != FLAST_SECT;
             j = vol->sect_tbl[j].next)
        {
          /*-----------------------------------------------------------*/
          /* Check sector is used.                                     */
          /*-----------------------------------------------------------*/
          if (!used_sect(j))
          {
            printf("File = %s, sector = %u not used\n",
                   find_file(vol, entry), j);
            goto FlashCheckSectTbl_exit;
          }
          ++num_used;

          /*-----------------------------------------------------------*/
          /* Check for loops in sector chain.                          */
          /*-----------------------------------------------------------*/
          if (bitmap1[j / 8] & (1 << (j % 8)))
          {
            printf("File = %s has loop in sector chain\n",
                   find_file(vol, entry));
            goto FlashCheckSectTbl_exit;
          }

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

          /*-----------------------------------------------------------*/
          /* Mark bitmaps.                                             */
          /*-----------------------------------------------------------*/
          bitmap1[j / 8] |= (1 << (j % 8));
          bitmap2[j / 8] |= (1 << (j % 8));

          /*-----------------------------------------------------------*/
          /* Check next and prev pointers.                             */
          /*-----------------------------------------------------------*/
          if (vol->sect_tbl[j].prev == FLAST_SECT)
          {
            if (j != entry->entry.comm.frst_sect)
            {
              printf("File = %s has inconsistent first sector\n",
                     find_file(vol, entry));
              goto FlashCheckSectTbl_exit;
            }
          }
          else
          {
            if (vol->sect_tbl[vol->sect_tbl[j].prev].next != j)
            {
              printf("File = %s inconsistent chain:\n",
                     find_file(vol, entry));
              printf("   * sect[%u].prev = %u, sect[%u].next = %u\n",
                     j, vol->sect_tbl[j].prev, vol->sect_tbl[j].prev,
                     vol->sect_tbl[vol->sect_tbl[j].prev].next);
              goto FlashCheckSectTbl_exit;
            }
          }
          if (vol->sect_tbl[j].next == FLAST_SECT)
          {
            if (j != entry->entry.comm.last_sect)
            {
              printf("File = %s has inconsistent last sector\n",
                     find_file(vol, entry));
              goto FlashCheckSectTbl_exit;
            }
            if (entry->entry.comm.one_past_last.sector == 0xFFFF)
              size += vol->sect_sz;
            else
              size += entry->entry.comm.one_past_last.offset;
          }
          else
          {
            if (vol->sect_tbl[vol->sect_tbl[j].next].prev != j)
            {
              printf("File = %s inconsistent chain:\n",
                     find_file(vol, entry));
              printf("   * sect[%u].next = %u, sect[%u].prev = %u\n",
                     j, vol->sect_tbl[j].next, vol->sect_tbl[j].next,
                     vol->sect_tbl[vol->sect_tbl[j].next].prev);
              goto FlashCheckSectTbl_exit;
            }
            size += vol->sect_sz;
          }
        }
        memset(bitmap1, 0, bitmap_sz);

        /*-------------------------------------------------------------*/
        /* Check file size is correct.                                 */
        /*-------------------------------------------------------------*/
        if (size != entry->entry.comm.size)
        {
          printf("File = %s: thought size = %u, actual size = %u\n",
                 find_file(vol, entry), entry->entry.comm.size, size);
          goto FlashCheckSectTbl_exit;
        }
      }
  }

  /*-------------------------------------------------------------------*/
  /* Check total number of used sectors.                               */
  /*-------------------------------------------------------------------*/
  if (num_used != vol->used_sects)
  {
    printf("Vol total thought used = %u, actual used = %u\n",
           vol->used_sects, num_used);

⌨️ 快捷键说明

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