--- linux/kernel/blk_drv/hd.c 2018/04/24 18:04:38 1.1.1.4 +++ linux/kernel/blk_drv/hd.c 2018/04/24 18:06:41 1.1.1.6 @@ -11,8 +11,13 @@ * sleep. Special care is recommended. * * modified by Drew Eckhardt to check nr of hd's from the CMOS. + * + * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug + * in the early extended-partition checks and added DM partitions */ +#include + #include #include #include @@ -26,10 +31,11 @@ #define MAJOR_NR 3 #include "blk.h" -#define CMOS_READ(addr) ({ \ -outb_p(0x80|addr,0x70); \ -inb_p(0x71); \ -}) +static inline unsigned char CMOS_READ(unsigned char addr) +{ + outb_p(0x80|addr,0x70); + return inb_p(0x71); +} /* Max read/write errors/sector */ #define MAX_ERRORS 7 @@ -73,26 +79,125 @@ extern void rd_load(void); static unsigned int current_minor; +/* + * Create devices for each logical partition in an extended partition. + * The logical partitions form a linked list, with each entry being + * a partition table with two entries. The first entry + * is the real data partition (with a start relative to the partition + * table start). The second is a pointer to the next logical partition + * (with a start relative to the entire extended partition). + * We do not create a Linux partition for the partition tables, but + * only for the actual data partitions. + */ +static void extended_partition(unsigned int dev) +{ + struct buffer_head *bh; + struct partition *p; + unsigned long first_sector, this_sector; + + first_sector = hd[MINOR(dev)].start_sect; + this_sector = first_sector; + + while (1) { + if ((current_minor & 0x3f) >= 60) + return; + if (!(bh = bread(dev,0))) { + printk("Unable to read partition table of device %04x\n",dev); + return; + } + /* + * This block is from a device that we're about to stomp on. + * So make sure nobody thinks this block is usable. + */ + bh->b_dirt=0; + bh->b_uptodate=0; + if (*(unsigned short *) (bh->b_data+510) == 0xAA55) { + p = 0x1BE + (void *)bh->b_data; + /* + * Process the first entry, which should be the real + * data partition. + */ + if (p->sys_ind == EXTENDED_PARTITION || + !(hd[current_minor].nr_sects = p->nr_sects)) + goto done; /* shouldn't happen */ + hd[current_minor].start_sect = this_sector + p->start_sect; + printk(" Logical part %d start %d size %d end %d\n\r", + current_minor, hd[current_minor].start_sect, + hd[current_minor].nr_sects, + hd[current_minor].start_sect + + hd[current_minor].nr_sects); + current_minor++; + p++; + /* + * Process the second entry, which should be a link + * to the next logical partition. Create a minor + * for this just long enough to get the next partition + * table. The minor will be reused for the real + * data partition. + */ + if (p->sys_ind != EXTENDED_PARTITION || + !(hd[current_minor].nr_sects = p->nr_sects)) + goto done; /* no more logicals in this partition */ + hd[current_minor].start_sect = first_sector + p->start_sect; + this_sector = first_sector + p->start_sect; + dev = 0x0300 | current_minor; + brelse(bh); + } else + goto done; + } +done: + brelse(bh); +} + static void check_partition(unsigned int dev) { - int minor, i; + int i, minor = current_minor; struct buffer_head *bh; struct partition *p; + unsigned long first_sector; + first_sector = hd[MINOR(dev)].start_sect; if (!(bh = bread(dev,0))) { printk("Unable to read partition table of device %04x\n",dev); return; } - minor = current_minor; + printk("Drive %d:\n\r",minor >> 6); + current_minor += 4; /* first "extra" minor */ if (*(unsigned short *) (bh->b_data+510) == 0xAA55) { p = 0x1BE + (void *)bh->b_data; - for (i=0 ; i<4 ; i++,p++) { - hd[i+minor].start_sect = p->start_sect; - hd[i+minor].nr_sects = p->nr_sects; + for (i=1 ; i<=4 ; minor++,i++,p++) { + if (!(hd[minor].nr_sects = p->nr_sects)) + continue; + hd[minor].start_sect = first_sector + p->start_sect; + printk(" part %d start %d size %d end %d \n\r", i, + hd[minor].start_sect, hd[minor].nr_sects, + hd[minor].start_sect + hd[minor].nr_sects); + if ((current_minor & 0x3f) >= 60) + continue; + if (p->sys_ind == EXTENDED_PARTITION) { + extended_partition(0x0300 | minor); + } } - if (p->nr_sects && p->sys_ind == EXTENDED_PARTITION) { - current_minor += 4; - check_partition(i+minor); + /* + * check for Disk Manager partition table + */ + if (*(unsigned short *) (bh->b_data+0xfc) == 0x55AA) { + p = 0x1BE + (void *)bh->b_data; + for (i = 4 ; i < 16 ; i++, current_minor++) { + p--; + if ((current_minor & 0x3f) >= 60) + break; + if (!(p->start_sect && p->nr_sects)) + continue; + hd[current_minor].start_sect = p->start_sect; + hd[current_minor].nr_sects = p->nr_sects; + printk(" DM part %d start %d size %d end %d\n\r", + current_minor, + hd[current_minor].start_sect, + hd[current_minor].nr_sects, + hd[current_minor].start_sect + + hd[current_minor].nr_sects); + } } } else printk("Bad partition table on dev %04x\n",dev); @@ -119,16 +224,6 @@ int sys_setup(void * BIOS) hd_info[drive].sect = *(unsigned char *) (14+BIOS); BIOS += 16; } - if (hd_info[1].cyl) - NR_HD=2; - else - NR_HD=1; -#endif - for (i=0 ; ierrors >= MAX_ERRORS) end_request(0); if (CURRENT->errors > MAX_ERRORS/2) reset = 1; + else + recalibrate = 1; } static void read_intr(void) { + SET_INTR(&read_intr); if (win_result()) { + SET_INTR(NULL); bad_rw_intr(); do_hd_request(); return; @@ -302,10 +416,9 @@ static void read_intr(void) CURRENT->errors = 0; CURRENT->buffer += 512; CURRENT->sector++; - if (--CURRENT->nr_sectors) { - SET_INTR(&read_intr); + if (--CURRENT->nr_sectors) return; - } + SET_INTR(NULL); end_request(1); do_hd_request(); } @@ -335,19 +448,24 @@ static void recal_intr(void) do_hd_request(); } +/* + * This is another of the error-routines I don't know what to do with. The + * best idea seems to just set reset, and start all over again. + */ static void hd_times_out(void) -{ +{ + do_hd = NULL; + reset = 1; if (!CURRENT) return; - printk("HD timeout"); + printk("HD timeout\n\r"); + cli(); if (++CURRENT->errors >= MAX_ERRORS) end_request(0); - SET_INTR(NULL); - reset = 1; do_hd_request(); } -void do_hd_request(void) +static void do_hd_request(void) { int i,r; unsigned int block,dev; @@ -357,7 +475,8 @@ void do_hd_request(void) INIT_REQUEST; dev = MINOR(CURRENT->dev); block = CURRENT->sector; - if (dev >= (NR_HD<<6) || block+2 > hd[dev].nr_sects) { + nsect = CURRENT->nr_sectors; + if (dev >= (NR_HD<<6) || block+nsect > hd[dev].nr_sects) { end_request(0); goto repeat; } @@ -368,7 +487,6 @@ void do_hd_request(void) head = block % hd_info[dev].head; cyl = block / hd_info[dev].head; sec++; - nsect = CURRENT->nr_sectors; if (reset) { recalibrate = 1; reset_hd(); @@ -376,7 +494,7 @@ void do_hd_request(void) } if (recalibrate) { recalibrate = 0; - hd_out(dev,hd_info[CURRENT_DEV].sect,0,0,0, + hd_out(dev,hd_info[dev].sect,0,0,0, WIN_RESTORE,&recal_intr); if (reset) goto repeat; @@ -404,8 +522,32 @@ void do_hd_request(void) void hd_init(void) { blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST; - set_trap_gate(0x2E,&hd_interrupt); + set_intr_gate(0x2E,&hd_interrupt); outb_p(inb_p(0x21)&0xfb,0x21); outb(inb_p(0xA1)&0xbf,0xA1); timer_table[HD_TIMER].fn = hd_times_out; } + +int hd_ioctl(int dev, int cmd, int arg) +{ + struct hd_geometry *loc = (void *) arg; + + if (!loc) + return -EINVAL; + dev = MINOR(dev) >> 6; + if (dev >= NR_HD) + return -EINVAL; + + switch (cmd) { + case HDIO_REQ: + put_fs_byte(hd_info[dev].head, + (char *) &loc->heads); + put_fs_byte(hd_info[dev].sect, + (char *) &loc->sectors); + put_fs_word(hd_info[dev].cyl, + (short *) &loc->cylinders); + return 0; + default: + return -EINVAL; + } +}