📄 hw_disk.c
字号:
/* This file is part of the program psim. Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#ifndef _HW_DISK_C_#define _HW_DISK_C_#include "device_table.h"#include "pk.h"#include <stdio.h>#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifndef SEEK_SET#define SEEK_SET 0#endif/* DEVICE cdrom - read-only removable mass storage device disk - mass storage device floppy - removable mass storage device DESCRIPTION Mass storage devices such as a hard-disk or cdrom-drive are not normally directly connected to the processor. Instead, these devices are attached to a logical bus, such as SCSI or IDE, and then a controller of that bus is made accessible to the processor. Reflecting this, within a device tree, mass storage devices such as a <<cdrom>>, <<disk>> or <<floppy>> are created as children of of a logical bus controller node (such as a SCSI or IDE interface). That controller, in turn, would be made the child of a physical bus node that is directly accessible to the processor. The above mass storage devices provide two interfaces - a logical and a physical. At the physical level the <<device_io_...>> functions can be used perform reads and writes of the raw media. The address being interpreted as an offset from the start of the disk. At the logical level, it is possible to create an instance of the disk that provides access to any of the physical media, a disk partition, or even a file within a partition. The <<disk-label>> package, which implements this functionality, is described elsewhere. Both the Open Firmware and Moto BUG rom emulations support this interface. Block devices such as the <<floppy>> and <<cdrom>> have removable media. At the programmer level, the media can be changed using the <<change_media>> ioctl. From within GDB, a <<change-media>> operation can be initated by using the command. | (gdb) sim PROPERTIES file = <file-name> (required) The name of the file that contains an image of the disk. For <<disk>> and <<floppy>> devices, the image will be opened for both reading and writing. Multiple image files may be specified, the second and later files being opened when <<change-media>> (with a NULL file name) being specified. block-size = <nr-bytes> (optional) The value is returned by the block-size method. The default value is 512 bytes. max-transfer = <nr-bytes> (optional) The value is returned by the max-transfer method. The default value is 512 bytes. #blocks = <nr-blocks> (optional) The value is returned by the #blocks method. If no value is present then -1 is returned. read-only = <anything> (optional) If this property is present, the disk file image is always opened read-only. EXAMPLES Enable tracing | $ psim -t 'disk-device' \ Add a CDROM and disk to an IDE bus. Specify the host operating system's cd drive as the CD-ROM image. | -o '/pci/ide/disk@0/file "disk-image' \ | -o '/pci/ide/cdrom@1/file "/dev/cd0a' \ As part of the code implementing a logical bus device (for instance the IDE controller), locate the CDROM device and then read block 47. | device *cdrom = device_tree_find_device(me, "cdrom"); | char block[512]; | device_io_read_buffer(cdrom, buf, 0, 0, 47 * sizeof(block), // space, address sizeof(block), NULL, 0); Use the device instance interface to read block 47 of the file called <<netbsd.elf>> on the disks default partition. Similar code would be used in an operating systems pre-boot loader. | device_instance *netbsd = | device_create_instance(root, "/pci/ide/disk:,\netbsd.elf"); | char block[512]; | device_instance_seek(netbsd, 0, 47 * sizeof(block)); | device_instance_read(netbsd, block, sizeof(block)); BUGS The block device specification includes mechanisms for determining the physical device characteristics - such as the disks size. Currently this mechanism is not implemented. The functionality of this device (in particular the device instance interface) depends on the implementation of <<disk-label>> package. That package may not be fully implemented. The disk does not know its size. Hence it relies on the failure of fread(), fwrite() and fseek() calls to detect errors. The disk size is limited by the addressable range covered by unsigned_word (addr). An extension would be to instead use the concatenated value space:addr. The method #blocks should `stat' the disk to determine the number of blocks if there is no #blocks property. It would appear that OpenFirmware does not define a client call for changing (ejecting) the media of a device. */typedef struct _hw_disk_device { int name_index; int nr_names; char *name; int read_only; /* unsigned_word size; */ FILE *image;} hw_disk_device;typedef struct _hw_disk_instance { unsigned_word pos; hw_disk_device *disk;} hw_disk_instance;static voidopen_disk_image(device *me, hw_disk_device *disk, const char *name){ if (disk->image != NULL) fclose(disk->image); if (disk->name != NULL) zfree(disk->name); disk->name = strdup(name); disk->image = fopen(disk->name, disk->read_only ? "r" : "r+"); if (disk->image == NULL) { perror(device_name(me)); device_error(me, "open %s failed\n", disk->name); } DTRACE(disk, ("image %s (%s)\n", disk->name, (disk->read_only ? "read-only" : "read-write")));}static voidhw_disk_init_address(device *me){ hw_disk_device *disk = device_data(me); unsigned_word address; int space; const char *name; /* attach to the parent. Since the bus is logical, attach using just the unit-address (size must be zero) */ device_address_to_attach_address(device_parent(me), device_unit_address(me), &space, &address, me); device_attach_address(device_parent(me), attach_callback, space, address, 0/*size*/, access_read_write_exec, me); /* Tell the world we are a disk. */ device_add_string_property(me, "device_type", "block"); /* get the name of the file specifying the disk image */ disk->name_index = 0; disk->nr_names = device_find_string_array_property(me, "file", disk->name_index, &name); if (!disk->nr_names) device_error(me, "invalid file property"); /* is it a RO device? */ disk->read_only = (strcmp(device_name(me), "disk") != 0 && strcmp(device_name(me), "floppy") != 0 && device_find_property(me, "read-only") == NULL); /* now open it */ open_disk_image(me, disk, name);}static inthw_disk_ioctl(device *me, cpu *processor, unsigned_word cia, device_ioctl_request request, va_list ap){ switch (request) { case device_ioctl_change_media: { hw_disk_device *disk = device_data(me); const char *name = va_arg(ap, const char *); if (name != NULL) { disk->name_index = -1; } else { disk->name_index = (disk->name_index + 1) % disk->nr_names; if (!device_find_string_array_property(me, "file", disk->name_index, &name)) device_error(me, "invalid file property"); } open_disk_image(me, disk, name); } break; default: device_error(me, "insupported ioctl request"); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -