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,116 @@ +/* + * 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 ueber eine Partition, die fdisk benoetigt +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 get_end_sector(struct partition_t* p); +// Gibt die Disk Signature des zurueck +uint32_t get_disk_signature(); +// Zerlegt die 3 Byte grossen CHS Eintraege +void uncompress_chs(uint8_t* compressed, unsigned int* cylinder, + unsigned int* head, unsigned int* sector); +// Erstellt die 3 Byte grossen CHS Eintraege +void compress_chs(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 zurueck, wenn erfolgreich +int read_partitions(char* filename); +// Neuen MBR erstellen und auf Festplatte schreiben. Gibt 0 zurueck, +// wenn erfolgreich +int apply_changes(); +// Loescht eine Partition +void delete_partition(struct partition_t* p); +// Erstellt eine neue Partition +void create_partition(struct partition_t* p, unsigned int number, + unsigned int start_lba, unsigned int end_lba); + +// Kopie des MBR und Array fuer 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,465 @@ +/* + * 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 aenderungen vorgenommen wurden, +// und sobald diese auf die Festplatte geschrieben wurden, auf 0 zurueck +// gesetzt +int unsaved_changes = 0; + +// Diese Funktion wird nur vorruebergehend benoetigt, 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", get_disk_signature()); + + // Kopfzeile der Tabelle + printf("# | Start | End | Boot | Type\n"); + + // TODO extended/logical partitions unterstuetzen + + // 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, + get_end_sector(&mbr_partitions[i]), + mbr_partitions[i].data.bootable == BOOTFLAG_SET ? '*' : ' ', + (int)mbr_partitions[i].data.type); + } + } +} + +void shell_toggle_bootable() +{ + // TODO extended/logical partitions unterstuetzen + + // Den User nach der Partitionsnummer fragen. Den passenden Array Index + // erhaelt 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 ueberpruefen. Die Nummer muss gueltig sein, und die + // dazugehoerige 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 aenderungen 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 (!apply_changes()) { + 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 + // erhaelt 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 unterstuetzen + + // Abbrechen? + if (partition == -1) { + return; + } + + // Eingabe ueberpruefen. Die Nummer muss gueltig sein, und die + // dazugehoerige Partition soll (noch) exisitieren + if (partition < 0 || partition > 3 || + mbr_partitions[partition].exists == 0) + { + printf("Invalid partition\n"); + } else { + delete_partition(&mbr_partitions[partition]); + unsaved_changes = 1; + } +} + +void shell_typelist() +{ + int i; + int types_printed = 0; + for (i=0;i<256;i++) { + if (partition_type_id[i].name != 0) { + // 3 Typen pro Zeile anzeigen. Um diesen Wert zu aendern, muss + // die 21 im printf String und die 3 in der if Abfrage geaendert + // werden + printf("%02x %-21s", i, partition_type_id[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 + // erhaelt 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 unterstuetzen + + // Abbrechen? + if (partition == -1) { + return; + } + + // Eingabe ueberpruefen. Die Nummer muss gueltig sein, und die + // dazugehoerige 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 eingeschraenkt werden und eine Suchfunktion + // bzw. etwas vergleichbares intigiert werden. + // Den User solange nach einer neuen ID fragen, wie er eine + // ungueltige 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 + // abaendern + mbr_partitions[partition].data.type = hex2uint8(input[0], input[1]); + free(input); + + unsaved_changes = 1; + } +} + +void shell_new() +{ + // TODO extended/logical partitions unterstuetzen + // TODO Funktion etwas aufraeumen + + // Den User nach der Partitionsnummer fragen. Den passenden Array Index + // erhaelt man 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 ueberpruefen. Die Nummer muss gueltig sein, und die + // dazugehoerige Partition darf noch nicht exisitieren + if (partition < 0 || partition > 3 || + mbr_partitions[partition].exists != 0) + { + printf("Invalid partition number\n"); + } else { + // TODO Nur gueltige Werte akzeptieren, so dass es nicht mehr moeglich + // ist, Partitionen zu erstellen, die zu gross sind, nicht + // vollstaendig auf der Festplatte liegen, die sich mit anderen + // Partitionen ueberlappen 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 Groesse 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. Groesse 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 Partitionsgroesse berechnen + char* input = readline("End Position: Partition size " + "(in sectors)? "); + end_lba = start_lba + atoi(input); + free(input); + } + + // Neue Partition erstellen + create_partition(&mbr_partitions[partition], partition, start_lba, + end_lba); + + unsaved_changes = 1; + } +} + +void shell() +{ + printf("Type h for help\n"); + + while (1) { + // Naechsten 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 aendern + shell_toggle_bootable(); + } else if (strcmp(input, "w") == 0) { // aenderungen speichern + shell_write(); + } else if (strcmp(input, "d") == 0) { // Partition loeschen + 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 ueberpruefen + if (argc != 2) { + printf("Usage: fdisk device\n"); + printf("For example: fdisk ata:/ata00\n"); + return 0; + } + + // Aktuelle Partitionierung einlesen + if (read_partitions(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" + +// Siehe http://www.win.tue.nl/~aeb/partitions/partition_types-1.html +// Die Namen duerfen momenten nicht laenger als 21 sein, +// siehe shell_typelist() in fdisk.c +// TODO Namen und Abkuerzungen vereinheitlichen +struct partition_type_id_t partition_type_id[257] = { + {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 Manag"}, + {0x0B, "FAT32"}, + {0x0C, "FAT32 (LBA)"}, + {0x0D, 0}, + {0x0E, "FAT16 (LBA)"}, + {0x0F, "W95 Ext' LBA"}, + {0x10, "OPUS"}, + {0x11, "FAT12 (Hidden)"}, + {0x12, "Diagnostic"}, + {0x13, 0}, + {0x14, "FAT16 <32M hidn"}, + {0x15, 0}, + {0x16, "FAT16 >32M hidn"}, + {0x17, "NTFS/HPFS hid'n"}, + {0x18, "AST SmartSleep"}, + {0x19, 0}, + {0x1A, 0}, + {0x1B, "FAT32 hidden"}, + {0x1C, "FAT32 hid'n LBA"}, + {0x1D, 0}, + {0x1E, "FAT16 hid'n 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"}, + {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 hid'n FAT12"}, + {0x8E, "Linux LVM"}, + {0x8F, 0}, + {0x90, "FreeDOS hid'n FAT16"}, + {0x91, "FreeDOS hid'n ext'd"}, + {0x92, "FreeDOS hid'n FAT16"}, + {0x93, "Amoeba"}, + {0x94, "Amoeba BBT"}, + {0x95, "MIT EXOPC"}, + {0x96, 0}, + {0x97, "FreeDOS hid'n FAT32"}, + {0x98, "FreeDOS hid FAT32LBA"}, + {0x99, 0}, + {0x9A, "FreeDOS hid FAT16LBA"}, + {0x9B, "FreeDOS hid ext'dLBA"}, + {0x9C, 0}, + {0x9D, 0}, + {0x9E, 0}, + {0x9F, "BSD/OS"}, + {0xA0, "Thinkpad hibernate"}, + {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, "BootWiz hid'n"}, + {0xBC, 0}, + {0xBD, 0}, + {0xBE, "Solaris 8 boot"}, + {0xBF, "Solaris x86"}, + {0xC0, "CTOS/DRDOS"}, + {0xC1, "DRDOS sec'd FAT12"}, + {0xC2, "Hidden Linux"}, + {0xC3, "Hid'n Linux swap"}, + {0xC4, "DRDOS sec FAT16 <32M"}, + {0xC5, "DRDOS sec'd ext'd"}, + {0xC6, "DRDOS sec FAT16 >32M"}, + {0xC7, "Syrinx"}, + {0xC8, 0}, + {0xC9, 0}, + {0xCA, 0}, + {0xCB, "DRDOS sec'd FAT32"}, + {0xCC, "DRDOS sec FAT32 LBA"}, + {0xCD, 0}, + {0xCE, "DRDOS sec FAT16 LBA"}, + {0xCF, "DRDOS sec 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 --start-group *.o $2 --end-group + +$LOST_TOOLS_STRIP -s fdisk -o $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,53 @@ +/* + * 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 fuer extended partitions. 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 partition_type_id_t +{ + unsigned char id; + char* name; +}; + +extern struct partition_type_id_t partition_type_id[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,246 @@ +/* + * 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 get_end_sector(struct partition_t* p) +{ + return p->data.start_sector+p->data.num_sectors-1; +} +void uncompress_chs(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 compress_chs(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 apply_changes() +{ + // TODO extended/logical partitions unterstuetzen + + // Datei/Laufwerk oeffnen + 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 aendern + 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 fuellen + 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 schliessen + fwrite(mbr, 1, 512, dev); + fflush(dev); + fclose(dev); + + return 0; +} + +void delete_partition(struct partition_t* p) +{ + // TODO extended/logical partitions unterstuetzen + + memset(&(p->data), 0, sizeof(struct partition_entry_t)); + p->number = 0; + p->exists = 0; +} + +static void create_new_mbr_partition_table() +{ + memset(&mbr[PARTITION_TABLE_START], 0, + 4 * sizeof(struct partition_entry_t)); + *((uint16_t*)&mbr[BOOT_SIGNATURE_POS]) = BOOT_SIGNATURE; +} + +int read_partitions(char* filename) +{ + // TODO extended/logical partitions unterstuetzen + + // Dateinamen speichern + device_name = filename; + + // Laufwerk/Datei oeffnen 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 ueberpruefen + if (*(uint16_t*)(&mbr[BOOT_SIGNATURE_POS]) != BOOT_SIGNATURE) { + printf("Invalid boot signature!\nfdisk will create a new empty " + "partition table.\nKeep this in mind when writing to disk.\n"); + create_new_mbr_partition_table(); + } + + // 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)); + // Ob eine Partition existiert, ueberpruefen wir m.H. des Type Bytes + if (mbr_partitions[i].data.type) { + 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 schliessen + fclose(dev); + + return 0; +} + +void create_partition(struct partition_t* p, unsigned int number, + unsigned int start_lba, unsigned int end_lba) +{ + // TODO extended/logical partitions unterstuetzen + + // 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 Eintraege erstellen + uint8_t start_chs[3]; + compress_chs(start_chs, start_cyl, start_head, start_sec); + uint8_t end_chs[3]; + compress_chs(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 get_disk_signature() +{ + return *(uint32_t*)(&mbr[DISK_SIGNATURE_START]); +}