/**************************************************************************************
 Copyright (c) 2002 The UbixOS Project
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors
in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id: main.c,v 1.5 2003/05/01 12:30:02 reddawg Exp $

**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/sys.h>

#define MAXPARTITIONS 4


struct partStruct {
  char active;
  char startHd;
  char startSec;
  char startCyl;
  char pType;
  char endHd;
  char endSec;
  char endCyl;
  long startLba;
  long size;
  };


struct mbrStruct {
  char code[446];
  struct partStruct *parts;
  char sig[2];
  };

struct driveInfo {
  char hdSector[512];
  char hdEnable;
  char hdDev;
  char hdFlags;
  char hdShift;
  long hdMask;
  long hdMulti;
  long hdPort;
  long hdSize;
  long hdCalc;
  };

struct driveDiskLabel {
  uInt32 magicNum;
  uInt32 magicNum2;
  uInt16 driveType;
  uInt16 numPartitions;
  struct  drivePartitions {  //the partition table
    uInt32 pSize;            //number of sectors in partition
    uInt32 pOffset;          //starting sector
    uInt32 pFsSize;          //filesystem basic fragment size
    uInt32 pBatSize;         //BAT size
    uInt8  pFsType;          //filesystem type, see below
    uInt8 pFrag;            //filesystem fragments per block
    } partitions[MAXPARTITIONS];
  };

struct driveDriver {
  struct driveDriver    *prev;
  struct driveDriver    *next;
  struct driveDiskLabel *diskLabel;
  int  id;
  struct driveInfo *driveInfoStruct;
  char driveType;
  void (*read)(void *,long,long,void *);
  void (*write)(void *,long,long,void *);
  void (*reset)(void *);
  };  
  
int main(int argc,char **argv) {
  int i = 0x0;
  char *data = (char *)malloc(512);
  char drive[32];
  struct driveDriver *drives = 0x0,*tmpDrives = 0x0;
  struct partStruct  *parts = (struct partStruct *)malloc(64);
  FILE *mbr,*hdDrive;
  printf("UbixOS Disk Editor\n(C) 2003 Ubix Corp\n\n");
  drives = getDrives();
  while (1) {
    printf("0) Show Disks\n");
    printf("1) Quit\n");
    gets(data);    
    switch (data[0]) {
      case '0':
        printf("Please Choose A Disk\n");
        for (tmpDrives=drives;tmpDrives;tmpDrives=tmpDrives->next) {
          printf("[%i][0x%X][%i]\n",tmpDrives->id,tmpDrives->driveType,((tmpDrives->driveInfoStruct->hdSize*512)/1024));
          }
        gets(data);
        sprintf(drive,"drive%c@devfs",data[0]);
        driveOpts:
        printf("0) Install MBR\n");
        printf("1) Show Partitions\n");
        printf("3) Delete Partition\n");
        printf("4) Add Partition\n");
        gets(data);
        switch (data[0]) {
          case '0':
            printf("Installing MBR\n");
            mbr = fopen("mbr.img@sys","rb");
            printf("[%s]\n",drive);
            hdDrive = fopen(drive,"rb");
            printf("[0x%X]\n",hdDrive);
            fread(data,512,1,mbr);
            fwrite(data,512,1,hdDrive);
            fclose(mbr);
            fclose(hdDrive);
            break;
          case '1':
            hdDrive = fopen(drive,"rb");
            printf("Going To Seek\n");
            fseek(hdDrive,446,0);
            printf("Done Seeking\n");
            fread(parts,64,1,hdDrive);
            for (i=0;i<4;i++) {
              printf("Active  Type  Start C/H/S  End C/H/S  Start LBA  Size\n");
              printf("%i      0x%X  %i/%i/%i     %i/%i/%i   0x%X       0x%X\n",parts->active,(uInt8)parts->pType,parts->startCyl,parts->startHd,parts->startSec,parts->endCyl,parts->endHd,parts->endSec,parts->startLba,parts->size);
              parts++;
              }
            fclose(hdDrive);
            break;
          default:
            printf("Invalid Option\n");
            goto driveOpts;
            break;
          }
        break;
      case '1':
        return(0);
        break;
      default:
        break;
      }
    }
  return(0);
  }
