changeset 1685:b1d2762b2eec

* src/disk.c: * include/disk.h: * include/diskio.h: Use predefined arrays for partition devices and info radher than malloc. Extended DISK_CHANNEL macro to support defining maximum number of partitions.
author asl
date Thu, 01 Jul 2004 13:03:20 +0000
parents 3cd6f4cd3db2
children 961151dc048e
files packages/io/disk/current/ChangeLog packages/io/disk/current/include/disk.h packages/io/disk/current/include/diskio.h packages/io/disk/current/src/disk.c
diffstat 4 files changed, 58 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/packages/io/disk/current/ChangeLog
+++ b/packages/io/disk/current/ChangeLog
@@ -1,3 +1,12 @@
+2004-07-01  Savin Zlobec  <savin@elatec.si> 
+
+ 	* src/disk.c:
+ 	* include/disk.h:
+ 	* include/diskio.h:
+ 	Use predefined arrays for partition devices and info 
+ 	radher than malloc. Extended DISK_CHANNEL macro to
+  	support defining maximum number of partitions.
+
 2004-01-15  Nick Garnett  <nickg@calivar.com>
 
 	* src/disk.c:
--- a/packages/io/disk/current/include/disk.h
+++ b/packages/io/disk/current/include/disk.h
@@ -106,6 +106,8 @@ struct disk_channel {
     void                    *dev_priv;    // device private data
     cyg_disk_info_t         *info;        // disk info 
     cyg_disk_partition_t    *partition;   // partition data 
+    struct cyg_devtab_entry *pdevs_dev;   // partition devs devtab ents 
+    disk_channel            *pdevs_chan;  // partition devs disk chans 
     cyg_bool                 mbr_support; // true if disk has MBR
     cyg_bool                 valid;       // true if device valid 
     cyg_bool                 init;        // true if initialized
@@ -115,14 +117,23 @@ struct disk_channel {
 #define DISK_CHANNEL(_l,                                              \
                      _funs,                                           \
                      _dev_priv,                                       \
-                     _mbr_supp)                                       \
-cyg_disk_info_t _l##_disk_info;                                       \
-disk_channel _l = {                                                   \
+                     _mbr_supp,                                       \
+                     _max_part_num)                                   \
+static struct cyg_devtab_entry _l##_part_dev[_max_part_num];          \
+static disk_channel            _l##_part_chan[_max_part_num];         \
+static cyg_disk_partition_t    _l##_part_tab[_max_part_num];          \
+static cyg_disk_info_t         _l##_disk_info = {                     \
+    _l##_part_tab,                                                    \
+    _max_part_num                                                     \
+};                                                                    \
+static disk_channel _l = {                                            \
     &(_funs),                                                         \
     &cyg_io_disk_callbacks,                                           \
     &(_dev_priv),                                                     \
     &(_l##_disk_info),                                                \
     NULL,                                                             \
+    _l##_part_dev,                                                    \
+    _l##_part_chan,                                                   \
     _mbr_supp,                                                        \
     false,                                                            \
     false                                                             \
--- a/packages/io/disk/current/include/diskio.h
+++ b/packages/io/disk/current/include/diskio.h
@@ -58,7 +58,7 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-    
+
 typedef struct {
     char        serial[20+1];      // serial number
     char        firmware_rev[8+1]; // firmware revision
@@ -78,9 +78,9 @@ typedef struct {
 } cyg_disk_partition_t;
 
 typedef struct {
+    cyg_disk_partition_t *partitions;    // partition table
+    int                   partitions_num;// partition table size
     cyg_disk_identify_t   ident;         // identify data
-    cyg_disk_partition_t  partitions[4]; // partitions
-    cyg_addrword_t        devs[4];       // device instances
     cyg_uint32            block_size;    // block size
     cyg_uint32            blocks_num;    // number of blocks on disk
     cyg_bool              connected;     // true if device connected
--- a/packages/io/disk/current/src/disk.c
+++ b/packages/io/disk/current/src/disk.c
@@ -56,8 +56,6 @@
 #include <cyg/infra/cyg_ass.h>      // assertion support
 #include <cyg/infra/diag.h>         // diagnostic output
 
-#include <stdlib.h>  // malloc 
-
 // ---------------------------------------------------------------------------
 
 //#define DEBUG 1
@@ -172,7 +170,7 @@ read_partition(cyg_uint8            *dat
 
     READ_CHS(&data[5], c, h, s);
     CHS_TO_LBA(&info->ident, c, h, s, part->end);
-
+    
     READ_DWORD(&data[12], part->size);
 }
 
@@ -188,7 +186,7 @@ read_mbr(disk_channel *chan)
     Cyg_ErrNo res = ENOERR;
     int i;
  
-    for (i = 0; i < MBR_PART_NUM; i++)
+    for (i = 0; i < info->partitions_num; i++)
         info->partitions[i].type = 0x00;    
    
     res = (funs->read)(chan, (void *)buf, 512, 0);
@@ -197,9 +195,14 @@ read_mbr(disk_channel *chan)
 
     if (MBR_SIG_BYTE0 == buf[MBR_SIG_ADDR+0] && MBR_SIG_BYTE1 == buf[MBR_SIG_ADDR+1])
     {
+        int npart;
+
         D(("disk MBR found\n")); 
  
-        for (i = 0; i < MBR_PART_NUM; i++)
+        npart = info->partitions_num < MBR_PART_NUM ? 
+            info->partitions_num : MBR_PART_NUM;
+
+        for (i = 0; i < npart; i++)
         {
             cyg_disk_partition_t *part = &info->partitions[i];
             
@@ -231,14 +234,10 @@ disk_init(struct cyg_devtab_entry *tab)
     {
         info->connected = false;
 
-        // clear devices array (one per partition)
-        // and partition data
-        for (i = 0; i < MBR_PART_NUM; i++)
-        {
-            info->devs[i] = (cyg_addrword_t) 0;
+        // clear partition data
+        for (i = 0; i < info->partitions_num; i++)
             info->partitions[i].type = 0x00;
-        }
-        
+
         chan->init = true;
     }
     return true;
@@ -294,22 +293,11 @@ disk_disconnected(struct cyg_devtab_entr
     info->connected = false;
     chan->valid     = false;
      
-    // clear partition data and invalidate 
-    // any allocated devices
-    for (i = 0; i < MBR_PART_NUM; i++)
+    // clear partition data and invalidate partition devices 
+    for (i = 0; i < info->partitions_num; i++)
     {
-        info->partitions[i].type = 0x00;
-
-        if (0 != info->devs[i])
-        {
-            struct cyg_devtab_entry *dtab;
-            disk_channel            *dchan;
-
-            dtab  = (struct cyg_devtab_entry *)info->devs[i];
-            dchan = (disk_channel *) dtab->priv;
-            
-            dchan->valid = false;
-        }
+        info->partitions[i].type  = 0x00;
+        chan->pdevs_chan[i].valid = false;
     }
 
     
@@ -329,10 +317,22 @@ disk_lookup(struct cyg_devtab_entry **ta
     disk_channel            *new_chan;
     int dev_num;
     
-    if (!info->connected || name[0] < '0' || name[0] > '4' || '\0' != name[1])
+    if (!info->connected)
         return -EINVAL;
 
-    dev_num = name[0] - '0';
+    dev_num = 0;
+
+    while ('\0' != *name)
+    {
+        if (*name < '0' || *name > '9')
+            return -EINVAL;
+
+        dev_num = 10 * dev_num + (*name - '0');
+        name++;
+    }
+   
+    if (dev_num > info->partitions_num)
+        return -EINVAL;
 
     D(("disk lookup dev number = %d\n", dev_num)); 
 
@@ -346,30 +346,9 @@ disk_lookup(struct cyg_devtab_entry **ta
         D(("disk NO partition for dev\n")); 
         return -EINVAL;
     }
-    
-    if (0 == info->devs[dev_num-1])
-    {
-        D(("disk creating new devtab entry\n")); 
 
-        // alloc mem for new device
-        new_tab = (struct cyg_devtab_entry *)
-            malloc(sizeof(struct cyg_devtab_entry));
-        if (NULL == new_tab)
-            return -ENOMEM;        
-
-        // alloc mem for new device private data
-        new_chan = (disk_channel *)malloc(sizeof(disk_channel));
-        if (NULL == new_chan)
-        {
-            free(new_tab);
-            return -ENOMEM;
-        }
-    }
-    else
-    {
-        new_tab  = (struct cyg_devtab_entry *) info->devs[dev_num-1]; 
-        new_chan = (disk_channel *) new_tab->priv;   
-    }
+    new_tab  = &chan->pdevs_dev[dev_num-1];
+    new_chan = &chan->pdevs_chan[dev_num-1];
     
     // copy device data from parent
     *new_tab  = **tab; 
@@ -377,9 +356,8 @@ disk_lookup(struct cyg_devtab_entry **ta
 
     new_tab->priv = (void *)new_chan;
 
-    // set partition ptr and put this device into devices array    
+    // set partition ptr
     new_chan->partition = &info->partitions[dev_num-1];
-    chan->info->devs[dev_num-1] = (cyg_addrword_t) new_tab;
 
     // return device tab 
     *tab = new_tab;