SayoriOS 0.3.3

diskctl.c
1 #include "drv/atapi.h"
2 #include "io/tty.h"
3 #include "io/status_loggers.h"
4
5 extern ata_drive_t drives[4];
6
7 void diskctl_help() {
8 tty_printf(
9 "DiskCTL - Disk Management Utility by NDRAEY (c) 2023.\n"
10 "\n"
11 "Commands:\n"
12 "- info [disk num] - shows info about selected disk (or all disks if disk num is not present)\n"
13 "- eject [disk num] - ejects disc\n"
14 );
15 }
16
17 void diskctl_show_disk(size_t drive_n) {
18 if(drives[drive_n].is_packet) // Re-Identifying works in ATAPI devices
19 ide_identify(drive_n >> 1, drive_n & 1);
20
21 ata_drive_t drive = drives[drive_n];
22
23 tty_printf(
24 "Drive #%d: %s [%s]\n",
25 drive_n + 1,
26 drive.is_packet ? "Packet Device" : "Hard Drive",
27 drive.online ? "online" : "offline"
28 );
29
30 if(!drive.online)
31 goto end;
32
33 tty_printf("|--- Capacity: ");
34
35 if(drive.is_chs_addressing) {
36 tty_printf(
37 "Cylinders: %d; Heads: %d; Sectors: %d\n",
38 drive.cylinders,
39 drive.heads,
40 drive.sectors
41 );
42 } else {
43 tty_printf(
44 "%d bytes; %d kilobytes; %d megabytes\n",
45 drive.capacity,
46 drive.capacity / 1024,
47 drive.capacity / (1024 * 1024)
48 );
49 }
50
51 tty_printf("|--- CHS Addressing: %s\n", drive.is_chs_addressing ? "yes" : "no");
52 tty_printf("|--- Block size: %d\n", drive.block_size);
53
54 if(drive.is_packet)
55 tty_printf("|--- Media presence: %s\n", atapi_check_media_presence(ATA_SECONDARY, ATA_MASTER) ? "true" : "false");
56
57 end:
58 tty_printf("\n");
59 }
60
61 void diskctl_show_all_disks() {
62 for(int i = 0; i < 4; i++) {
63 diskctl_show_disk(i);
64 }
65 }
66
67 void diskctl_info(uint32_t argc, char** argv) {
68 if(!argc) {
69 diskctl_show_all_disks();
70 } else {
71 if(!isnumberstr(argv[1])) {
72 tty_error("Not a number!\n");
73 return;
74 }
75
76 size_t disk_n = atoi(argv[1]);
77
78 if(disk_n < 1 || disk_n > 4) {
79 tty_error("Invalid disk number. Disk number in range 1 ... 4\n");
80 return;
81 }
82
83 diskctl_show_disk(disk_n - 1);
84 }
85 }
86
87 void diskctl_eject(uint32_t argc, char** argv) {
88 if(!argc) {
89 tty_error("Choose disc drive in range 1 ... 4");
90 } else {
91 if(!isnumberstr(argv[1])) {
92 tty_error("Not a number!\n");
93 return;
94 }
95
96 size_t disk_n = atoi(argv[1]);
97
98 if(disk_n < 1 || disk_n > 4) {
99 tty_error("Invalid disk number. Disk number in range 1 ... 4\n");
100 return;
101 }
102
103 disk_n--;
104
105 if(!drives[disk_n].is_packet) {
106 tty_error("This drive does not supports eject.\n");
107 return;
108 }
109
110 atapi_eject(disk_n >> 1, disk_n & 1);
111 }
112 }
113
114 void diskctl_test(uint32_t argc, char** argv) {
115 if(!argc) {
116 tty_error("Choose disc drive in range 1 ... 4");
117 } else {
118 if(!isnumberstr(argv[1])) {
119 tty_error("Not a number!\n");
120 return;
121 }
122
123 size_t disk_n = atoi(argv[1]);
124
125 if(disk_n < 1 || disk_n > 4) {
126 tty_error("Invalid disk number. Disk number in range 1 ... 4\n");
127 return;
128 }
129
130 disk_n--;
131
132 if(!drives[disk_n].online) {
133 tty_printf("Cannot read from that drive: it's offline.\n");
134 return;
135 }
136
137 tty_printf("Allocating 1 MB buffer...\n");
138
139 uint8_t* buf = kmalloc(1 * 1024 * 1024);
140
141 tty_printf("Reading 1 MB from address 0...\n");
142
143 ata_read(disk_n, buf, 0, 1 * 1024 * 1024);
144
145 tty_printf("Freeing buffer...\n");
146
147 kfree(buf);
148 }
149 }
150
151 int shell_diskctl(uint32_t argc, char** argv) {
152 if(argc == 0) {
153 tty_printf("Not enough arguments.\n\n");
154
155 diskctl_help();
156 return 1;
157 }
158
159 char* verb = argv[1];
160
161 if(!strcmp(verb, "info")) {
162 diskctl_info(argc - 1, argv + 1);
163 } else if(!strcmp(verb, "eject")) {
164 diskctl_eject(argc - 1, argv + 1);
165 } else if(!strcmp(verb, "test")) {
166 diskctl_test(argc - 1, argv + 1);
167 } else {
168 tty_error("Unknown verb: %s", verb);
169 }
170
171 return 0;
172 }
strcmp
int strcmp(const char *s1, const char *s2)
Сравнение строк
Definition: string.c:253
atoi
uint32_t atoi(const char s[])
Превращает строку в число
Definition: string.c:565
ata_drive_t
Эта структура определяет каждый ATA диск в системе
Definition: ata.h:102
ata_drive_t::heads
uint16_t heads
Цилиндры
Definition: ata.h:114
ata_drive_t::cylinders
uint16_t cylinders
Размер сектора
Definition: ata.h:113
ata_drive_t::sectors
uint16_t sectors
Головки
Definition: ata.h:115
ata_drive_t::block_size
uint16_t block_size
Адресация по CHS?
Definition: ata.h:111
ata_drive_t::capacity
size_t capacity
Устройство доступно?
Definition: ata.h:105
ata_drive_t::is_packet
bool is_packet
Ёмкость диска в секторах
Definition: ata.h:107