diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2016-08-23 16:13:33 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-08-23 16:13:33 +0200 |
commit | 607f2b404e992174d7c5956d11e8f35f78d2701f (patch) | |
tree | 60a7bb8d2a187348f055b60d8ee399cfe2e47aa4 | |
parent | 6b76e2345490a1f65423b72580f84d09b3983ab6 (diff) | |
download | busybox-w32-607f2b404e992174d7c5956d11e8f35f78d2701f.tar.gz busybox-w32-607f2b404e992174d7c5956d11e8f35f78d2701f.tar.bz2 busybox-w32-607f2b404e992174d7c5956d11e8f35f78d2701f.zip |
fdisk: print much less cryptic partition table
Before:
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 998 255471+ 6 FAT16
What are "blocks"? What is that "+"?
How big is this partition?
Is start/end shown came from LBA fields or CHS fields?
Why are we torturing the user??
After:
Device Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
/dev/sdb1 * 0,1,1 996,15,32 32 510974 510943 249M 6 FAT16
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | util-linux/fdisk.c | 190 |
1 files changed, 103 insertions, 87 deletions
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c index 6391f9bd9..6b5e3880f 100644 --- a/util-linux/fdisk.c +++ b/util-linux/fdisk.c | |||
@@ -363,7 +363,6 @@ struct globals { | |||
363 | 363 | ||
364 | jmp_buf listingbuf; | 364 | jmp_buf listingbuf; |
365 | char line_buffer[80]; | 365 | char line_buffer[80]; |
366 | char partname_buffer[80]; | ||
367 | /* Raw disk label. For DOS-type partition tables the MBR, | 366 | /* Raw disk label. For DOS-type partition tables the MBR, |
368 | * with descriptions of the primary partitions. */ | 367 | * with descriptions of the primary partitions. */ |
369 | char MBRbuffer[MAX_SECTOR_SIZE]; | 368 | char MBRbuffer[MAX_SECTOR_SIZE]; |
@@ -399,7 +398,6 @@ struct globals { | |||
399 | #define total_number_of_sectors (G.total_number_of_sectors) | 398 | #define total_number_of_sectors (G.total_number_of_sectors) |
400 | #define listingbuf (G.listingbuf ) | 399 | #define listingbuf (G.listingbuf ) |
401 | #define line_buffer (G.line_buffer ) | 400 | #define line_buffer (G.line_buffer ) |
402 | #define partname_buffer (G.partname_buffer) | ||
403 | #define MBRbuffer (G.MBRbuffer ) | 401 | #define MBRbuffer (G.MBRbuffer ) |
404 | #define ptes (G.ptes ) | 402 | #define ptes (G.ptes ) |
405 | #define INIT_G() do { \ | 403 | #define INIT_G() do { \ |
@@ -468,9 +466,6 @@ static sector_t bb_BLKGETSIZE_sectors(int fd) | |||
468 | 466 | ||
469 | #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2)) | 467 | #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2)) |
470 | 468 | ||
471 | #define hsc2sector(h,s,c) \ | ||
472 | (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c))) | ||
473 | |||
474 | static void | 469 | static void |
475 | close_dev_fd(void) | 470 | close_dev_fd(void) |
476 | { | 471 | { |
@@ -478,44 +473,6 @@ close_dev_fd(void) | |||
478 | xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd); | 473 | xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd); |
479 | } | 474 | } |
480 | 475 | ||
481 | /* | ||
482 | * Return partition name - uses static storage | ||
483 | */ | ||
484 | static const char * | ||
485 | partname(const char *dev, int pno, int lth) | ||
486 | { | ||
487 | const char *p; | ||
488 | int w, wp; | ||
489 | int bufsiz; | ||
490 | char *bufp; | ||
491 | |||
492 | bufp = partname_buffer; | ||
493 | bufsiz = sizeof(partname_buffer); | ||
494 | |||
495 | w = strlen(dev); | ||
496 | p = ""; | ||
497 | |||
498 | if (isdigit(dev[w-1])) | ||
499 | p = "p"; | ||
500 | |||
501 | /* devfs kludge - note: fdisk partition names are not supposed | ||
502 | to equal kernel names, so there is no reason to do this */ | ||
503 | if (strcmp(dev + w - 4, "disc") == 0) { | ||
504 | w -= 4; | ||
505 | p = "part"; | ||
506 | } | ||
507 | |||
508 | wp = strlen(p); | ||
509 | |||
510 | if (lth) { | ||
511 | snprintf(bufp, bufsiz, "%*.*s%s%-2u", | ||
512 | lth-wp-2, w, dev, p, pno); | ||
513 | } else { | ||
514 | snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno); | ||
515 | } | ||
516 | return bufp; | ||
517 | } | ||
518 | |||
519 | static ALWAYS_INLINE struct partition * | 476 | static ALWAYS_INLINE struct partition * |
520 | get_part_table(int i) | 477 | get_part_table(int i) |
521 | { | 478 | { |
@@ -1898,14 +1855,14 @@ check_consistency(const struct partition *p, int partition) | |||
1898 | return; /* do not check extended partitions */ | 1855 | return; /* do not check extended partitions */ |
1899 | 1856 | ||
1900 | /* physical beginning c, h, s */ | 1857 | /* physical beginning c, h, s */ |
1901 | pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300); | 1858 | pbc = cylinder(p->sector, p->cyl); |
1902 | pbh = p->head; | 1859 | pbh = p->head; |
1903 | pbs = p->sector & 0x3f; | 1860 | pbs = sector(p->sector); |
1904 | 1861 | ||
1905 | /* physical ending c, h, s */ | 1862 | /* physical ending c, h, s */ |
1906 | pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300); | 1863 | pec = cylinder(p->end_sector, p->end_cyl); |
1907 | peh = p->end_head; | 1864 | peh = p->end_head; |
1908 | pes = p->end_sector & 0x3f; | 1865 | pes = sector(p->end_sector); |
1909 | 1866 | ||
1910 | /* compute logical beginning (c, h, s) */ | 1867 | /* compute logical beginning (c, h, s) */ |
1911 | linear2chs(get_start_sect(p), &lbc, &lbh, &lbs); | 1868 | linear2chs(get_start_sect(p), &lbc, &lbh, &lbs); |
@@ -1916,17 +1873,17 @@ check_consistency(const struct partition *p, int partition) | |||
1916 | /* Same physical / logical beginning? */ | 1873 | /* Same physical / logical beginning? */ |
1917 | if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) { | 1874 | if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) { |
1918 | printf("Partition %u has different physical/logical " | 1875 | printf("Partition %u has different physical/logical " |
1919 | "beginnings (non-Linux?):\n", partition + 1); | 1876 | "start (non-Linux?):\n", partition + 1); |
1920 | printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs); | 1877 | printf(" phys=(%u,%u,%u) ", pbc, pbh, pbs); |
1921 | printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs); | 1878 | printf("logical=(%u,%u,%u)\n", lbc, lbh, lbs); |
1922 | } | 1879 | } |
1923 | 1880 | ||
1924 | /* Same physical / logical ending? */ | 1881 | /* Same physical / logical ending? */ |
1925 | if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) { | 1882 | if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) { |
1926 | printf("Partition %u has different physical/logical " | 1883 | printf("Partition %u has different physical/logical " |
1927 | "endings:\n", partition + 1); | 1884 | "end:\n", partition + 1); |
1928 | printf(" phys=(%u, %u, %u) ", pec, peh, pes); | 1885 | printf(" phys=(%u,%u,%u) ", pec, peh, pes); |
1929 | printf("logical=(%u, %u, %u)\n", lec, leh, les); | 1886 | printf("logical=(%u,%u,%u)\n", lec, leh, les); |
1930 | } | 1887 | } |
1931 | 1888 | ||
1932 | /* Ending on cylinder boundary? */ | 1889 | /* Ending on cylinder boundary? */ |
@@ -2099,10 +2056,53 @@ fix_partition_table_order(void) | |||
2099 | } | 2056 | } |
2100 | #endif | 2057 | #endif |
2101 | 2058 | ||
2059 | /* Return partition name */ | ||
2060 | static const char * | ||
2061 | partname(const char *dev, int pno, int lth) | ||
2062 | { | ||
2063 | const char *p; | ||
2064 | int w, wp; | ||
2065 | int bufsiz; | ||
2066 | char *bufp; | ||
2067 | |||
2068 | bufp = auto_string(xzalloc(80)); | ||
2069 | bufsiz = 80; | ||
2070 | |||
2071 | w = strlen(dev); | ||
2072 | p = ""; | ||
2073 | |||
2074 | if (isdigit(dev[w-1])) | ||
2075 | p = "p"; | ||
2076 | |||
2077 | /* devfs kludge - note: fdisk partition names are not supposed | ||
2078 | to equal kernel names, so there is no reason to do this */ | ||
2079 | if (strcmp(dev + w - 4, "disc") == 0) { | ||
2080 | w -= 4; | ||
2081 | p = "part"; | ||
2082 | } | ||
2083 | |||
2084 | wp = strlen(p); | ||
2085 | |||
2086 | if (lth) { | ||
2087 | snprintf(bufp, bufsiz, "%*.*s%s%-2u", | ||
2088 | lth-wp-2, w, dev, p, pno); | ||
2089 | } else { | ||
2090 | snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno); | ||
2091 | } | ||
2092 | return bufp; | ||
2093 | } | ||
2094 | |||
2095 | static const char * | ||
2096 | chs_string11(unsigned cyl, unsigned head, unsigned sect) | ||
2097 | { | ||
2098 | char *buf = auto_string(xzalloc(sizeof(int)*3 * 3)); | ||
2099 | sprintf(buf, "%u,%u,%u", cylinder(sect,cyl), head, sector(sect)); | ||
2100 | return buf; | ||
2101 | } | ||
2102 | |||
2102 | static void | 2103 | static void |
2103 | list_table(int xtra) | 2104 | list_table(int xtra) |
2104 | { | 2105 | { |
2105 | const struct partition *p; | ||
2106 | int i, w; | 2106 | int i, w; |
2107 | 2107 | ||
2108 | if (LABEL_IS_SUN) { | 2108 | if (LABEL_IS_SUN) { |
@@ -2126,50 +2126,62 @@ list_table(int xtra) | |||
2126 | } | 2126 | } |
2127 | 2127 | ||
2128 | /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3, | 2128 | /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3, |
2129 | but if the device name ends in a digit, say /dev/foo1, | 2129 | * but if the device name ends in a digit, say /dev/foo1, |
2130 | then the partition is called /dev/foo1p3. */ | 2130 | * then the partition is called /dev/foo1p3. |
2131 | */ | ||
2131 | w = strlen(disk_device); | 2132 | w = strlen(disk_device); |
2132 | if (w && isdigit(disk_device[w-1])) | 2133 | if (w && isdigit(disk_device[w-1])) |
2133 | w++; | 2134 | w++; |
2134 | if (w < 5) | 2135 | if (w < 7) |
2135 | w = 5; | 2136 | w = 7; |
2136 | 2137 | ||
2137 | // 1 12345678901 12345678901 12345678901 12 | 2138 | printf("%-*s Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type\n", |
2138 | printf("%*s Boot Start End Blocks Id System\n", | 2139 | w-1, "Device"); |
2139 | w+1, "Device"); | ||
2140 | 2140 | ||
2141 | for (i = 0; i < g_partitions; i++) { | 2141 | for (i = 0; i < g_partitions; i++) { |
2142 | const struct partition *p; | ||
2142 | const struct pte *pe = &ptes[i]; | 2143 | const struct pte *pe = &ptes[i]; |
2143 | sector_t psects; | 2144 | char boot4[4]; |
2144 | sector_t pblocks; | 2145 | char numstr6[6]; |
2145 | unsigned podd; | 2146 | sector_t start_sect; |
2147 | sector_t end_sect; | ||
2148 | sector_t nr_sects; | ||
2146 | 2149 | ||
2147 | p = pe->part_table; | 2150 | p = pe->part_table; |
2148 | if (!p || is_cleared_partition(p)) | 2151 | if (!p || is_cleared_partition(p)) |
2149 | continue; | 2152 | continue; |
2150 | 2153 | ||
2151 | psects = get_nr_sects(p); | 2154 | sprintf(boot4, "%02x", p->boot_ind); |
2152 | pblocks = psects; | 2155 | if ((p->boot_ind & 0x7f) == 0) { |
2153 | podd = 0; | 2156 | /* 0x80 shown as '*', 0x00 is ' ' */ |
2154 | 2157 | boot4[0] = p->boot_ind ? '*' : ' '; | |
2155 | if (sector_size < 1024) { | 2158 | boot4[1] = ' '; |
2156 | pblocks /= (1024 / sector_size); | ||
2157 | podd = psects % (1024 / sector_size); | ||
2158 | } | 2159 | } |
2159 | if (sector_size > 1024) | ||
2160 | pblocks *= (sector_size / 1024); | ||
2161 | 2160 | ||
2162 | printf("%s %c %11"SECT_FMT"u %11"SECT_FMT"u %11"SECT_FMT"u%c %2x %s\n", | 2161 | start_sect = get_partition_start_from_dev_start(pe); |
2163 | partname(disk_device, i+1, w+2), | 2162 | end_sect = start_sect; |
2164 | !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */ | 2163 | nr_sects = get_nr_sects(p); |
2165 | ? '*' : '?', | 2164 | if (nr_sects != 0) |
2166 | cround(get_partition_start_from_dev_start(pe)), /* start */ | 2165 | end_sect += nr_sects - 1; |
2167 | cround(get_partition_start_from_dev_start(pe) + psects /* end */ | ||
2168 | - (psects ? 1 : 0)), | ||
2169 | pblocks, podd ? '+' : ' ', /* odd flag on end */ | ||
2170 | p->sys_ind, /* type id */ | ||
2171 | partition_type(p->sys_ind)); /* type name */ | ||
2172 | 2166 | ||
2167 | smart_ulltoa5((ullong)nr_sects * sector_size, | ||
2168 | numstr6, " KMGTPEZY")[0] = '\0'; | ||
2169 | |||
2170 | #define SFMT SECT_FMT | ||
2171 | // Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type | ||
2172 | printf("%s%s %-11s"/**/" %-11s"/**/" %10"SFMT"u %10"SFMT"u %10"SFMT"u %s %2x %s\n", | ||
2173 | partname(disk_device, i+1, w+2), | ||
2174 | boot4, | ||
2175 | chs_string11(p->cyl, p->head, p->sector), | ||
2176 | chs_string11(p->end_cyl, p->end_head, p->end_sector), | ||
2177 | start_sect, | ||
2178 | end_sect, | ||
2179 | nr_sects, | ||
2180 | numstr6, | ||
2181 | p->sys_ind, | ||
2182 | partition_type(p->sys_ind) | ||
2183 | ); | ||
2184 | #undef SFMT | ||
2173 | check_consistency(p, i); | 2185 | check_consistency(p, i); |
2174 | } | 2186 | } |
2175 | 2187 | ||
@@ -2198,13 +2210,17 @@ x_list_table(int extend) | |||
2198 | p = (extend ? pe->ext_pointer : pe->part_table); | 2210 | p = (extend ? pe->ext_pointer : pe->part_table); |
2199 | if (p != NULL) { | 2211 | if (p != NULL) { |
2200 | printf("%2u %02x%4u%4u%5u%4u%4u%5u%11"SECT_FMT"u%11"SECT_FMT"u %02x\n", | 2212 | printf("%2u %02x%4u%4u%5u%4u%4u%5u%11"SECT_FMT"u%11"SECT_FMT"u %02x\n", |
2201 | i + 1, p->boot_ind, p->head, | 2213 | i + 1, p->boot_ind, |
2214 | p->head, | ||
2202 | sector(p->sector), | 2215 | sector(p->sector), |
2203 | cylinder(p->sector, p->cyl), p->end_head, | 2216 | cylinder(p->sector, p->cyl), |
2217 | p->end_head, | ||
2204 | sector(p->end_sector), | 2218 | sector(p->end_sector), |
2205 | cylinder(p->end_sector, p->end_cyl), | 2219 | cylinder(p->end_sector, p->end_cyl), |
2206 | get_start_sect(p), get_nr_sects(p), | 2220 | get_start_sect(p), |
2207 | p->sys_ind); | 2221 | get_nr_sects(p), |
2222 | p->sys_ind | ||
2223 | ); | ||
2208 | if (p->sys_ind) | 2224 | if (p->sys_ind) |
2209 | check_consistency(p, i); | 2225 | check_consistency(p, i); |
2210 | } | 2226 | } |