GadgetSeed  0.9.6
storage.c
[詳解]
1 /** @file
2  @brief ストレージデバイス
3 
4  ストレージデバイスのファイルシステムへのマウント、アンマウント等を行う。
5 
6  @date 2007.12.16
7  @author Takashi SHUDO
8 
9  @page storage_device ストレージデバイス
10 
11  GadgetSeedはストレージデバイスに対してファイルシステムへのマウント及びアンマウント操作を行うAPIがあります。
12 
13 
14  ---
15  @section storage_device_api ストレージデバイスAPI
16 
17  include ファイル : storage.h
18 
19  | API名 | 機能 |
20  |:--------------------------|:--------------------------------------|
21  | mount_storage() | @copybrief mount_storage |
22  | unmount_storage() | @copybrief unmount_storage |
23  | register_storage_device() | @copybrief register_storage_device |
24  | get_storage_device_name() | @copybrief get_storage_device_name |
25 
26  システムの初期化時に mount_storage() または register_storage_device() を実行し、ストレージデバイスをマウントして下さい。
27 
28  マウントされたデバイスは、ファイル操作関数によりアクセスすることができます。
29 */
30 
31 #include "storage.h"
32 #include "fs.h"
33 
34 #include "device.h"
35 #include "diskio.h"
36 #include "tprintf.h"
37 #include "tkprintf.h"
38 
39 struct st_storage_info storage[GSC_FS_VOLUME_NUM]; ///< ストレージデバイステーブル
40 
41 /**
42  @brief 外部記憶装置管理初期化
43 */
44 void init_storage(void)
45 {
46  int i ;
47 
48  for(i=0; i<GSC_FS_VOLUME_NUM; i++) {
49  storage[i].device = 0;
50  storage[i].fs = 0;
51  }
52 }
53 
54 #define DEVNAME_LEN 2 // "X:"
55 
56 /**
57  @brief ストレージデバイスをマウントする
58 
59  @param[in] drvno デバイス番号
60  @param[in] devname デバイス名文字列ポインタ
61 
62  @return エラーコード
63 */
64 int mount_storage(int drvno, const char *devname, const char *fsname)
65 {
66  int rtn;
67  struct st_filesystem *fs = 0;
68  struct st_device *dev = 0;
69 
70  if(drvno >= GSC_FS_VOLUME_NUM) {
71  SYSERR_PRINT("Storage number %d too large.\n", drvno);
72  return -1;
73  }
74 
75  if(storage[drvno].device != 0) {
76  SYSERR_PRINT("Storage %d: already mounted.\n", drvno);
77  return -1;
78  }
79 
80  fs = search_filesystem(fsname);
81  if(fs == 0) {
82  SYSERR_PRINT("Invalid file system \"%s\".\n", fsname);
83  return -1;
84  }
85  storage[drvno].fs = fs;
86 
87  if(devname != 0) {
88  dev = open_device((char *)devname);
89  if(dev != 0) {
90  storage[drvno].device = dev;
91  } else {
92  SYSERR_PRINT("Cannot open Device \"%s\" (Storage %d:).\n", devname, drvno);
93  return -1;
94  }
95  }
96 
97  if(fs->mount != 0) {
98  rtn = fs->mount(drvno, dev);
99  if(rtn != 0) {
100  SYSERR_PRINT("Device \"%s\" (Storage %d:) mount error.\n", devname, drvno);
101  storage[drvno].device = 0;
102  storage[drvno].fs = 0;
103  return -1;
104  } else {
105  return 0;
106  }
107  }
108 
109  return 0;
110 }
111 
112 /**
113  @brief ストレージデバイスをアンマウントする
114 
115  @param[in] drvno デバイス番号
116 
117  @return エラーコード
118 */
119 int unmount_storage(int drvno)
120 {
121  int rtn;
122 
123  if(drvno >= GSC_FS_VOLUME_NUM) {
124  SYSERR_PRINT("Storage number %d too large.\n", drvno);
125  return -1;
126  }
127 
128  if(storage[drvno].fs == 0) {
129  SYSERR_PRINT("Storage %d: not mounted.\n", drvno);
130  return -1;
131  }
132 
133  if(storage[drvno].device != 0) {
134  sync_device(storage[drvno].device);
135  }
136 
137  rtn = storage[drvno].fs->unmount(drvno, storage[drvno].device);
138  if(rtn != 0) {
139  SYSERR_PRINT("Device (Storage %d:) unmount error.\n", drvno);
140  return -1;
141  } else {
142  if(storage[drvno].device != 0) {
143  close_device(storage[drvno].device);
144  }
145 
146  storage[drvno].device = 0;
147  storage[drvno].fs = 0;
148 
149  return 0;
150  }
151 }
152 
153 /**
154  @brief ストレージデバイスをリストでマウントする
155 
156  @param[in] device_name デバイス名リスト
157 
158  @return 0:成功, !=0:失敗
159 */
160 int register_storage_device(const char * const device_name[])
161 {
162  int i = 0;
163  char *drvname = (char *)device_name[i];
164 
165  i = 0;
166  while(drvname) {
167  if(i < GSC_FS_VOLUME_NUM) {
168  if(mount_storage(i, drvname, FSNAME_VFAT) == 0) {
169  tprintf("Storage %d: \"%s\"\n", i, drvname);
170  } else {
171  tprintf("Storage %d: \"%s\" mount failed.\n",
172  i, drvname);
173  }
174  } else {
175  SYSERR_PRINT("Storage number %d too large.\n", i);
176  return -1;
177  break;
178  }
179  i++;
180  drvname = (char *)device_name[i];
181  }
182 
183  return 0;
184 }
185 
186 /**
187  @brief マウントされているデバイス名を取得する
188 
189  @param num デバイス番号
190  @param devneme デバイス名
191  @param fsneme ファイルシステム名
192 
193  @return 0:成功, !=0:マウントされていないデバイス番号
194 */
195 int get_storage_device_name(int drv, char **devname, char **fsname)
196 {
197  if(drv >= GSC_FS_VOLUME_NUM) {
198  return -1;
199  }
200 
201  if(storage[drv].device != 0) {
202  *devname = storage[drv].device->name;
203  *fsname = storage[drv].fs->name;
204  return 0;
205  } else {
206  return -1;
207  }
208 }
ファイルシステム構造体
Definition: fs.h:56
int unmount_storage(int drvno)
ストレージデバイスをアンマウントする
Definition: storage.c:119
struct st_device * open_device(char *name)
デバイスをオープンする
Definition: device.c:262
ファイルシステムAPI
const char * device_name(int num)
デバイス名を取得する
Definition: device.c:132
int mount_storage(int drvno, const char *devname, const char *fsname)
ストレージデバイスをマウントする
Definition: storage.c:64
int sync_device(struct st_device *dev)
デバイスの書き込みデータの同期をとる
Definition: device.c:563
struct st_filesystem * fs
ファイルシステム
Definition: storage.h:20
カーネル用機能限定printf
int register_storage_device(const char *const device_name[])
ストレージデバイスをリストでマウントする
Definition: storage.c:160
struct st_storage_info storage[GSC_FS_VOLUME_NUM]
ストレージデバイステーブル
Definition: storage.c:39
外部記憶装置管理
ストレージデバイス
Definition: storage.h:18
int tprintf(const char *fmt,...)
簡易printf
Definition: tprintf.c:85
#define GSC_FS_VOLUME_NUM
$gsc 最大ストレージデバイスボリューム数
Definition: storage.h:15
int get_storage_device_name(int drv, char **devname, char **fsname)
マウントされているデバイス名を取得する
Definition: storage.c:195
int close_device(struct st_device *dev)
デバイスをクローズする
Definition: device.c:291
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
char name[MAX_DEVNAMELRN]
デバイス名文字列
Definition: device.h:26
void init_storage(void)
外部記憶装置管理初期化
Definition: storage.c:44
機能限定printf
struct st_device * device
ストレージデバイスドライバ
Definition: storage.h:19