aboutsummaryrefslogtreecommitdiffstats
path: root/com32/hdt/hdt-cli-disk.c
blob: 3209c8b12d8c5fe5086935df0f2cafa19a7be5b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2009 Pierre-Alexandre Meyer - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * -----------------------------------------------------------------------
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <disk/geom.h>
#include <disk/read.h>
#include <disk/error.h>
#include <disk/swsusp.h>
#include <disk/msdos.h>

#include "hdt-cli.h"
#include "hdt-common.h"
#include "hdt-util.h"

/**
 * show_partition_information - print information about a partition
 * @ptab:	part_entry describing the partition
 * @i:		Partition number (UI purposes only)
 * @ptab_root:	part_entry describing the root partition (extended only)
 * @drive_info:	driveinfo struct describing the drive on which the partition
 *		is
 *
 * Note on offsets (from hpa, see chain.c32):
 *
 *  To make things extra confusing: data partition offsets are relative to where
 *  the data partition record is stored, whereas extended partition offsets
 *  are relative to the beginning of the extended partition all the way back
 *  at the MBR... but still not absolute!
 **/
static void show_partition_information(struct driveinfo *drive_info,
				       struct part_entry *ptab,
				       struct part_entry *ptab_root,
				       int offset_root, int data_partitions_seen,
				       int ebr_seen)
{
	char size[8];
	char *parttype;
	int error = 0;
	char *error_buffer;
	unsigned int start, end;

	int i = 1 + ebr_seen * 4 + data_partitions_seen;

	start = ptab->start_lba + ptab_root->start_lba + offset_root;
	end = (ptab->start_lba + ptab_root->start_lba) + ptab->length + offset_root;

	if (ptab->length > 0)
		sectors_to_size(ptab->length, size);
	else
		memset(size, 0, sizeof size);

	get_label(ptab->ostype, &parttype);
	more_printf("  %2d  %s %11d %11d %s %02X %s",
		    i, (ptab->active_flag == 0x80) ? "x" : " ",
		    start,
		    end,
		    size,
		    ptab->ostype, parttype);

	/* Extra info */
	if (ptab->ostype == 0x82 && swsusp_check(drive_info, ptab, &error)) {
		more_printf("%s", " (Swsusp sig. detected)");
	} else if (error) {
		get_error(error, &error_buffer);
		more_printf("%s\n", error_buffer);
		free(error_buffer);
	}

	more_printf("\n");

	free(parttype);
}

void main_show_disk(int argc, char **argv,
		    struct s_hardware *hardware)
{
	reset_more_printf();
	if (!argc) {
		more_printf("Which disk?\n");
		return;
	}

	int drive = strtol(argv[0], (char**) NULL, 16);

	if (drive < 0x80 || drive >= 0xff) {
		more_printf("Invalid disk: %d.\n", drive);
		return;
	}

	int i = drive - 0x80;
	struct driveinfo *d = &hardware->disk_info[i];
	int error;
	char *error_buffer;
	char disk_size[8];

	detect_disks(hardware);
	if (!hardware->disk_info[i].cbios)
		return; /* Invalid geometry */

	if ((int) d->edd_params.sectors > 0)
		sectors_to_size((int) d->edd_params.sectors, disk_size);
	else
		memset(disk_size, 0, sizeof disk_size);

	more_printf("DISK 0x%X:\n", d->disk);
	more_printf("  C/H/S: %d cylinders, %d heads, %d sectors/track\n",
		d->legacy_max_cylinder + 1, d->legacy_max_head + 1,
		d->legacy_sectors_per_track);
	more_printf("  EDD:   Version: %X\n", d->edd_version);
	more_printf("         Size: %s, %d bytes/sector, %d sectors/track\n",
		disk_size,
		(int) d->edd_params.bytes_per_sector,
		(int) d->edd_params.sectors_per_track);
	more_printf("         Host bus: %s, Interface type: %s\n\n",
		remove_spaces(d->edd_params.host_bus_type),
		remove_spaces(d->edd_params.interface_type));

	more_printf("   #  B       Start         End    Size Id Type\n");
	error = 0;
	if (parse_partition_table(d, &show_partition_information, &error)) {
		if (error) {
			more_printf("I/O error: ");
			get_error(error, &error_buffer);
			more_printf("%s\n", error_buffer);
			free(error_buffer);
		} else
			more_printf("An unknown error occured.\n");
		return;
	}
}

void main_show_disks(int argc __unused, char **argv __unused,
		     struct s_hardware *hardware)
{
	reset_more_printf();
	detect_disks(hardware);

	for (int drive = 0x80; drive < 0xff; drive++) {
		char buf[5] = "";
		sprintf(buf, "0x%x", drive);
		char *argv[1] = { buf };
		main_show_disk(1, argv, hardware);
	}
}

void disks_summary(int argc __unused, char** argv __unused,
		   struct s_hardware *hardware)
{
	int i = -1;

	detect_disks(hardware);

	for (int drive = 0x80; drive < 0xff; drive++) {
		i++;
		if (!hardware->disk_info[i].cbios)
			continue; /* Invalid geometry */
		struct driveinfo *d = &hardware->disk_info[i];
		char disk_size[8];

		if ((int) d->edd_params.sectors > 0)
			sectors_to_size((int) d->edd_params.sectors, disk_size);
		else
			memset(disk_size, 0, sizeof disk_size);

		more_printf("DISK 0x%X:\n", d->disk);
		more_printf("  C/H/S: %d cylinders, %d heads, %d sectors/track\n",
			d->legacy_max_cylinder + 1, d->legacy_max_head + 1,
			d->legacy_sectors_per_track);
		more_printf("  EDD:   Version: %X, size: %s\n", d->edd_version,
			disk_size);
		more_printf("         Host bus: %s, Interface type: %s\n\n",
			remove_spaces(d->edd_params.host_bus_type),
			remove_spaces(d->edd_params.interface_type));
	}
}

struct cli_callback_descr list_disk_show_modules[] = {
	{
		.name = "disks",
		.exec = main_show_disks,
	},
	{
		.name = "disk",
		.exec = main_show_disk,
	},
	{
		.name = NULL,
		.exec = NULL,
	},
};


struct cli_module_descr disk_show_modules = {
	.modules = list_disk_show_modules,
	.default_callback = disks_summary,
};

struct cli_mode_descr disk_mode = {
	.mode = DISK_MODE,
	.name = CLI_DISK,
	.default_modules = NULL,
	.show_modules = &disk_show_modules,
	.set_modules = NULL,
};