home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!sgiblab!tsoft!dennis
- From: bbs.dennis@tsoft.sf-bay.org (Dennis Yelle)
- Newsgroups: comp.os.os2.programmer
- Subject: How can I read the MBR (Master Boot Record) under OS/2?
- Message-ID: <N2c9PB2w165w@tsoft.sf-bay.org>
- Date: 28 Aug 92 18:54:34 GMT
- Sender: bbs@tsoft.sf-bay.org (BBS User)
- Organization: The TSoft BBS and Public Access Unix, +1 415 969 8238
- Lines: 119
-
- #if 0
- I am trying to read the Master Boot Record from my C: drive
- under OS/2.
-
- When I compile the following program with:
- icc getmbr.c
- And then run it with:
- getmbr z
- I get this:
- GETMBR
- Got handle 5.
- Sorry, the error code is 4400-19
- Does anyone know what I am doing wrong?
- Can anyone help me?
- #endif
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define INCL_DOSDEVICES
- #include <os2.h>
-
- char mbr_buf[ 512];
-
- void internal_error_x( int code, long rc)
- {
- printf( "Sorry, the error code is %d-%ld\n", code, rc);
- exit(99);
- }
-
- int get_pd_handle( int disk, HFILE *handle)
- {
- char cdisk[3];
- APIRET rc;
-
- cdisk[0] = disk; cdisk[1] = ':'; cdisk[2] = 0;
- *handle = 0;
- rc = DosPhysicalDisk( INFO_GETIOCTLHANDLE,
- handle,
- 2, /* sizeof( *handle), */
- cdisk,
- sizeof( cdisk) );
- if ( rc)
- internal_error_x( 1234, rc);
- return 0;
- }
- int free_pd_handle( HFILE handle)
- {
- APIRET rc;
-
- rc = DosPhysicalDisk( INFO_FREEIOCTLHANDLE, 0, 0,
- &handle, 2 /* sizeof(handle) */ );
- if ( rc)
- internal_error_x( 1245, rc);
- return 0;
- }
-
- typedef unsigned short WORD; /* long didn't work, so... */
-
- typedef struct pp_s {
- BYTE command; /* 1 */
- WORD head; /* 0 */
- WORD cyl; /* 0 */
- WORD first_sector; /* 1 */
- WORD num_sectors; /* 1 */
- WORD sector_num; /* 1 */
- WORD sector_size; /* 512 */
- } pp_t;
-
- void read_mbr( int disk, void *p_buf, ULONG p_buf_len)
- {
- HFILE handle;
- APIRET rc;
- pp_t pp = { 1, 0, 0, 1, 1, 1, 512};
- ULONG pp_len = sizeof( pp);
- ULONG b_len = 0 /* p_buf_len */;
-
- memset( p_buf, 0, p_buf_len);
-
- get_pd_handle( disk, &handle);
-
- printf( "Got handle %ld.\n", (long)handle);
-
- rc = DosDevIOCtl( handle, 9, 0x64 /* read */,
- &pp, sizeof(pp), &pp_len,
- p_buf, p_buf_len, &b_len);
- if ( rc)
- internal_error_x( 4400, rc);
-
- free_pd_handle( handle);
- }
-
- main( int argc, char **argv)
- {
- char *name;
- FILE *out;
- int len;
-
- printf( "GETMBR\n");
- if ( argc != 2 )
- internal_error_x( 5555, argc);
- name = argv[1];
- out = fopen( name, "wb");
- if ( ! out ) {
- printf( "Sorry, I can't write to the file %s\n", name);
- exit(9);
- }
- read_mbr( '1', mbr_buf, sizeof( mbr_buf) );
- len = fwrite( mbr_buf, 1, 512, out);
- if ( len != 512 ) {
- printf( "Sorry, I can't write to the file %s\n", name);
- printf( "Disk full?\n");
- exit(9);
- }
- return 0;
- }
-
- --
- Dennis Yelle (bbs.dennis@tsoft.sf-bay.org)
-