hw_disk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _HW_DISK_C_
  15. #define _HW_DISK_C_
  16. #include "device_table.h"
  17. #include "pk.h"
  18. #include <stdio.h>
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #ifndef SEEK_SET
  23. #define SEEK_SET 0
  24. #endif
  25. /* DEVICE
  26. cdrom - read-only removable mass storage device
  27. disk - mass storage device
  28. floppy - removable mass storage device
  29. DESCRIPTION
  30. Mass storage devices such as a hard-disk or cdrom-drive are not
  31. normally directly connected to the processor. Instead, these
  32. devices are attached to a logical bus, such as SCSI or IDE, and
  33. then a controller of that bus is made accessible to the processor.
  34. Reflecting this, within a device tree, mass storage devices such as
  35. a <<cdrom>>, <<disk>> or <<floppy>> are created as children of of a
  36. logical bus controller node (such as a SCSI or IDE interface).
  37. That controller, in turn, would be made the child of a physical bus
  38. node that is directly accessible to the processor.
  39. The above mass storage devices provide two interfaces - a logical
  40. and a physical.
  41. At the physical level the <<device_io_...>> functions can be used
  42. perform reads and writes of the raw media. The address being
  43. interpreted as an offset from the start of the disk.
  44. At the logical level, it is possible to create an instance of the
  45. disk that provides access to any of the physical media, a disk
  46. partition, or even a file within a partition. The <<disk-label>>
  47. package, which implements this functionality, is described
  48. elsewhere. Both the Open Firmware and Moto BUG rom emulations
  49. support this interface.
  50. Block devices such as the <<floppy>> and <<cdrom>> have removable
  51. media. At the programmer level, the media can be changed using the
  52. <<change_media>> ioctl. From within GDB, a <<change-media>>
  53. operation can be initated by using the command.
  54. | (gdb) sim
  55. PROPERTIES
  56. file = <file-name> (required)
  57. The name of the file that contains an image of the disk. For
  58. <<disk>> and <<floppy>> devices, the image will be opened for both
  59. reading and writing. Multiple image files may be specified, the
  60. second and later files being opened when <<change-media>> (with a
  61. NULL file name) being specified.
  62. block-size = <nr-bytes> (optional)
  63. The value is returned by the block-size method. The default value
  64. is 512 bytes.
  65. max-transfer = <nr-bytes> (optional)
  66. The value is returned by the max-transfer method. The default value
  67. is 512 bytes.
  68. #blocks = <nr-blocks> (optional)
  69. The value is returned by the #blocks method. If no value is
  70. present then -1 is returned.
  71. read-only = <anything> (optional)
  72. If this property is present, the disk file image is always opened
  73. read-only.
  74. EXAMPLES
  75. Enable tracing
  76. | $ psim -t 'disk-device' \
  77. Add a CDROM and disk to an IDE bus. Specify the host operating
  78. system's cd drive as the CD-ROM image.
  79. | -o '/pci/ide/disk@0/file "disk-image' \
  80. | -o '/pci/ide/cdrom@1/file "/dev/cd0a' \
  81. As part of the code implementing a logical bus device (for instance
  82. the IDE controller), locate the CDROM device and then read block
  83. 47.
  84. | device *cdrom = device_tree_find_device(me, "cdrom");
  85. | char block[512];
  86. | device_io_read_buffer(cdrom, buf, 0,
  87. 0, 47 * sizeof(block), // space, address
  88. sizeof(block), NULL, 0);
  89. Use the device instance interface to read block 47 of the file
  90. called <<netbsd.elf>> on the disks default partition. Similar code
  91. would be used in an operating systems pre-boot loader.
  92. | device_instance *netbsd =
  93. | device_create_instance(root, "/pci/ide/disk:,\netbsd.elf");
  94. | char block[512];
  95. | device_instance_seek(netbsd, 0, 47 * sizeof(block));
  96. | device_instance_read(netbsd, block, sizeof(block));
  97. BUGS
  98. The block device specification includes mechanisms for determining
  99. the physical device characteristics - such as the disks size.
  100. Currently this mechanism is not implemented.
  101. The functionality of this device (in particular the device instance
  102. interface) depends on the implementation of <<disk-label>> package.
  103. That package may not be fully implemented.
  104. The disk does not know its size. Hence it relies on the failure of
  105. fread(), fwrite() and fseek() calls to detect errors.
  106. The disk size is limited by the addressable range covered by
  107. unsigned_word (addr). An extension would be to instead use the
  108. concatenated value space:addr.
  109. The method #blocks should `stat' the disk to determine the number
  110. of blocks if there is no #blocks property.
  111. It would appear that OpenFirmware does not define a client call for
  112. changing (ejecting) the media of a device.
  113. */
  114. typedef struct _hw_disk_device {
  115. int name_index;
  116. int nr_names;
  117. char *name;
  118. int read_only;
  119. /* unsigned_word size; */
  120. FILE *image;
  121. } hw_disk_device;
  122. typedef struct _hw_disk_instance {
  123. unsigned_word pos;
  124. hw_disk_device *disk;
  125. } hw_disk_instance;
  126. static void
  127. open_disk_image(device *me,
  128. hw_disk_device *disk,
  129. const char *name)
  130. {
  131. if (disk->image != NULL)
  132. fclose(disk->image);
  133. if (disk->name != NULL)
  134. free(disk->name);
  135. disk->name = strdup(name);
  136. disk->image = fopen(disk->name, disk->read_only ? "r" : "r+");
  137. if (disk->image == NULL) {
  138. perror(device_name(me));
  139. device_error(me, "open %s failed\n", disk->name);
  140. }
  141. DTRACE(disk, ("image %s (%s)\n",
  142. disk->name,
  143. (disk->read_only ? "read-only" : "read-write")));
  144. }
  145. static void
  146. hw_disk_init_address(device *me)
  147. {
  148. hw_disk_device *disk = device_data(me);
  149. unsigned_word address;
  150. int space;
  151. const char *name;
  152. /* attach to the parent. Since the bus is logical, attach using just
  153. the unit-address (size must be zero) */
  154. device_address_to_attach_address(device_parent(me), device_unit_address(me),
  155. &space, &address, me);
  156. device_attach_address(device_parent(me), attach_callback,
  157. space, address, 0/*size*/, access_read_write_exec,
  158. me);
  159. /* Tell the world we are a disk. */
  160. device_add_string_property(me, "device_type", "block");
  161. /* get the name of the file specifying the disk image */
  162. disk->name_index = 0;
  163. disk->nr_names = device_find_string_array_property(me, "file",
  164. disk->name_index, &name);
  165. if (!disk->nr_names)
  166. device_error(me, "invalid file property");
  167. /* is it a RO device? */
  168. disk->read_only =
  169. (strcmp(device_name(me), "disk") != 0
  170. && strcmp(device_name(me), "floppy") != 0
  171. && device_find_property(me, "read-only") == NULL);
  172. /* now open it */
  173. open_disk_image(me, disk, name);
  174. }
  175. static int
  176. hw_disk_ioctl(device *me,
  177. cpu *processor,
  178. unsigned_word cia,
  179. device_ioctl_request request,
  180. va_list ap)
  181. {
  182. switch (request) {
  183. case device_ioctl_change_media:
  184. {
  185. hw_disk_device *disk = device_data(me);
  186. const char *name = va_arg(ap, const char *);
  187. if (name != NULL) {
  188. disk->name_index = -1;
  189. }
  190. else {
  191. disk->name_index = (disk->name_index + 1) % disk->nr_names;
  192. if (!device_find_string_array_property(me, "file",
  193. disk->name_index, &name))
  194. device_error(me, "invalid file property");
  195. }
  196. open_disk_image(me, disk, name);
  197. }
  198. break;
  199. default:
  200. device_error(me, "insupported ioctl request");
  201. break;
  202. }
  203. return 0;
  204. }
  205. static unsigned
  206. hw_disk_io_read_buffer(device *me,
  207. void *dest,
  208. int space,
  209. unsigned_word addr,
  210. unsigned nr_bytes,
  211. cpu *processor,
  212. unsigned_word cia)
  213. {
  214. hw_disk_device *disk = device_data(me);
  215. unsigned nr_bytes_read;
  216. if (space != 0)
  217. device_error(me, "read - extended disk addressing unimplemented");
  218. if (nr_bytes == 0)
  219. nr_bytes_read = 0;
  220. else if (fseek(disk->image, addr, SEEK_SET) < 0)
  221. nr_bytes_read = 0;
  222. else if (fread(dest, nr_bytes, 1, disk->image) != 1)
  223. nr_bytes_read = 0;
  224. else
  225. nr_bytes_read = nr_bytes;
  226. DTRACE(disk, ("io-read - address 0x%lx, nr-bytes-read %d, requested %d\n",
  227. (unsigned long) addr, (int)nr_bytes_read, (int)nr_bytes));
  228. return nr_bytes_read;
  229. }
  230. static unsigned
  231. hw_disk_io_write_buffer(device *me,
  232. const void *source,
  233. int space,
  234. unsigned_word addr,
  235. unsigned nr_bytes,
  236. cpu *processor,
  237. unsigned_word cia)
  238. {
  239. hw_disk_device *disk = device_data(me);
  240. unsigned nr_bytes_written;
  241. if (space != 0)
  242. device_error(me, "write - extended disk addressing unimplemented");
  243. if (disk->read_only)
  244. nr_bytes_written = 0;
  245. else if (nr_bytes == 0)
  246. nr_bytes_written = 0;
  247. else if (fseek(disk->image, addr, SEEK_SET) < 0)
  248. nr_bytes_written = 0;
  249. else if (fwrite(source, nr_bytes, 1, disk->image) != 1)
  250. nr_bytes_written = 0;
  251. else
  252. nr_bytes_written = nr_bytes;
  253. DTRACE(disk, ("io-write - address 0x%lx, nr-bytes-written %d, requested %d\n",
  254. (unsigned long) addr, (int)nr_bytes_written, (int)nr_bytes));
  255. return nr_bytes_written;
  256. }
  257. /* instances of the hw_disk device */
  258. static void
  259. hw_disk_instance_delete(device_instance *instance)
  260. {
  261. hw_disk_instance *data = device_instance_data(instance);
  262. DITRACE(disk, ("delete - instance=%ld\n",
  263. (unsigned long)device_instance_to_external(instance)));
  264. free(data);
  265. }
  266. static int
  267. hw_disk_instance_read(device_instance *instance,
  268. void *buf,
  269. unsigned_word len)
  270. {
  271. hw_disk_instance *data = device_instance_data(instance);
  272. DITRACE(disk, ("read - instance=%ld len=%ld\n",
  273. (unsigned long)device_instance_to_external(instance),
  274. (long)len));
  275. if ((data->pos + len) < data->pos)
  276. return -1; /* overflow */
  277. if (fseek(data->disk->image, data->pos, SEEK_SET) < 0)
  278. return -1;
  279. if (fread(buf, len, 1, data->disk->image) != 1)
  280. return -1;
  281. data->pos = ftell(data->disk->image);
  282. return len;
  283. }
  284. static int
  285. hw_disk_instance_write(device_instance *instance,
  286. const void *buf,
  287. unsigned_word len)
  288. {
  289. hw_disk_instance *data = device_instance_data(instance);
  290. DITRACE(disk, ("write - instance=%ld len=%ld\n",
  291. (unsigned long)device_instance_to_external(instance),
  292. (long)len));
  293. if ((data->pos + len) < data->pos)
  294. return -1; /* overflow */
  295. if (data->disk->read_only)
  296. return -1;
  297. if (fseek(data->disk->image, data->pos, SEEK_SET) < 0)
  298. return -1;
  299. if (fwrite(buf, len, 1, data->disk->image) != 1)
  300. return -1;
  301. data->pos = ftell(data->disk->image);
  302. return len;
  303. }
  304. static int
  305. hw_disk_instance_seek(device_instance *instance,
  306. unsigned_word pos_hi,
  307. unsigned_word pos_lo)
  308. {
  309. hw_disk_instance *data = device_instance_data(instance);
  310. if (pos_hi != 0)
  311. device_error(device_instance_device(instance),
  312. "seek - extended addressing unimplemented");
  313. DITRACE(disk, ("seek - instance=%ld pos_hi=%ld pos_lo=%ld\n",
  314. (unsigned long)device_instance_to_external(instance),
  315. (long)pos_hi, (long)pos_lo));
  316. data->pos = pos_lo;
  317. return 0;
  318. }
  319. static int
  320. hw_disk_max_transfer(device_instance *instance,
  321. int n_stack_args,
  322. unsigned32 stack_args[/*n_stack_args*/],
  323. int n_stack_returns,
  324. unsigned32 stack_returns[/*n_stack_returns*/])
  325. {
  326. device *me = device_instance_device(instance);
  327. if ((n_stack_args != 0)
  328. || (n_stack_returns != 1)) {
  329. device_error(me, "Incorrect number of arguments for max-transfer method\n");
  330. return -1;
  331. }
  332. else {
  333. unsigned_cell max_transfer;
  334. if (device_find_property(me, "max-transfer"))
  335. max_transfer = device_find_integer_property(me, "max-transfer");
  336. else
  337. max_transfer = 512;
  338. DITRACE(disk, ("max-transfer - instance=%ld max-transfer=%ld\n",
  339. (unsigned long)device_instance_to_external(instance),
  340. (long int)max_transfer));
  341. stack_returns[0] = max_transfer;
  342. return 0;
  343. }
  344. }
  345. static int
  346. hw_disk_block_size(device_instance *instance,
  347. int n_stack_args,
  348. unsigned32 stack_args[/*n_stack_args*/],
  349. int n_stack_returns,
  350. unsigned32 stack_returns[/*n_stack_returns*/])
  351. {
  352. device *me = device_instance_device(instance);
  353. if ((n_stack_args != 0)
  354. || (n_stack_returns != 1)) {
  355. device_error(me, "Incorrect number of arguments for block-size method\n");
  356. return -1;
  357. }
  358. else {
  359. unsigned_cell block_size;
  360. if (device_find_property(me, "block-size"))
  361. block_size = device_find_integer_property(me, "block-size");
  362. else
  363. block_size = 512;
  364. DITRACE(disk, ("block-size - instance=%ld block-size=%ld\n",
  365. (unsigned long)device_instance_to_external(instance),
  366. (long int)block_size));
  367. stack_returns[0] = block_size;
  368. return 0;
  369. }
  370. }
  371. static int
  372. hw_disk_nr_blocks(device_instance *instance,
  373. int n_stack_args,
  374. unsigned32 stack_args[/*n_stack_args*/],
  375. int n_stack_returns,
  376. unsigned32 stack_returns[/*n_stack_returns*/])
  377. {
  378. device *me = device_instance_device(instance);
  379. if ((n_stack_args != 0)
  380. || (n_stack_returns != 1)) {
  381. device_error(me, "Incorrect number of arguments for block-size method\n");
  382. return -1;
  383. }
  384. else {
  385. unsigned_word nr_blocks;
  386. if (device_find_property(me, "#blocks"))
  387. nr_blocks = device_find_integer_property(me, "#blocks");
  388. else
  389. nr_blocks = -1;
  390. DITRACE(disk, ("#blocks - instance=%ld #blocks=%ld\n",
  391. (unsigned long)device_instance_to_external(instance),
  392. (long int)nr_blocks));
  393. stack_returns[0] = nr_blocks;
  394. return 0;
  395. }
  396. }
  397. static device_instance_methods hw_disk_instance_methods[] = {
  398. { "max-transfer", hw_disk_max_transfer },
  399. { "block-size", hw_disk_block_size },
  400. { "#blocks", hw_disk_nr_blocks },
  401. { NULL, },
  402. };
  403. static const device_instance_callbacks hw_disk_instance_callbacks = {
  404. hw_disk_instance_delete,
  405. hw_disk_instance_read,
  406. hw_disk_instance_write,
  407. hw_disk_instance_seek,
  408. hw_disk_instance_methods,
  409. };
  410. static device_instance *
  411. hw_disk_create_instance(device *me,
  412. const char *path,
  413. const char *args)
  414. {
  415. device_instance *instance;
  416. hw_disk_device *disk = device_data(me);
  417. hw_disk_instance *data = ZALLOC(hw_disk_instance);
  418. data->disk = disk;
  419. data->pos = 0;
  420. instance = device_create_instance_from(me, NULL,
  421. data,
  422. path, args,
  423. &hw_disk_instance_callbacks);
  424. DITRACE(disk, ("create - path=%s(%s) instance=%ld\n",
  425. path, args,
  426. (unsigned long)device_instance_to_external(instance)));
  427. return pk_disklabel_create_instance(instance, args);
  428. }
  429. static device_callbacks const hw_disk_callbacks = {
  430. { hw_disk_init_address, NULL },
  431. { NULL, }, /* address */
  432. { hw_disk_io_read_buffer,
  433. hw_disk_io_write_buffer, },
  434. { NULL, }, /* DMA */
  435. { NULL, }, /* interrupt */
  436. { NULL, }, /* unit */
  437. hw_disk_create_instance,
  438. hw_disk_ioctl,
  439. };
  440. static void *
  441. hw_disk_create(const char *name,
  442. const device_unit *unit_address,
  443. const char *args)
  444. {
  445. /* create the descriptor */
  446. hw_disk_device *hw_disk = ZALLOC(hw_disk_device);
  447. return hw_disk;
  448. }
  449. const device_descriptor hw_disk_device_descriptor[] = {
  450. { "disk", hw_disk_create, &hw_disk_callbacks },
  451. { "cdrom", hw_disk_create, &hw_disk_callbacks },
  452. { "floppy", hw_disk_create, &hw_disk_callbacks },
  453. { NULL },
  454. };
  455. #endif /* _HW_DISK_C_ */