Index: lost/trunk/src/modules/c/fdisk/partition.h =================================================================== --- lost/trunk/src/modules/c/fdisk/partition.h (revision 0) +++ lost/trunk/src/modules/c/fdisk/partition.h (revision 0) @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2008 The LOST Project. All rights reserved. + * + * This code is derived from software contributed to the LOST Project + * by Martin Kleusberg. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the LOST Project + * and its contributors. + * 4. Neither the name of the LOST 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 HOLDERS 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. + */ + +#ifndef __FDISK_PARTITION_H__ +#define __FDISK_PARTITION_H__ + +#include + +#define DISK_SIGNATURE_START 0x1B8 +#define PARTITION_TABLE_START 0x1BE +#define BOOT_SIGNATURE_POS 0x1FE +#define BOOT_SIGNATURE 0xAA55 + +#define SECTORS_PER_TRACK 63 + +#define BOOTFLAG_SET 0x80 +#define BOOTFLAG_UNSET 0x00 + +#define DEFAULT_PARTITION_TYPE 0x83 +#define DEFAULT_BOOTABLE_FLAG BOOTFLAG_UNSET + +#define PARTITION_TYPE_PRIMARY 0 +#define PARTITION_TYPE_EXTENDED 1 +#define PARTITION_TYPE_LOGICAL 2 + +/* Ein Zeiger auf argv[1] oder besser gesagt den Laufwerksnamen */ +char *device_name; +/* Anzahl der Heads */ +unsigned long device_numheads; + +/* Ein einzelner Eintrag der Partitionstabelle, wie er auch auf der Festplatte + vorkommt +*/ +struct partition_entry_t +{ + uint8_t bootable; /* Bootflag (0x00 nicht bootable, 0x80 bootable) */ + uint8_t start[3]; /* Startposition im CHS Format */ + uint8_t type; /* Partitionstyp */ + uint8_t end[3]; /* Endposition im CHS Format */ + uint32_t start_sector; /* LBA Startposition */ + uint32_t num_sectors; /* Anzahl der Sektoren in der Partition */ +} __attribute__ ((__packed__)); + +/* Alle Informationen über eine Partition, die fdisk benötigt */ +struct partition_t +{ + int exists; /* 1, wenn die Partition existiert, 0 wenn nicht */ + unsigned int number; /* Nummer der Partition, beginnend mit 1 */ + int type; /* 0 = primary, 1 = extended, 2 = logical */ + + struct partition_entry_t data; /* Die eigentlichen Daten */ +}; + +/* Berechnet den Endsektor einer Partition */ +long getEndSector(struct partition_t *p); +/* Gibt die Disk Signature des zurück */ +uint32_t getDiskSignature(); +/* Zerlegt die 3 Byte großen CHS Einträge */ +void uncompressCHS(uint8_t *compressed, unsigned int *cylinder, unsigned int *head, unsigned int *sector); +/* Erstellt die 3 Byte großen CHS Einträge */ +void compressCHS(uint8_t *compressed, unsigned int cylinder, unsigned int head, unsigned int sector); +/* Konvertiert CHS nach LBA */ +unsigned int CHS2LBA(unsigned int cylinder, unsigned int head, unsigned int sector); +/* Konvertiert LBA nach CHS */ +void LBA2CHS(unsigned int lba, unsigned int *cylinder, unsigned int *head, unsigned int *sector); +/* Partitionsdaten von der Fesplatte lesen. Gibt 0 zurück, wenn erfolgreich */ +int readPartitions(char *filename); +/* Neuen MBR erstellen und auf Festplatte schreiben. Gibt 0 zurück, wenn erfolgreich */ +int applyChanges(); +/* Löscht eine Partition */ +void deletePartition(struct partition_t *p); +/* Erstellt eine neue Partition */ +void createPartition(struct partition_t *p, unsigned int number, unsigned int start_lba, unsigned int end_lba); + +/* Kopie des MBR und Array für die Primary Partitions */ +unsigned char MBR[512]; +struct partition_t MBR_Partitions[4]; + +#endif Index: lost/trunk/src/modules/c/fdisk/fdisk.c =================================================================== --- lost/trunk/src/modules/c/fdisk/fdisk.c (revision 0) +++ lost/trunk/src/modules/c/fdisk/fdisk.c (revision 0) @@ -0,0 +1,462 @@ +/* + * Copyright (c) 2008 The LOST Project. All rights reserved. + * + * This code is derived from software contributed to the LOST Project + * by Martin Kleusberg. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the LOST Project + * and its contributors. + * 4. Neither the name of the LOST 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 HOLDERS 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. + */ + +#include +#include +#include +#include +#include +#define _USE_START_ +#include + +#include "partition.h" +#include "typeids.h" + +/* Diese Variable wird auf 1 gesetzt, wenn Änderungen vorgenommen wurden, + und sobald diese auf die Festplatte geschrieben wurden, auf 0 zurück + gesetzt + */ +int unsaved_changes = 0; + +/* Diese Funktion wird nur vorrübergehend benötigt, da getchar() nicht + auf einen Tastendruck wartet. + TODO Entfernen, sobald getchar wie erwartet funktioniert +*/ +char keyboard_read_char() +{ + fflush(stdout); + char c = 0; + while(!fread(&c, 1, 1, stdin)); + putchar(c); + return c; +} + +/* Diese Funktion konvertiert 2 chars in einen 8bit Integer Wert, + z.B.: c1 = F und c2 = F => Ergebis = 255. + TODO Entfernen, sobald sscanf implementiert ist +*/ +unsigned char hex2uint8(char c1, char c2) +{ + unsigned char temp = 0; + char ret_val = 0; + + if(c1 >= '0' && c1 <= '9') + temp = c1 - '0'; + if(c1 >= 'A' && c1 <= 'F') + temp = c1 - 'A' + 10; + if(c1 >= 'a' && c1 <= 'f') + temp = c1 - 'a' + 10; + ret_val = temp * 16; + + if(c2 >= '0' && c2 <= '9') + temp = c2 - '0'; + if(c2 >= 'A' && c2 <= 'F') + temp = c2 - 'A' + 10; + if(c2 >= 'a' && c2 <= 'f') + temp = c2 - 'a' + 10; + ret_val += temp; + + return ret_val; +} + +void shell_list_partitions() +{ + /* Allgemeine Daten und Statistiken */ + printf("%s\n", device_name); + printf(" Disk Signature: 0x%-08x\n\n", getDiskSignature()); + + /* Kopfzeile der Tabelle */ + printf("# | Start | End | Boot | Type\n"); + + /* TODO extended/logical partitions unterstützen */ + + /* Partitionen auflisten */ + int i; + for(i=0;i<4;i++) + { + /* Nur dann was anzeigen, wenn die Partition auch existiert */ + if(MBR_Partitions[i].exists) + printf("%2i|%12i|%12i| %c | %02x\n", MBR_Partitions[i].number, MBR_Partitions[i].data.start_sector, getEndSector(&MBR_Partitions[i]), + MBR_Partitions[i].data.bootable == BOOTFLAG_SET ? '*' : ' ', (int)MBR_Partitions[i].data.type); + } +} + +void shell_toggle_bootable() +{ + /* TODO extended/logical partitions unterstützen */ + + /* Den User nach der Partitionsnummer fragen. Den passenden Array Index kann man + erhält man durch Subtraktion von 1 */ + printf("Which partition (0 to cancel)? "); + int partition = (int)(keyboard_read_char()-'0') - 1; + printf("\n"); + + /* Abbrechen? */ + if(partition == -1) + return; + + /* Eingabe überprüfen. Die Nummer muss gültig sein, und die dazugehörige Partition soll + exisitieren */ + if(partition < 0 || partition > 3 || MBR_Partitions[partition].exists == 0) + { + printf("Invalid partition\n"); + } else { + if(MBR_Partitions[partition].data.bootable == BOOTFLAG_UNSET) + { + MBR_Partitions[partition].data.bootable = BOOTFLAG_SET; + } else if(MBR_Partitions[partition].data.bootable == BOOTFLAG_SET) { + MBR_Partitions[partition].data.bootable = BOOTFLAG_UNSET; + } else { + printf("WARNING: The bootable flag was invalid! Maybe the partition table is corrupted!\n"); + printf(" It's now set to not bootable.\n"); + MBR_Partitions[partition].data.bootable = BOOTFLAG_UNSET; + } + unsaved_changes = 1; + } +} + +void shell_write() +{ + /* Sollen die Änderungen wirklich auf die HDD geschrieben werden? */ + printf("WARNING: You should be absoulutely sure what you do! You may destroy your data and it's hard to undo this step.\n"); + printf("Continue? [Yn] "); + char answer = keyboard_read_char(); + printf("\n"); + + if(answer == 'Y') + { + if(!applyChanges()) + { + printf("Saved. You should restart your computer now.\n"); + unsaved_changes = 0; + } + } +} + +void shell_delete() +{ + /* Den User nach der Partitionsnummer fragen. Den passenden Array Index kann man + erhält man durch Subtraktion von 1 */ + printf("Which partition (0 to cancel)? "); + int partition = (int)(keyboard_read_char()-'0') - 1; + printf("\n"); + + /* TODO extended/logical partitions unterstützen */ + + /* Abbrechen? */ + if(partition == -1) + return; + + /* Eingabe überprüfen. Die Nummer muss gültig sein, und die dazugehörige Partition + soll (noch) exisitieren */ + if(partition < 0 || partition > 3 || MBR_Partitions[partition].exists == 0) + { + printf("Invalid partition\n"); + } else { + deletePartition(&MBR_Partitions[partition]); + unsaved_changes = 1; + } +} + +void shell_typelist() +{ + int i; + int types_printed = 0; + for(i=0;i<256;i++) + { + if(PartitionTypeID[i].name != 0) + { + /* 3 Typen pro Zeile anzeigen. Um diesen Wert zu ändern, muss die 15 + im printf String und die 3 in der if Abfrage geändert werden */ + printf("%02x %-15s", i, PartitionTypeID[i].name); + if(((++types_printed) % 3 == 0) || (i == 255)) + printf("\n"); + else + printf(" - "); + } + } +} + +void shell_settype() +{ + /* Den User nach der Partitionsnummer fragen. Den passenden Array Index kann man + erhält man durch Subtraktion von 1 */ + printf("Which partition (0 to cancel)? "); + int partition = (int)(keyboard_read_char()-'0') - 1; + printf("\n"); + + /* TODO extended/logical partitions unterstützen */ + + /* Abbrechen? */ + if(partition == -1) + return; + + /* Eingabe überprüfen. Die Nummer muss gültig sein, und die dazugehörige Partition + muss exisitieren */ + if(partition < 0 || partition > 3 || MBR_Partitions[partition].exists == 0) + { + printf("Invalid partition\n"); + } else { + /* TODO Die Eingabe sollte komfortabler werden. Daher muss das Eingabeformat + eingeschränkt werden und eine Suchfunktion bzw. etwas vergleichbares + intigiert werden + */ + /* Den User solange nach einer neuen ID fragen, wie er eine ungültige eingibt */ + char good_input = 0; + char *input; + do + { + input = readline("Please enter the new type as two digit hex code or h for help: "); + if(strcmp(input, "h") == 0) + { + shell_typelist(); + free(input); + } else if(isxdigit(input[0]) && isxdigit(input[1])) { + good_input = 1; + } else { + free(input); + } + } while(good_input == 0); + /* Die Eingabe konvertieren und den Partitionseintrag entsprechend abändern */ + MBR_Partitions[partition].data.type = hex2uint8(input[0], input[1]); + free(input); + + unsaved_changes = 1; + } +} + +void shell_new() +{ + /* TODO extended/logical partitions unterstützen */ + /* TODO Funktion etwas aufräumen */ + + /* Den User nach der Partitionsnummer fragen. Den passenden Array Index kann man + erhält man durch Subtraktion von 1 */ + printf("Which partition (0 to cancel)? "); + int partition = (int)(keyboard_read_char()-'0') - 1; + printf("\n"); + + /* Abbrechen? */ + if(partition == -1) + return; + + /* Eingabe überprüfen. Die Nummer muss gültig sein, und die dazugehörige Partition + darf noch nicht exisitieren */ + if(partition < 0 || partition > 3 || MBR_Partitions[partition].exists != 0) + { + printf("Invalid partition number\n"); + } else { + /* TODO Nur gültige Werte akzeptieren, so dass es nicht mehr möglich ist, + Partitionen zu erstellen, die zu groß sind, nicht vollständig auf + der Festplatte liegen, die sich mit anderen Partitionen überlappen + oder die ihren Startpunkt nach ihrem Ende haben + */ + + /* In welchem Format soll die Startadresse eingegeben werden */ + printf("Start Position: CHS or LBA? [CL] "); + char input_type = 0; + do + { + printf("\b"); /* NOTE Diese Zeile sollte eigentlich das Zeichen von der letzten (falschen) + Eingabe entfernen. Das \b funktioniert momentan aber nicht richtig */ + input_type = keyboard_read_char(); + } while(input_type != 'C' && input_type != 'c' && input_type != 'L' && input_type != 'l'); + printf("\n"); + + /* Start Adresse */ + unsigned int start_lba; + if(input_type == 'C' || input_type == 'c') + { + /* User will Adresse im CHS Format eingeben */ + char *input = readline("Start Position: Cylinder? "); + unsigned int start_cylinder = atoi(input); + free(input); + input = readline("Start Position: Head? "); + unsigned int start_head = atoi(input); + free(input); + input = readline("Start Position: Sector? "); + unsigned int start_sector = atoi(input); + free(input); + + start_lba = CHS2LBA(start_cylinder, start_head, start_sector); + } else { + /* Eingabe im LBA Format */ + char *input = readline("Start Position: LBA Address? "); + start_lba = atoi(input); + free(input); + } + + /* Soll die Endadresse im CHS oder im LBA Format eingegeben werden? Oder anhand der + Größe berechnet werden? */ + printf("End Position: CHS, LBA or calculate from size? [CLS] "); + input_type = 0; + do + { + printf("\b"); + input_type = keyboard_read_char(); + } while(input_type != 'C' && input_type != 'c' && input_type != 'L' && input_type != 'l' && input_type != 'S' && input_type != 's'); + printf("\n"); + + /* Endposition bzw. Größe ermitteln */ + unsigned int end_lba; + if(input_type == 'C' || input_type == 'c') + { + /* User will Adresse im CHS Format eingeben */ + char *input = readline("End Position: Cylinder? "); + unsigned int end_cylinder = atoi(input); + free(input); + input = readline("End Position: Head? "); + unsigned int end_head = atoi(input); + free(input); + input = readline("End Position: Sector? "); + unsigned int end_sector = atoi(input); + free(input); + + end_lba = CHS2LBA(end_cylinder, end_head, end_sector); + } else if(input_type == 'L' || input_type == 'l') { + /* Eingabe im LBA Format */ + char *input = readline("End Position: LBA Address? "); + end_lba = atoi(input); + free(input); + } else { + /* Endposition anhand der Partitionsgröße berechnen */ + char *input = readline("End Position: Partition size (in sectors)? "); + end_lba = start_lba + atoi(input); + free(input); + } + + /* Neue Partition erstellen */ + createPartition(&MBR_Partitions[partition-1], partition, start_lba, end_lba); + + unsaved_changes = 1; + } +} + +void shell() +{ + printf("Type h for help\n"); + + while(1) + { + /* Nächsten Befehl holen */ + char *input = readline("> "); + + /* Eingegebenen Befehl ermitteln */ + if(strcmp(input, "h") == 0) /* Hilfe */ + { + /* NOTE Das \t funktioniert momentan nicht */ + printf("Type one of the following commands:\n"); + printf("b\t\t\tToggle bootable flag\n"); + printf("d\t\t\tDelete partition\n"); + printf("h\t\t\tHelp\n"); + printf("l\t\t\tList partitions\n"); + printf("n\t\t\tCreate a new partition\n"); + printf("q\t\t\tQuit\n"); + printf("t\t\t\tChange a partition's type\n"); + printf("tl\t\t\tList all partition types\n"); + printf("w\t\t\tWrite changes to disk\n"); + } else if(strcmp(input, "q") == 0) /* Beenden */ + { + if(unsaved_changes == 1) + { + printf("You are about to quit. Any unsaved changes will be lost, do you want to continue? [Yn] "); + char answer = keyboard_read_char(); + printf("\n"); + if(answer == 'Y') + { + free(input); + break; + } + } else { + free(input); + break; + } + } else if(strcmp(input, "l") == 0) /* Partitionen auflisten */ + { + shell_list_partitions(); + } else if(strcmp(input, "b") == 0) /* Bootable Flag ändern */ + { + shell_toggle_bootable(); + } else if(strcmp(input, "w") == 0) /* Änderungen speichern */ + { + shell_write(); + } else if(strcmp(input, "d") == 0) /* Partition löschen */ + { + shell_delete(); + } else if(strcmp(input, "t") == 0) /* Change partition type */ + { + shell_settype(); + } else if(strcmp(input, "n") == 0) /* Neue Partition erstellen */ + { + shell_new(); + } else if(strcmp(input, "tl") == 0) /* Partitionstypen auflisten */ + { + shell_typelist(); + } else { + printf("Unknown command (h for help)\n"); + } + + free(input); + } +} + +int main(int argc, char *argv[]) +{ + printf("fdisk\nThis program is in development. Be careful!\n\n"); + + /* Command Line überprüfen */ + if(argc != 2) + { + printf("Usage: fdisk device\n"); + printf("For example: fdisk ata:/ata00\n"); + return 0; + } + + /* Aktuelle Partitionierung einlesen */ + if(readPartitions(argv[1])) + return 1; + + /* Anzahl der Heads erfragen */ + /* TODO Automatisch erledigen */ + char *input = readline("Please enter the number of heads: "); + device_numheads = atoi(input); + free(input); + + /* Initialisierung fertig. Starte den interaktiven Teil */ + shell(); + + /* Beenden, kein Fehler aufgetreten */ + return 0; +} Index: lost/trunk/src/modules/c/fdisk/typeids.c =================================================================== --- lost/trunk/src/modules/c/fdisk/typeids.c (revision 0) +++ lost/trunk/src/modules/c/fdisk/typeids.c (revision 0) @@ -0,0 +1,300 @@ +/* + * Copyright (c) 2008 The LOST Project. All rights reserved. + * + * This code is derived from software contributed to the LOST Project + * by Martin Kleusberg. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the LOST Project + * and its contributors. + * 4. Neither the name of the LOST 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 HOLDERS 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. + */ + +#include "typeids.h" + +struct PartitionTypeID_t PartitionTypeID[257] = + /* Siehe http://www.win.tue.nl/~aeb/partitions/partition_types-1.html */ + /* TODO Einige Einträge haben einen zu langen Namen, Die maximale Länge + sollte bei ca 15 liegen */ + { + {0x00, "No Part."}, + {0x01, "FAT12"}, + {0x02, "XENIX root"}, + {0x03, "XENIX user"}, + {0x04, "FAT16 <32MB"}, + {0x05, "Extended"}, + {0x06, "FAT16 >32MB"}, + {0x07, "NTFS/HPFS"}, + {0x08, "AIX"}, + {0x09, "AIX bootable"}, + {0x0A, "OS/2 Boot Manager"}, + {0x0B, "FAT32"}, + {0x0C, "FAT32 (LBA)"}, + {0x0D, 0}, + {0x0E, "FAT16 (LBA)"}, + {0x0F, "W95 Extended (LBA)"}, + {0x10, "OPUS"}, + {0x11, "FAT12 (Hidden)"}, + {0x12, "Diagnostic"}, + {0x13, 0}, + {0x14, "FAT16 <32MB hidden"}, + {0x15, 0}, + {0x16, "FAT16 >32MB hidden"}, + {0x17, "NTFS/HPFS hidden"}, + {0x18, "AST SmartSleep"}, + {0x19, 0}, + {0x1A, 0}, + {0x1B, "FAT32 hidden"}, + {0x1C, "FAT32 hidden, LBA)"}, + {0x1D, 0}, + {0x1E, "FAT16 hidden, LBA)"}, + {0x1F, 0}, + {0x20, 0}, + {0x21, 0}, + {0x22, 0}, + {0x23, 0}, + {0x24, "NEC DOS"}, + {0x25, 0}, + {0x26, 0}, + {0x27, 0}, + {0x28, 0}, + {0x29, 0}, + {0x2A, "AtheOS AFS"}, + {0x2B, "SyllableSecure"}, + {0x2C, 0}, + {0x2D, 0}, + {0x2E, 0}, + {0x2F, 0}, + {0x30, 0}, + {0x31, 0}, + {0x32, "NOS"}, + {0x33, 0}, + {0x34, 0}, + {0x35, "JFS on OS/2"}, + {0x36, 0}, + {0x37, 0}, + {0x38, "THEOS 2GB"}, + {0x39, "Plan9"}, + {0x3A, "THEOS 4GB"}, + {0x3B, "THEOS extended"}, + {0x3C, "PartitionMagic"}, + {0x3D, "NetWare Hidden"}, + {0x3E, 0}, + {0x3F, 0}, + {0x40, "Venix 80286"}, + {0x41, "PPC PReP Boot"}, + {0x42, "SFS"}, + {0x43, 0}, + {0x44, "GoBack"}, + {0x45, "Priam"}, + {0x46, "Eumel/Ergos L3"}, + {0x47, "Eumel/Ergos L3"}, + {0x48, "Eumel/Ergos L3"}, + {0x49, 0}, + {0x4A, "AdaOS"}, + {0x4B, 0}, + {0x4C, "Oberon"}, + {0x4D, "QNX4.x"}, + {0x4E, "QNX4.x 2nd part"}, + {0x4F, "QNX4.x 3rd part"}, + {0x50, "OnTrack DM"}, + {0x51, "OnTrack DM6 Aux1"}, + {0x52, "CP/M"}, + {0x53, "OnTrack DM6 Aux3"}, + {0x54, "OnTrack DM6 DDO"}, + {0x55, "EZ-Drive"}, + {0x56, "Golden Bow"}, + {0x57, "DrivePro"}, + {0x58, 0}, + {0x59, 0}, + {0x5A, 0}, + {0x5B, 0}, + {0x5C, "Priam EDisk"}, + {0x5D, 0}, + {0x5E, 0}, + {0x5F, 0}, + {0x60, 0}, + {0x61, "SpeedStor"}, + {0x62, 0}, + {0x63, "GNU Hurd/System V"}, + {0x64, "Novell Netware 286"}, + {0x65, "Novell Netware 386"}, + {0x66, "Novell Netware SMS"}, + {0x67, "Novell"}, + {0x68, "Novell"}, + {0x69, "Novell Netware 5+"}, + {0x6A, 0}, + {0x6B, 0}, + {0x6C, 0}, + {0x6D, 0}, + {0x6E, 0}, + {0x6F, 0}, + {0x70, "DiskSecure MultiBoot"}, + {0x71, 0}, + {0x72, "V7/x86"}, + {0x73, 0}, + {0x74, 0}, + {0x75, "PX/IX"}, + {0x76, 0}, + {0x77, "M2FS/M2CS"}, + {0x77, "VNDI"}, + {0x78, "XOSL FS"}, + {0x79, 0}, + {0x7A, 0}, + {0x7B, 0}, + {0x7C, 0}, + {0x7D, 0}, + {0x7E, 0}, + {0x7F, 0}, + {0x80, "Minix < 1.4a"}, + {0x81, "Minix > 1.4b/Old Linux"}, + {0x82, "Linux swap/Solaris x86"}, + {0x83, "Linux"}, + {0x84, "OS/2 hidden C:"}, + {0x85, "Linux extended"}, + {0x86, "FAT16 volume set"}, + {0x87, "NTFS volume set"}, + {0x88, "Linux plaintext"}, + {0x89, 0}, + {0x8A, 0}, + {0x8B, 0}, + {0x8C, 0}, + {0x8D, "FreeDOS hidden FAT12"}, + {0x8E, "Linux LVM"}, + {0x8F, 0}, + {0x90, "FreeDOS hidden FAT16"}, + {0x91, "FreeDOS hidden extended"}, + {0x92, "FreeDOS hidden FAT16 large"}, + {0x93, "Amoeba"}, + {0x94, "Amoeba BBT"}, + {0x95, "MIT EXOPC"}, + {0x96, 0}, + {0x97, "FreeDOS hidden FAT32"}, + {0x98, "FreeDOS hidden FAT32 LBA"}, + {0x99, 0}, + {0x9A, "FreeDOS hidden FAT16 LBA"}, + {0x9B, "FreeDOS hidden ext'd LBA"}, + {0x9C, 0}, + {0x9D, 0}, + {0x9E, 0}, + {0x9F, "BSD/OS"}, + {0xA0, "IBM Thinkpad hibernation"}, + {0xA1, 0}, + {0xA2, 0}, + {0xA3, 0}, + {0xA4, 0}, + {0xA5, "NetBSD/FreeBSD"}, + {0xA6, "OpenBSD"}, + {0xA7, "MeXTStep"}, + {0xA8, "Darwin UFS"}, + {0xA9, "NetBSD"}, + {0xAA, 0}, + {0xAB, "Darwin boot"}, + {0xAC, 0}, + {0xAD, 0}, + {0xAE, "ShagOS"}, + {0xAF, "ShagOS swap"}, + {0xB0, 0}, + {0xB1, 0}, + {0xB2, 0}, + {0xB3, 0}, + {0xB4, 0}, + {0xB5, 0}, + {0xB6, 0}, + {0xB7, "BSDI BSD/386"}, + {0xB8, "BSDI BSD swap"}, + {0xB9, 0}, + {0xBA, 0}, + {0xBB, "BootWizard hidden"}, + {0xBC, 0}, + {0xBD, 0}, + {0xBE, "Solaris 8 boot"}, + {0xBF, "Solaris x86"}, + {0xC0, "CTOS/DRDOS"}, + {0xC1, "DRDOS secured FAT12"}, + {0xC2, "Hidden Linux"}, + {0xC3, "Hidden Linux swap"}, + {0xC4, "DRDOS sec'd FAT16 <32M"}, + {0xC5, "DRDOS sec'd ext'd"}, + {0xC6, "DRDOS sec'd FAT16 >=32M"}, + {0xC7, "Syrinx"}, + {0xC8, 0}, + {0xC9, 0}, + {0xCA, 0}, + {0xCB, "DRDOS sec'd FAT32"}, + {0xCC, "DRDOS sec'd FAT32 LBA"}, + {0xCD, 0}, + {0xCE, "DRDOS sec'd FAT16X LBA"}, + {0xCF, "DRDOS sec'd ext'd LBA"}, + {0xD0, 0}, + {0xD1, 0}, + {0xD2, 0}, + {0xD3, 0}, + {0xD4, 0}, + {0xD5, 0}, + {0xD6, 0}, + {0xD7, 0}, + {0xD8, "CP/M-86"}, + {0xD9, 0}, + {0xDA, "Non-FS data"}, + {0xDB, "CP/M / CTOS"}, + {0xDC, 0}, + {0xDD, 0}, + {0xDE, "Dell PowerEdge"}, + {0xDF, "BootIt"}, + {0xE0, 0}, + {0xE1, "DOS access"}, + {0xE2, 0}, + {0xE3, "DOS R/O"}, + {0xE4, "SpeedStor"}, + {0xE5, "Tandy MDOS"}, + {0xE6, 0}, + {0xE7, 0}, + {0xE8, "LUKS"}, + {0xE9, 0}, + {0xEA, 0}, + {0xEB, "BeOS BFS"}, + {0xEC, "SkyOS SkyFS"}, + {0xED, 0}, + {0xEE, "EFI GPT"}, + {0xEF, "EFI"}, + {0xF0, "Linux/PA-RISC boot"}, + {0xF1, "SpeedStor"}, + {0xF2, "DOS 3.3+ secondary"}, + {0xF3, 0}, + {0xF4, "SpeedStor"}, + {0xF5, 0}, + {0xF6, "SpeedStor"}, + {0xF7, 0}, + {0xF8, 0}, + {0xF9, "pCache"}, + {0xFA, "Bochs"}, + {0xFB, "VMware FS"}, + {0xFC, "VMware swap"}, + {0xFD, "Linux raid auto"}, + {0xFE, "LANstep"}, + {0xFF, "Xenix BBT"} + }; Index: lost/trunk/src/modules/c/fdisk/Makefile.all =================================================================== --- lost/trunk/src/modules/c/fdisk/Makefile.all (revision 0) +++ lost/trunk/src/modules/c/fdisk/Makefile.all (revision 0) @@ -0,0 +1,8 @@ +shopt -s extglob +source $LOST_BUILDMK_ROOT/config.sh + +echo "LD $1/apps/fdisk" +$LOST_TOOLS_LD -ofdisk -Ttext=0x40000000 *.o --start-group $2 --end-group + +mv fdisk $1/apps/ +strip $1/apps/fdisk Index: lost/trunk/src/modules/c/fdisk/typeids.h =================================================================== --- lost/trunk/src/modules/c/fdisk/typeids.h (revision 0) +++ lost/trunk/src/modules/c/fdisk/typeids.h (revision 0) @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2008 The LOST Project. All rights reserved. + * + * This code is derived from software contributed to the LOST Project + * by Martin Kleusberg. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the LOST Project + * and its contributors. + * 4. Neither the name of the LOST 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 HOLDERS 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. + */ + +#ifndef __FDISK_TYPEIDS_H__ +#define __FDISK_TYPEIDS_H__ + +/* Die IDs für extended artitions. Es gibt noch ein paar + andere, die aber unwichtig sind, wie ich finde +*/ +#define TYPE_EXTENDED_PARTITION_1 0x05 +#define TYPE_EXTENDED_PARTITION_2 0x0F +#define TYPE_EXTENDED_PARTITION_3 0x85 + +struct PartitionTypeID_t +{ + unsigned char id; + char *name; +}; + +extern struct PartitionTypeID_t PartitionTypeID[257]; + +#endif Index: lost/trunk/src/modules/c/fdisk/partition.c =================================================================== --- lost/trunk/src/modules/c/fdisk/partition.c (revision 0) +++ lost/trunk/src/modules/c/fdisk/partition.c (revision 0) @@ -0,0 +1,229 @@ +/* + * Copyright (c) 2008 The LOST Project. All rights reserved. + * + * This code is derived from software contributed to the LOST Project + * by Martin Kleusberg. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the LOST Project + * and its contributors. + * 4. Neither the name of the LOST 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 HOLDERS 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. + */ + +#include +#include +#include +#include "partition.h" +#include "typeids.h" + +long getEndSector(struct partition_t *p) +{ + return p->data.start_sector+p->data.num_sectors-1; +} +void uncompressCHS(uint8_t *compressed, unsigned int *cylinder, unsigned int *head, unsigned int *sector) +{ + *head = compressed[0]; + + *cylinder = (compressed[1] & 0xC0) << 2; + *cylinder += compressed[2]; + + *sector = compressed[1] & 0x3F; +} +void compressCHS(uint8_t *compressed, unsigned int cylinder, unsigned int head, unsigned int sector) +{ + if(head > 255) + head = 254; + if(cylinder > 1024) + cylinder = 1023; + if(sector > 63) + sector = 63; + if(sector < 1) + sector = 1; + + compressed[0] = head; + + compressed[1] = (cylinder & 0x300) >> 2; + compressed[1] += sector; + + compressed[2] = cylinder & 0xFF; +} +unsigned int CHS2LBA(unsigned int cylinder, unsigned int head, unsigned int sector) +{ + return(cylinder * device_numheads * SECTORS_PER_TRACK + head * SECTORS_PER_TRACK + (sector - 1)); +} +void LBA2CHS(unsigned int lba, unsigned int *cylinder, unsigned int *head, unsigned int *sector) +{ + *cylinder = lba / (device_numheads * SECTORS_PER_TRACK); + *head = (lba % (device_numheads * SECTORS_PER_TRACK)) / SECTORS_PER_TRACK; + *sector = ((lba % (device_numheads * SECTORS_PER_TRACK)) % SECTORS_PER_TRACK) + 1; +} + +int applyChanges() +{ + /* TODO extended/logical partitions unterstützen */ + + /* Datei/Laufwerk öffnen */ + FILE *dev = fopen(device_name, "w"); + if(dev == 0) + { + printf("Could not open device \"%s\"\nAborting!\n", dev); + return -1; + } + + /* Die im RAM liegende Kopie des MBR aktualisieren, ohne jedoch + den Code Teil zu ändern */ + int i; + for(i=0;i<4;i++) + { + /* Wenn die Partition existiert, den Datenblock an die korrekte Position kopieren. Wenn nicht, + den entsprechenden Bereich einfach mit Nullen füllen */ + if(MBR_Partitions[i].exists) + memcpy(&MBR[PARTITION_TABLE_START+i*sizeof(struct partition_entry_t)], &MBR_Partitions[i].data, sizeof(struct partition_entry_t)); + else + memset(&MBR[PARTITION_TABLE_START+i*sizeof(struct partition_entry_t)], 0, sizeof(struct partition_entry_t)); + } + + /* Die Boot Signature nocheinmal setzen, um sicher zu gehen, dass diese + auch wirklich korrekt ist */ + uint16_t *signature = (uint16_t*)(MBR + BOOT_SIGNATURE_POS); + *signature = BOOT_SIGNATURE; + + /* Neuen MBR schreiben, den Output Buffer leeren und die Datei schließen */ + fwrite(MBR, 1, 512, dev); + fflush(dev); + fclose(dev); + + return 0; +} + +void deletePartition(struct partition_t *p) +{ + /* TODO extended/logical partitions unterstützen */ + + memset(&(p->data), 0, sizeof(struct partition_entry_t)); + p->number = 0; + p->exists = 0; +} + +void createNewMBRPartitionTable() +{ + memset(&MBR[PARTITION_TABLE_START], 0, 4*sizeof(struct partition_entry_t)); + *((uint16_t*)&MBR[BOOT_SIGNATURE_POS]) = BOOT_SIGNATURE; +} + +int readPartitions(char *filename) +{ + /* TODO extended/logical partitions unterstützen */ + + /* Dateinamen speichern */ + device_name = filename; + + /* Laufwerk/Datei öffnen und MBR lesen */ + FILE *dev = fopen(filename, "r"); + if(dev == 0) + { + printf("Could not open device \"%s\"\n", filename); + return -1; + } + fread(MBR, 1, 512, dev); + + /* Boot Signature überprüfen */ + if(*(uint16_t*)(&MBR[BOOT_SIGNATURE_POS]) != BOOT_SIGNATURE) + { + printf("Invalid boot signature!\nfdisk will create a new empty partition table." + " Keep this in mind when writing to disk.\n"); + createNewMBRPartitionTable(); + } + + /* Die Partitionstabelle analysieren und in die internen Strukturen kopieren */ + int i; + for(i=0;i<4;i++) + { + memcpy(&MBR_Partitions[i].data, &MBR[PARTITION_TABLE_START+i*sizeof(struct partition_entry_t)], sizeof(struct partition_entry_t)); + if(MBR_Partitions[i].data.type) /* Ob eine Partition existiert, überprüfen wir m.H. des Type Bytes */ + { + MBR_Partitions[i].number = i+1; + MBR_Partitions[i].exists = 1; + + /* Handelt es sich um eine extended Partition? */ + if(MBR_Partitions[i].data.type == TYPE_EXTENDED_PARTITION_1 || MBR_Partitions[i].data.type == TYPE_EXTENDED_PARTITION_2 || + MBR_Partitions[i].data.type == TYPE_EXTENDED_PARTITION_3) + { + /* Es ist eine! Also diesen Eintrag extra behandeln */ + MBR_Partitions[i].type = PARTITION_TYPE_EXTENDED; + printf("Extended/Logical partitions are not yet supported!\n"); + } else { + MBR_Partitions[i].type = PARTITION_TYPE_PRIMARY; + } + } else { + MBR_Partitions[i].number = 0; + MBR_Partitions[i].exists = 0; + MBR_Partitions[i].type = PARTITION_TYPE_PRIMARY; + } + } + + /* Datei schließen */ + fclose(dev); + + return 0; +} + +void createPartition(struct partition_t *p, unsigned int number, unsigned int start_lba, unsigned int end_lba) +{ + /* TODO extended/logical partitions unterstützen */ + + /* Die LBA Daten ins CHS Format konvertieren */ + unsigned int start_cyl, start_head, start_sec; + LBA2CHS(start_lba, &start_cyl, &start_head, &start_sec); + unsigned int end_cyl, end_head, end_sec; + LBA2CHS(end_lba, &end_cyl, &end_head, &end_sec); + + /* Daraus die "komprimierten" CHS Einträge erstellen */ + uint8_t start_chs[3]; + compressCHS(start_chs, start_cyl, start_head, start_sec); + uint8_t end_chs[3]; + compressCHS(end_chs, end_cyl, end_head, end_sec); + + /* Die Strukturen entsprechend anpassen */ + p->exists = 1; + p->number = number; + p->type = PARTITION_TYPE_PRIMARY; + p->data.bootable = DEFAULT_BOOTABLE_FLAG; + p->data.start[0] = start_chs[0]; + p->data.start[1] = start_chs[1]; + p->data.start[2] = start_chs[2]; + p->data.type = DEFAULT_PARTITION_TYPE; + p->data.end[0] = end_chs[0]; + p->data.end[1] = end_chs[1]; + p->data.end[2] = end_chs[2]; + p->data.start_sector = start_lba; + p->data.num_sectors = end_lba - start_lba + 1; +} + +uint32_t getDiskSignature() +{ + return *(uint32_t*)(&MBR[DISK_SIGNATURE_START]); +}