GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Jun-2006, 04:21
ajeshjohn ajeshjohn is offline
New Member
 
Join Date: Jun 2006
Posts: 3
ajeshjohn is on a distinguished road
Post

Find size of hard disk


Can I anyone tell me how to find the total size of the hard disk using C?

The hard disk may not be formatted. I need to use it before installing the operating system on the hard disk.

I need to do the program in C. Can anyone please help me?
  #2  
Old 20-Jun-2006, 10:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Find size of hard disk


Quote:
Originally Posted by ajeshjohn
Can I anyone tell me how to find the total size of the hard disk using C?

The hard disk may not be formatted. I need to use it before installing the operating system on the hard disk.

I need to do the program in C. Can anyone please help me?

I find this to be an interesting question. (See footnote 1.)

Like most "interesting" questions, there may be lots of answers, but they all depend on other information that must be known before you even think of creating a program (in C or anything else).

The couple of questions that I post below encompass some things to ask (and answer) yourself. I do not intend to take the list of your answers and give you a cookbook formula to success. (I probably can't, and I probably wouldn't, even if I could. If all you want is an answer, you probably will stop reading right about now, and I wouldn't blame you if you did.)

Anyhow, here are a few things that you should write down before writing #include <stdio.h>


1. What is the context of your statement that you "need to do" the program? Is this a class assignment or a research project or some other edificational experience? (If so, what are the prerequisites that you are supposed to have mastered by now. What courses? What programming and programming project knowledge/experience?)

2. What is the context (environment) of the program? I'll expand:

Since the program is going to examine a disk drive before it is formatted, obviously the program is not resident on that drive.

So: is the program going to run on a machine that has another disk drive already formatted and running an operating system from which you can launch your program?

If so: what operating system? Since things like disk accesses are not part of the C or C++ standards, programs that look at your system's hardware must necessarily be implementation-dependent. DOS can access disk drive information. Windows can access disk drive information. Linux can access disk drive information (so can OS/2, BSD, and anything else). Actually, you don't even need an operating system: Your computer's BIOS accesses disk drive information before your operating system is even loaded.

If not (that is, if the new disk drive is attached to a computer that doesn't already have an operating system running), then how is the program going to be running? Is it on a CD (or other medium) or what? If it's on the CD, then what other stuff on the CD has been loaded into the machine's memory to allow the new program to be running?

Maybe you have a CD, like Knoppix, that loads Linux into your computer's memory and then presents you with a Linux command line and/or graphical interface, from which you could read in your disk examination program (from another CD, from a FLASH drive, from the network, ...).

Well, that doesn't make much sense, since if you have an operating system running in your computer's memory ---regardless of how it got there--- then you could use that operating system's disk management tools to find out the disk drive's information (fdisk for Linux, for example). The Windows installation disks load a stripped-down version of Windows before it gets to the part where it evaluates your disk drives and other attached hardware.

So, depending on your answer to question number 2, you may need to revisit question number 1: Why do you "need to write" the program in the first place?

If it's for your education (institutional or on-the-job training or otherwise), and you have no experience with disk drive access, you could try to find some source code somewhere. For example the GNU fdisk program from any Linux distribution performs the task. Since source code is freely available you could look at that. (See footnote 2.)

I have to warn you that looking at Linux source code (or any other significant operating-system type of code--- "low level" or not) may not be the easiest thing you have ever done. Functionality is almost always spread out over a large number of files (some of which are really hard to understand). Low-level stuff like disk drive information of raw (unformatted) disks may very well involve links to architecture-dependent low level code that is even more difficult to follow (some of it may depend on initialization code buried even deeper in the source code tree and even more difficult to understand).

GNU fdisk is actually an interface to the GNU parted (partition editing) library. (See footnote 3.)

One final thing: a quote, and it's too important for a footnote.

"Ya gotta have a plan."
---me

Regards,

Dave

Footnote 1:
"May you live in interesting times."
---Often quoted as an "ancient Chinese curse", but I doubt that there is anything Chinese about it. There is something interesting about it, however. (Or, maybe, not so interesting to you. But if you have some free time:Get a(n interesting) life!)


Footnote 2:
Freely available means, in this case, both "free" as in "free beer" and "free" as in "free speech". Start at http://www.gnu.org or http://linux.slashdot.org or http://www.sourceforge.net or ...
---Free Software Foundation


Footnote 3:

"toebone's connected to the footbone...
footbone's connected to the legbone...
legbone's connected to the thighbone...
etc.

---Bottom-up description of human anatomy from an old Spiritual song.
  #3  
Old 20-Jun-2006, 23:02
ajeshjohn ajeshjohn is offline
New Member
 
Join Date: Jun 2006
Posts: 3
ajeshjohn is on a distinguished road

Re: Find size of hard disk


Hi Dave,

Thanks for the info. I'm sorry to have missed out the details that you asked in the first post.


In fact, I'll boot the system into DOS mode using a CD and run the program in DOS mode using a BAT script. The system will have only one hard disk and it need not be formatted ie. "a new disk drive is attached to a computer that doesn't already have an operating system running".

And there's nothing much that gets loaded into the memory from the CD like Knoppix as you mentioned. And about the UNIX source code, will the program, if made similarly as in UNIX, work in DOS mode? I had gone through the source code "df". But I need to know if it will work in DOS mode before I proceed further.


Thanks in advance...
Aju
  #4  
Old 21-Jun-2006, 08:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Find size of hard disk


Quote:
Originally Posted by ajeshjohn
In fact, I'll boot the system into DOS mode using a CD and run the program in DOS mode using a BAT script.

It sounds interesting, and I wish I knew how to help. Even if I had any specific recommendations, I don't have any way to test them, since I don't have anything for DOS program development. (I can create a DOS floppy boot disk on my Windows XP system, but I hadn't even thought about how one would go about making a bootable DOS CD. Hmmm...)

Here's the thing: if you are going to write a "DOS mode" program in C, what compiler are you using? The reason that I ask is that all hardware-aware programs are necessarily using functions that are not part of the C standard library. That is to say, each compiler writer/vendor chooses a particular set of functions to implement that allows such access. There is no guarantee that any particular vendor will even supply such functions with the product (although, obviously, they had such functions in whatever in-house system they were using for development and testing.)

So, if you have (or can get) any documentation from wherever you got your compiler, you can look. Lacking that, you can poke around in all of the header files (the compler's include directory and subdirectories) and look for functions that may appear to have something to do with hardware access. I can't imagine that there will be anything ready-made for your purposes, but you can look.

I'm guessing that what you want might require access to BIOS routines to access the hardware. Compilers for Windows XP won't have any such things, but, again, depending on your compiler (since it is for DOS) you probably have some kind of BIOS call. Now, of course you have to become familiar with BIOS access to your disk information. Obviously it will be there, but I'm not sure things are set up the way they used to be when we used int13 to get to the disk in DOS programs (in a time long, long ago, in a galaxy far, far away). If you are familiar with something like djgpp, you may be able to find projects on sourceforge or other source project sites.

Quote:
Originally Posted by ajeshjohn
And about the UNIX source code, will the program, if made similarly as in UNIX, work in DOS mode?
You will need the same functions in your library that the UNIX code used. Since DOS C compilers that I used to use had 16-bit integers, there are probably lots of places where UNIX code won't work without some typedefs or other tweaking, accompanied by massive find/replace editing in the source code. Not to mention limits on sizes (uses of near pointers and far pointers by these old 8088/8086 code generators that had addressing limits of either 64 kBytes or a MegaByte or so).

Quote:
Originally Posted by ajeshjohn
I had gone through the source code "df". But I need to know if it will work in DOS mode before I proceed further.
I am impressed. Every time I try to look at Linux source code my brain goes into overdrive; my pulse rate increases dangerously, and the old "fight or flight" adrenaline rush takes over and threatens to either:

1. Shut down all non-critical systems and enter minimum-maintenance subsistence mode.
or
2. Direct my feet to the nearest public house.

The "df" program on my Linux systems specifically lists disk space available on all mounted file systems. Note that in Windows parlance, the UNIX/Linux term "mounted file systems" in reference to hard drives means that the disk has been partitioned and formatted and has been mounted by the operating system.

The fdisk program (and its big brother, parted) look at disk capacity in order to allow creation editing of disk partitions. I haven't tried to look at its source code (I'm not sure my tired old nervous system and cardiovascular system could tolerate it.)


It is entirely possible that someone else reading this thread can offer more substantial help. I hope so. I would be literally thrilled to see more information about it. Maybe there are some PC/XT design engineers out there who haven't been confined to rubber rooms, and they can give us some clues.

I wish you well, and if you ever get any questions that I could actually help with, I would like to hear from you. Actually I would like to hear about your progress even (especially) if you don't need help.


Regards,

Dave
  #5  
Old 23-Jun-2006, 02:15
ajeshjohn ajeshjohn is offline
New Member
 
Join Date: Jun 2006
Posts: 3
ajeshjohn is on a distinguished road

Re: Find size of hard disk


Hi Dave,

Your reply was really inspirational. And I think I'm inching forward to finding a solution.

It's easy to make a bootable DOS CD. you just have to make an image file and include it in the CD image. The system automatically boots from the CD and "autoexec.bat" is executed first.

The compiler that i use is Turbo C. And as you said, there are no in-built functions that could be useful to me.

But I think i have found a solution. I'm using int13 Extensions.

Well, I found in the internet this soltion. But the new problem that i'm facing now is that, I get the same value in different machines!!!

I'm still trying.
I'm supposed to give the buffer address to the stack segment register.
I'm not able to do that.
Here's the code segment.

CPP / C++ / C Code:
int getDriveParams(int drv)
{
  union REGS in, out;
  int intCylinderNo, intSectorNo, intHeadNo, intNoofDrives;

  in.x.ax = 0x4800;
  in.h.dl = (unsigned short int) drv;
  in.x.si = (unsigned) &out;
  in.x.di = 0;

  int86 (0x13, &in, &out);

  if (out.x.cflag & 1)
    return out.h.ah;

  intCylinderNo = 0;
  intSectorNo = getBits(out.h.cl, 5, 6);
  intHeadNo = out.h.dh;
  intNoofDrives = out.h.dl;

  printf("Cylinders: %d\tSectors: %d\tHeads: %d\tDrives: %d\n\n",
         intCylinderNo, intSectorNo, intHeadNo, intNoofDrives);
 return 0;
}

I've assigned the buffer like this

CPP / C++ / C Code:
 in.x.si = (unsigned) &out;

It's not working.

Later in the code, this statement
CPP / C++ / C Code:
int86 (0x13, &in, &out);
fills out, but the values are the same for each and every machine.


Here's the whole program,
CPP / C++ / C Code:
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>

#define BUFF_SIZE 0x10000				/* Buffer Size */

/* Gets the specified number of bits from a number */
int getBits(int intNumber, int intPosition, int intNumOfBits)
{
	return (intNumber >> (intPosition + 1 - intNumOfBits )) & ~(~0 << intNumOfBits);
}


/* returns -1 if installed,  0 otherwise */
int checkBiosSupport()
{
  union REGS setValues, getValues;
  setValues.x.ax = 0x4100;
  setValues.x.bx = 0x55AA;
  setValues.h.dl = 0x80;
  int86 (0x13, &setValues, &getValues);

  if (getValues.x.flags & 1) return 0;
  if (getValues.x.bx != 0xAA55) return 0;
  return -1;
}

/* returns 0 if all goes ok, >0 int13 error */
int getDriveParams(int drv)
{
  union REGS in, out;
  int intCylinderNo, intSectorNo, intHeadNo, intNoofDrives;

  in.x.ax = 0x4800;
  in.h.dl = (unsigned short int) drv;
  in.x.si = (unsigned) &out;
  in.x.di = 0;

  int86 (0x13, &in, &out);

  if (out.x.cflag & 1)
    return out.h.ah;

  intCylinderNo = 0;
  intSectorNo = getBits(out.h.cl, 5, 6);
  intHeadNo = out.h.dh;
  intNoofDrives = out.h.dl;

  printf("Cylinders: %d\tSectors: %d\tHeads: %d\tDrives: %d\n\n",
         intCylinderNo, intSectorNo, intHeadNo, intNoofDrives);

  return 0;
}

int main()
{
  if (!checkBiosSupport())
  {
    printf("No BIOS int13 extensions support. Exiting.\n");
    exit(1);
  }
  else
    printf("BIOS int13 extensions are supported.\n");

  if (getDriveParams(0x80) != 0)
	  printf("int13 error\n");
  return 0;
}
Last edited by LuciWiz : 23-Jun-2006 at 02:21. Reason: Please insert your C code between [c] & [/c] tags
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard disk serial number ameynirgude C Programming Language 1 20-Nov-2005 05:55
How to Remove Host Protected Area (HPA) of (ATA) Hard disk in windows with API calls? nagamohan_p MS Visual C++ / MFC Forum 0 27-Sep-2005 23:27
i can't find the wrong!! small_ticket C++ Forum 2 20-Sep-2004 23:05
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 12:17
Hard disk problem!! jack21 Computer Hardware Forum 2 17-May-2004 10:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.