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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/* -*- c -*- ------------------------------------------------------------- *
*
* Copyright 2003-2008 H. Peter Anvin - All Rights Reserved
* Portions copyright 2010 Shao Miller
*
* 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, Inc., 53 Temple Place Ste 330,
* Boston MA 02111-1307, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/*
* mdiskchk.c
*
* DOS program to check for the existence of a memdisk.
*
* This program can be compiled for DOS with the OpenWatcom compiler
* (http://www.openwatcom.org/):
*
* wcl -3 -osx -mt mdiskchk.c
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <i86.h> /* For MK_FP() */
typedef unsigned long uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
/* Pull in MEMDISK common structures */
#include "../memdisk/mstructs.h"
struct memdiskinfo {
struct mdi mdi;
/* We add our own fields at the end */
int cylinders;
int heads;
int sectors;
};
struct memdiskinfo *query_memdisk(int drive)
{
static struct memdiskinfo mm;
uint32_t _eax, _ebx, _ecx, _edx;
uint16_t _es, _di;
unsigned char _dl = drive;
uint16_t bytes;
__asm {
.386;
mov eax, 454d0800h;
mov ecx, 444d0000h;
mov edx, 53490000h;
mov dl, _dl;
mov ebx, 3f4b0000h;
int 13h;
mov _eax, eax;
mov _ecx, ecx;
mov _edx, edx;
mov _ebx, ebx;
mov _es, es;
mov _di, di;
}
if (_eax >> 16 != 0x4d21 ||
_ecx >> 16 != 0x4d45 || _edx >> 16 != 0x4944 || _ebx >> 16 != 0x4b53)
return NULL;
memset(&mm, 0, sizeof mm);
bytes = *(uint16_t far *) MK_FP(_es, _di);
/* 27 is the most we know how to handle */
if (bytes > 27)
bytes = 27;
_fmemcpy((void far *)&mm, (void far *)MK_FP(_es, _di), bytes);
mm.cylinders = ((_ecx >> 8) & 0xff) + ((_ecx & 0xc0) << 2) + 1;
mm.heads = ((_edx >> 8) & 0xff) + 1;
mm.sectors = (_ecx & 0x3f);
return &mm;
}
const char *bootloadername(uint8_t id)
{
static const struct {
uint8_t id, mask;
const char *name;
} *lp, list[] = {
{0x00, 0xf0, "LILO"},
{0x10, 0xf0, "LOADLIN"},
{0x31, 0xff, "SYSLINUX"},
{0x32, 0xff, "PXELINUX"},
{0x33, 0xff, "ISOLINUX"},
{0x34, 0xff, "EXTLINUX"},
{0x30, 0xf0, "SYSLINUX family"},
{0x40, 0xf0, "Etherboot"},
{0x50, 0xf0, "ELILO"},
{0x70, 0xf0, "GrUB"},
{0x80, 0xf0, "U-Boot"},
{0xA0, 0xf0, "Gujin"},
{0xB0, 0xf0, "Qemu"},
{0x00, 0x00, "unknown"}
};
for (lp = list;; lp++) {
if (((id ^ lp->id) & lp->mask) == 0)
return lp->name;
}
}
/* The function type for an output function */
#define OUTPUT_FUNC_DECL(x) \
void x(const int d, const struct memdiskinfo * const m)
typedef OUTPUT_FUNC_DECL((*output_func));
/* Show MEMDISK information for the passed structure */
static OUTPUT_FUNC_DECL(normal_output)
{
if (m == NULL)
return;
printf("Drive %02X is MEMDISK %u.%02u:\n"
"\tAddress = 0x%08lx, len = %lu sectors, chs = %u/%u/%u,\n"
"\tloader = 0x%02x (%s),\n"
"\tcmdline = %Fs\n",
d, m->mdi.version_major, m->mdi.version_minor,
m->mdi.diskbuf, m->mdi.disksize, m->cylinders, m->heads, m->sectors,
m->mdi.bootloaderid, bootloadername(m->mdi.bootloaderid),
MK_FP(m->mdi.cmdline.seg_off.segment,
m->mdi.cmdline.seg_off.offset));
}
/* Yield DOS SET command(s) as output for each MEMDISK kernel argument */
static OUTPUT_FUNC_DECL(batch_output)
{
if (m != NULL) {
char buf[256], *bc;
const char far *c =
MK_FP(m->mdi.cmdline.seg_off.segment,
m->mdi.cmdline.seg_off.offset);
const char *have_equals, is_set[] = "=1";
while (*c != '\0') {
/* Skip whitespace */
while (isspace(*c))
c++;
if (*c == '\0')
/* Trailing whitespace. That's enough processing */
break;
/* Walk the kernel arguments while filling the buffer,
* looking for space or NUL or checking for a full buffer
*/
bc = buf;
have_equals = is_set;
while ((*c != '\0') && !isspace(*c) &&
(bc < &buf[sizeof(buf) - 1])) {
/* Check if the param is "x=y" */
if (*c == '=')
/* "=1" not needed */
have_equals = &is_set[sizeof(is_set) - 1];
*bc = *c;
c++;
bc++;
}
/* Found the end of the parameter and optional value sequence */
*bc = '\0';
printf("set %s%s\n", buf, have_equals);
}
}
}
/* We do not output batch file output by default. We show MEMDISK info */
static output_func show_memdisk = normal_output;
/* A generic function type */
#define MDISKCHK_FUNC_DECL(x) \
void x(void)
typedef MDISKCHK_FUNC_DECL((*mdiskchk_func));
static MDISKCHK_FUNC_DECL(do_nothing)
{
return;
}
static MDISKCHK_FUNC_DECL(show_usage)
{
printf("\nUsage: mdiskchk [--safe-hooks] [--mbfts] [--batch-output]\n"
"\n"
"Action: --safe-hooks . . Will scan INT 13h \"safe hook\" chain\n"
" --mbfts . . . . Will scan memory for MEMDISK mBFTs\n"
" --batch-output . Will output SET command output based\n"
" on MEMDISK kernel arguments\n"
" --no-sequential Suppresses probing all drive numbers\n");
}
/* Search memory for mBFTs and report them via the output method */
static MDISKCHK_FUNC_DECL(show_mbfts)
{
const uint16_t far * const free_base_mem =
MK_FP(0x0040, 0x0013);
int seg;
uint8_t chksum;
uint32_t i;
const struct mBFT far *mbft;
struct memdiskinfo m;
struct patch_area far *patch_area;
for (seg = *free_base_mem / 16; seg < 0x9FFF; seg++) {
mbft = MK_FP(seg, 0);
/* Check for signature */
if (mbft->acpi.signature[0] != 'm' ||
mbft->acpi.signature[1] != 'B' ||
mbft->acpi.signature[2] != 'F' ||
mbft->acpi.signature[3] != 'T')
continue;
if (mbft->acpi.length != sizeof(struct mBFT))
continue;
/* Check sum */
chksum = 0;
for (i = 0; i < sizeof(struct mBFT); i++)
chksum += ((const uint8_t far *)mbft)[i];
if (chksum)
continue;
/* Copy the MDI from the mBFT */
_fmemcpy((void far *)&m, &mbft->mdi, sizeof(struct mdi));
/* Adjust C/H/S since we actually know
* it directly for any MEMDISK with an mBFT
*/
patch_area = (struct patch_area far *)&mbft->mdi;
m.cylinders = patch_area->cylinders;
m.heads = patch_area->heads;
m.sectors = patch_area->sectors;
show_memdisk(patch_area->driveno, &m);
}
}
/* Walk the "safe hook" chain as far as possible
* and report MEMDISKs that we find via the output method
*/
static MDISKCHK_FUNC_DECL(show_safe_hooks)
{
const real_addr_t far * const int13 =
MK_FP(0x0000, 0x0013 * sizeof(real_addr_t));
const struct safe_hook far *hook =
MK_FP(int13->seg_off.segment, int13->seg_off.offset);
while ((hook->signature[0] == '$') &&
(hook->signature[1] == 'I') &&
(hook->signature[2] == 'N') &&
(hook->signature[3] == 'T') &&
(hook->signature[4] == '1') &&
(hook->signature[5] == '3') &&
(hook->signature[6] == 'S') &&
(hook->signature[7] == 'F')) {
/* Found a valid "safe hook" */
if ((hook->vendor[0] == 'M') &&
(hook->vendor[1] == 'E') &&
(hook->vendor[2] == 'M') &&
(hook->vendor[3] == 'D') &&
(hook->vendor[4] == 'I') &&
(hook->vendor[5] == 'S') &&
(hook->vendor[6] == 'K')) {
/* Found a valid MEMDISK "safe hook". It will have an mBFT */
const struct mBFT far *mbft;
struct memdiskinfo m;
struct patch_area far *patch_area;
/* Copy the MDI from the mBFT. Offset is a misnomer here */
mbft = MK_FP(hook->mbft >> 4, 0); /* Always aligned */
_fmemcpy((void far *)&m, &mbft->mdi, sizeof(struct mdi));
/* Adjust C/H/S since we actually know
* it directly for any MEMDISK with an mBFT
*/
patch_area = (struct patch_area far *)&mbft->mdi;
m.cylinders = patch_area->cylinders;
m.heads = patch_area->heads;
m.sectors = patch_area->sectors;
show_memdisk(patch_area->driveno, &m);
} /* if */
/* Step to the next hook in the "safe hook" chain */
hook = MK_FP(hook->old_hook.seg_off.segment,
hook->old_hook.seg_off.offset);
} /* while */
}
int main(int argc, char *argv[])
{
int d;
int found = 0;
int sequential_scan = 1; /* Classic behaviour */
const struct memdiskinfo *m;
/* Default behaviour */
mdiskchk_func usage = do_nothing,
safe_hooks = do_nothing,
mbfts = do_nothing;
/* For each argument */
while (--argc) {
/* Argument should begin with one of these chars */
if ((*argv[argc] != '/') && (*argv[argc] != '-')) {
/* It doesn't. Print usage soon */
usage = show_usage;
break;
}
argv[argc]++;
/* Next char might be '-' as in "--safe-hooks" */
if (*argv[argc] == '-')
argv[argc]++;
switch (*argv[argc]) {
case 'S':
case 's':
safe_hooks = show_safe_hooks;
break;
case 'M':
case 'm':
mbfts = show_mbfts;
break;
case 'B':
case 'b':
show_memdisk = batch_output;
break;
case 'N':
case 'n':
sequential_scan = 0;
break;
default:
usage = show_usage;
} /* switch */
} /* while */
safe_hooks();
mbfts();
if (!sequential_scan)
goto skip_sequential;
for (d = 0; d <= 0xff; d++) {
m = query_memdisk(d);
if (m != NULL) {
found++;
show_memdisk(d, m);
}
}
skip_sequential:
usage();
return found;
}
|