Index: cpukit/libblock/src/flashdisk.c
===================================================================
--- cpukit/libblock/src/flashdisk.c	(revision 3ab9c76d3f84c5d556d1cd67b88766cc4ad7da0b)
+++ cpukit/libblock/src/flashdisk.c	(revision 12806cd02f2b2ee20ea4b6828ce44b7af5f5916d)
@@ -831,14 +831,17 @@
  */
 static int
-rtems_fdisk_seg_read (const rtems_flashdisk* fd,
-                      uint32_t               device,
-                      uint32_t               segment,
-                      uint32_t               offset,
-                      void*                  buffer,
-                      uint32_t               size)
-{
+rtems_fdisk_seg_read (const rtems_flashdisk*         fd,
+                      const rtems_fdisk_segment_ctl* sc,
+                      uint32_t                       offset,
+                      void*                          buffer,
+                      uint32_t                       size)
+{
+  uint32_t                           device;
+  uint32_t                           segment;
   const rtems_fdisk_segment_desc*    sd;
   const rtems_fdisk_driver_handlers* ops;
-  sd  = rtems_fdisk_seg_descriptor (fd, device, segment);
+  device = sc->device;
+  segment = sc->segment;
+  sd = rtems_fdisk_seg_descriptor (fd, device, segment);
   ops = fd->devices[device].descriptor->flash_ops;
 #if RTEMS_FDISK_TRACE
@@ -855,14 +858,18 @@
  */
 static int
-rtems_fdisk_seg_write (const rtems_flashdisk* fd,
-                       uint32_t               device,
-                       uint32_t               segment,
-                       uint32_t               offset,
-                       const void*            buffer,
-                       uint32_t               size)
-{
+rtems_fdisk_seg_write (const rtems_flashdisk*   fd,
+                       rtems_fdisk_segment_ctl* sc,
+                       uint32_t                 offset,
+                       const void*              buffer,
+                       uint32_t                 size)
+{
+  int ret;
+  uint32_t                           device;
+  uint32_t                           segment;
   const rtems_fdisk_segment_desc*    sd;
   const rtems_fdisk_driver_handlers* ops;
-  sd  = rtems_fdisk_seg_descriptor (fd, device, segment);
+  device = sc->device;
+  segment = sc->segment;
+  sd = rtems_fdisk_seg_descriptor (fd, device, segment);
   ops = fd->devices[device].descriptor->flash_ops;
 #if RTEMS_FDISK_TRACE
@@ -870,5 +877,9 @@
                       device, segment, offset, size);
 #endif
-  return ops->write (sd, device, segment, offset, buffer, size);
+  ret = ops->write (sd, device, segment, offset, buffer, size);
+  if (ret)
+    sc->failed = true;
+
+  return ret;
 }
 
@@ -877,12 +888,15 @@
  */
 static int
-rtems_fdisk_seg_blank_check (const rtems_flashdisk* fd,
-                             uint32_t               device,
-                             uint32_t               segment,
-                             uint32_t               offset,
-                             uint32_t               size)
-{
+rtems_fdisk_seg_blank_check (const rtems_flashdisk*   fd,
+                             rtems_fdisk_segment_ctl* sc,
+                             uint32_t                 offset,
+                             uint32_t                 size)
+{
+  uint32_t                           device;
+  uint32_t                           segment;
   const rtems_fdisk_segment_desc*    sd;
   const rtems_fdisk_driver_handlers* ops;
+  device = sc->device;
+  segment = sc->segment;
   sd = rtems_fdisk_seg_descriptor (fd, device, segment);
   ops = fd->devices[device].descriptor->flash_ops;
@@ -919,10 +933,9 @@
  */
 static int
-rtems_fdisk_seg_blank_check_page (const rtems_flashdisk* fd,
-                                  uint32_t               device,
-                                  uint32_t               segment,
-                                  uint32_t               page)
-{
-  return rtems_fdisk_seg_blank_check (fd, device, segment,
+rtems_fdisk_seg_blank_check_page (const rtems_flashdisk*   fd,
+                                  rtems_fdisk_segment_ctl* sc,
+                                  uint32_t                 page)
+{
+  return rtems_fdisk_seg_blank_check (fd, sc,
                                       page * fd->block_size, fd->block_size);
 }
@@ -932,11 +945,10 @@
  */
 static int
-rtems_fdisk_seg_read_page (const rtems_flashdisk* fd,
-                           uint32_t               device,
-                           uint32_t               segment,
-                           uint32_t               page,
-                           void*                  buffer)
-{
-  return rtems_fdisk_seg_read (fd, device, segment,
+rtems_fdisk_seg_read_page (const rtems_flashdisk*   fd,
+                           rtems_fdisk_segment_ctl* sc,
+                           uint32_t                 page,
+                           void*                    buffer)
+{
+  return rtems_fdisk_seg_read (fd, sc,
                                page * fd->block_size, buffer, fd->block_size);
 }
@@ -946,17 +958,16 @@
  */
 static int
-rtems_fdisk_seg_write_page (const rtems_flashdisk* fd,
-                            uint32_t               device,
-                            uint32_t               segment,
-                            uint32_t               page,
-                            const void*            buffer)
+rtems_fdisk_seg_write_page (rtems_flashdisk*         fd,
+                            rtems_fdisk_segment_ctl* sc,
+                            uint32_t                 page,
+                            const void*              buffer)
 {
   if ((fd->flags & RTEMS_FDISK_BLANK_CHECK_BEFORE_WRITE))
   {
-    int ret = rtems_fdisk_seg_blank_check_page (fd, device, segment, page);
+    int ret = rtems_fdisk_seg_blank_check_page (fd, sc, page);
     if (ret)
       return ret;
   }
-  return rtems_fdisk_seg_write (fd, device, segment,
+  return rtems_fdisk_seg_write (fd, sc,
                                 page * fd->block_size, buffer, fd->block_size);
 }
@@ -980,23 +991,21 @@
  */
 static int
-rtems_fdisk_seg_copy_page (const rtems_flashdisk* fd,
-                           uint32_t               src_device,
-                           uint32_t               src_segment,
-                           uint32_t               src_page,
-                           uint32_t               dst_device,
-                           uint32_t               dst_segment,
-                           uint32_t               dst_page)
+rtems_fdisk_seg_copy_page (rtems_flashdisk*         fd,
+                           rtems_fdisk_segment_ctl* src_sc,
+                           uint32_t                 src_page,
+                           rtems_fdisk_segment_ctl* dst_sc,
+                           uint32_t                 dst_page)
 {
   int ret;
 #if RTEMS_FDISK_TRACE
   rtems_fdisk_printf (fd, "  seg-copy-page: %02d-%03d~%03d=>%02d-%03d~%03d",
-                      src_device, src_segment, src_page,
-                      dst_device, dst_segment, dst_page);
-#endif
-  ret = rtems_fdisk_seg_read_page (fd, src_device, src_segment, src_page,
+                      src_sc->device, src_sc->segment, src_page,
+                      dst_sc->device, dst_sc->segment, dst_page);
+#endif
+  ret = rtems_fdisk_seg_read_page (fd, src_sc, src_page,
                                    fd->copy_buffer);
   if (ret)
     return ret;
-  return rtems_fdisk_seg_write_page (fd, dst_device, dst_segment, dst_page,
+  return rtems_fdisk_seg_write_page (fd, dst_sc, dst_page,
                                      fd->copy_buffer);
 }
@@ -1008,6 +1017,5 @@
 static int
 rtems_fdisk_seg_write_page_desc (const rtems_flashdisk*       fd,
-                                 uint32_t                     device,
-                                 uint32_t                     segment,
+                                 rtems_fdisk_segment_ctl*     sc,
                                  uint32_t                     page,
                                  const rtems_fdisk_page_desc* page_desc)
@@ -1016,5 +1024,5 @@
   if ((fd->flags & RTEMS_FDISK_BLANK_CHECK_BEFORE_WRITE))
   {
-    int ret = rtems_fdisk_seg_blank_check (fd, device, segment,
+    int ret = rtems_fdisk_seg_blank_check (fd, sc,
                                            offset,
                                            sizeof (rtems_fdisk_page_desc));
@@ -1022,5 +1030,5 @@
       return ret;
   }
-  return rtems_fdisk_seg_write (fd, device, segment, offset,
+  return rtems_fdisk_seg_write (fd, sc, offset,
                                 page_desc, sizeof (rtems_fdisk_page_desc));
 }
@@ -1032,6 +1040,5 @@
 static int
 rtems_fdisk_seg_write_page_desc_flags (const rtems_flashdisk*       fd,
-                                       uint32_t                     device,
-                                       uint32_t                     segment,
+                                       rtems_fdisk_segment_ctl*     sc,
                                        uint32_t                     page,
                                        const rtems_fdisk_page_desc* page_desc)
@@ -1043,5 +1050,5 @@
     uint16_t flash_flags;
     int      ret;
-    ret = rtems_fdisk_seg_read (fd, device, segment, offset,
+    ret = rtems_fdisk_seg_read (fd, sc, offset,
                                 &flash_flags, sizeof (flash_flags));
     if (ret)
@@ -1049,91 +1056,83 @@
     if ((flash_flags & page_desc->flags) != page_desc->flags)
     {
-      rtems_fdisk_error ("  seg-write-page-flags: %02d-%03d-%03d: " \
+      rtems_fdisk_error ("  seg-write-page-flags: %02d-%03d-%03d: "
                          "flags not erased: 0x%04 -> 0x%04x",
-                         device, segment, page, flash_flags, page_desc->flags);
+                         sc->device, sc->segment, page,
+                         flash_flags, page_desc->flags);
       return ret;
     }
   }
-  return rtems_fdisk_seg_write (fd, device, segment, offset,
+  return rtems_fdisk_seg_write (fd, sc, offset,
                                 &page_desc->flags, sizeof (page_desc->flags));
 }
 
 /**
- * Erase a segment.
- */
-static int
-rtems_fdisk_seg_erase (const rtems_flashdisk* fd,
-                       uint32_t               device,
-                       uint32_t               segment)
-{
+ * Erase a device.
+ */
+static int
+rtems_fdisk_device_erase (const rtems_flashdisk* fd, uint32_t device)
+{
+  const rtems_fdisk_driver_handlers* ops;
+  ops = fd->devices[device].descriptor->flash_ops;
+#if RTEMS_FDISK_TRACE
+  rtems_fdisk_printf (fd, " device-erase: %02d", device);
+#endif
+  return ops->erase_device (fd->devices[device].descriptor, device);
+}
+
+/**
+ * Erase all flash.
+ */
+static int
+rtems_fdisk_erase_flash (const rtems_flashdisk* fd)
+{
+  uint32_t device;
+  for (device = 0; device < fd->device_count; device++)
+  {
+    int ret;
+
+#if RTEMS_FDISK_TRACE
+    rtems_fdisk_info (fd, " erase-flash:%02d", device);
+#endif
+
+    ret = rtems_fdisk_device_erase (fd, device);
+
+    if (ret != 0)
+      return ret;
+  }
+  return 0;
+}
+
+/**
+ * Calculate the checksum of a page in a segment.
+ */
+static uint16_t
+rtems_fdisk_page_checksum (const uint8_t* buffer, uint32_t page_size)
+{
+  uint16_t cs = 0xffff;
+  uint32_t i;
+
+  for (i = 0; i < page_size; i++, buffer++)
+    cs = rtems_fdisk_calc_crc16 (cs, *buffer);
+
+  return cs;
+}
+
+/**
+ * Erase the segment.
+ */
+static int
+rtems_fdisk_erase_segment (rtems_flashdisk* fd, rtems_fdisk_segment_ctl* sc)
+{
+  int                                ret;
+  uint32_t                           device;
+  uint32_t                           segment;
   const rtems_fdisk_segment_desc*    sd;
   const rtems_fdisk_driver_handlers* ops;
-  sd  = rtems_fdisk_seg_descriptor (fd, device, segment);
+  device = sc->device;
+  segment = sc->segment;
+  sd = rtems_fdisk_seg_descriptor (fd, device, segment);
   ops = fd->devices[device].descriptor->flash_ops;
-#if RTEMS_FDISK_TRACE
-  rtems_fdisk_printf (fd, "  seg-erase: %02d-%03d", device, segment);
-#endif
-  return ops->erase (sd, device, segment);
-}
-
-/**
- * Erase a device.
- */
-static int
-rtems_fdisk_device_erase (const rtems_flashdisk* fd, uint32_t device)
-{
-  const rtems_fdisk_driver_handlers* ops;
-  ops = fd->devices[device].descriptor->flash_ops;
-#if RTEMS_FDISK_TRACE
-  rtems_fdisk_printf (fd, " device-erase: %02d", device);
-#endif
-  return ops->erase_device (fd->devices[device].descriptor, device);
-}
-
-/**
- * Erase all flash.
- */
-static int
-rtems_fdisk_erase_flash (const rtems_flashdisk* fd)
-{
-  uint32_t device;
-  for (device = 0; device < fd->device_count; device++)
-  {
-    int ret;
-
-#if RTEMS_FDISK_TRACE
-    rtems_fdisk_info (fd, " erase-flash:%02d", device);
-#endif
-
-    ret = rtems_fdisk_device_erase (fd, device);
-
-    if (ret != 0)
-      return ret;
-  }
-  return 0;
-}
-
-/**
- * Calculate the checksum of a page in a segment.
- */
-static uint16_t
-rtems_fdisk_page_checksum (const uint8_t* buffer, uint32_t page_size)
-{
-  uint16_t cs = 0xffff;
-  uint32_t i;
-
-  for (i = 0; i < page_size; i++, buffer++)
-    cs = rtems_fdisk_calc_crc16 (cs, *buffer);
-
-  return cs;
-}
-
-/**
- * Erase the segment.
- */
-static int
-rtems_fdisk_erase_segment (rtems_flashdisk* fd, rtems_fdisk_segment_ctl* sc)
-{
-  int ret = rtems_fdisk_seg_erase (fd, sc->device, sc->segment);
+  ret = ops->erase (sd, device, segment);
   if (ret)
   {
@@ -1430,7 +1429,7 @@
                               dsc->device, dsc->segment, dpage);
 #endif
-            ret = rtems_fdisk_seg_copy_page (fd, ssc->device, ssc->segment,
+            ret = rtems_fdisk_seg_copy_page (fd, ssc,
                                              spage + ssc->pages_desc,
-                                             dsc->device, dsc->segment,
+                                             dsc,
                                              dpage + dsc->pages_desc);
             if (ret)
@@ -1442,5 +1441,4 @@
                                  dsc->device, dsc->segment, dpage,
                                  strerror (ret), ret);
-              dsc->failed = true;
               rtems_fdisk_queue_segment (fd, dsc);
               rtems_fdisk_segment_queue_push_head (&fd->used, ssc);
@@ -1451,5 +1449,5 @@
 
             ret = rtems_fdisk_seg_write_page_desc (fd,
-                                                   dsc->device, dsc->segment,
+                                                   dsc,
                                                    dpage, dpd);
 
@@ -1461,5 +1459,4 @@
                                  dsc->device, dsc->segment, dpage,
                                  strerror (ret), ret);
-              dsc->failed = true;
               rtems_fdisk_queue_segment (fd, dsc);
               rtems_fdisk_segment_queue_push_head (&fd->used, ssc);
@@ -1541,4 +1538,5 @@
    * Scan each segment or each device recovering the valid pages.
    */
+  fd->erased_blocks = 0;
   for (device = 0; device < fd->device_count; device++)
   {
@@ -1582,5 +1580,5 @@
        *       so NAND flash could be better supported.
        */
-      ret = rtems_fdisk_seg_read (fd, device, segment, 0, (void*) pd,
+      ret = rtems_fdisk_seg_read (fd, sc, 0, (void*) pd,
                                   sc->pages_desc * fd->block_size);
 
@@ -1608,5 +1606,5 @@
            * Is the page erased ?
            */
-          ret = rtems_fdisk_seg_blank_check_page (fd, device, segment,
+          ret = rtems_fdisk_seg_blank_check_page (fd, sc,
                                                   page + sc->pages_desc);
 
@@ -1619,5 +1617,5 @@
             rtems_fdisk_page_desc_set_flags (pd, RTEMS_FDISK_PAGE_USED);
 
-            ret = rtems_fdisk_seg_write_page_desc (fd, device, segment,
+            ret = rtems_fdisk_seg_write_page_desc (fd, sc,
                                                    page, pd);
 
@@ -1626,5 +1624,4 @@
               rtems_fdisk_error ("forcing page to used failed: %d-%d-%d",
                                  device, segment, page);
-              sc->failed = true;
             }
 
@@ -1764,5 +1761,5 @@
        * driver. This skips the page descriptors.
        */
-      int ret = rtems_fdisk_seg_read_page (fd, sc->device, sc->segment,
+      int ret = rtems_fdisk_seg_read_page (fd, sc,
                                            bc->page + sc->pages_desc, buffer);
 
@@ -1888,6 +1885,5 @@
     rtems_fdisk_page_desc_set_flags (pd, RTEMS_FDISK_PAGE_USED);
 
-    ret = rtems_fdisk_seg_write_page_desc_flags (fd, sc->device, sc->segment,
-                                                 bc->page, pd);
+    ret = rtems_fdisk_seg_write_page_desc_flags (fd, sc, bc->page, pd);
 
     if (ret)
@@ -1899,5 +1895,4 @@
                         strerror (ret), ret);
 #endif
-      sc->failed = true;
     }
     else
@@ -2002,6 +1997,5 @@
        * driver. This skips the page descriptors.
        */
-      ret = rtems_fdisk_seg_write_page (fd, sc->device, sc->segment,
-                                        page + sc->pages_desc, buffer);
+      ret = rtems_fdisk_seg_write_page (fd, sc, page + sc->pages_desc, buffer);
       if (ret)
       {
@@ -2011,10 +2005,8 @@
                           strerror (ret), ret);
 #endif
-        sc->failed = true;
       }
       else
       {
-        ret = rtems_fdisk_seg_write_page_desc (fd, sc->device, sc->segment,
-                                               page, pd);
+        ret = rtems_fdisk_seg_write_page_desc (fd, sc, page, pd);
         if (ret)
         {
@@ -2025,5 +2017,4 @@
                             strerror (ret), ret);
 #endif
-          sc->failed = true;
         }
         else
@@ -2546,5 +2537,5 @@
         return RTEMS_NO_MEMORY;
       }
-      
+
       sc = fd->devices[device].segments;
 
@@ -2583,5 +2574,5 @@
       return ret;
     }
-    
+
     ret = rtems_fdisk_compact (fd);
     if (ret)
