1 /* partition table tools
2 Copyright (C) 2008-2009 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <parted/parted.h>
23 #include <parted/debug.h>
29 # define _(String) dgettext (PACKAGE, String)
31 # define _(String) (String)
32 #endif /* ENABLE_NLS */
34 static char zero[16 * 1024];
36 /* Write a single sector to DISK, filling the first BUFLEN
37 bytes of that sector with data from BUF, and NUL-filling
38 any remaining bytes. Return nonzero to indicate success,
41 ptt_write_sector (PedDisk const *disk, void const *buf, size_t buflen)
43 PED_ASSERT (buflen <= disk->dev->sector_size, return 0);
44 /* Allocate a big enough buffer for ped_device_write. */
45 char *s0 = ped_malloc (disk->dev->sector_size);
48 /* Copy boot_code into the first part. */
49 memcpy (s0, buf, buflen);
50 char *p = s0 + buflen;
51 /* Fill the rest with zeros. */
52 memset (p, 0, disk->dev->sector_size - buflen);
53 int write_ok = ped_device_write (disk->dev, s0, 0, 1);
59 /* Read sector, SECTOR_NUM (which has length DEV->sector_size) into malloc'd
60 storage. If the read fails, free the memory and return zero without
61 modifying *BUF. Otherwise, set *BUF to the new buffer and return 1. */
63 ptt_read_sector (PedDevice const *dev, PedSector sector_num, void **buf)
65 char *b = ped_malloc (dev->sector_size);
66 PED_ASSERT (b != NULL, return 0);
67 if (!ped_device_read (dev, b, sector_num, 1)) {
75 /* Zero N sectors of DEV, starting with START.
76 Return nonzero to indicate success, zero otherwise. */
78 ptt_clear_sectors (PedDevice *dev, PedSector start, PedSector n)
80 PED_ASSERT (dev->sector_size <= sizeof zero, return 0);
81 PedSector n_z_sectors = sizeof zero / dev->sector_size;
82 PedSector n_full = n / n_z_sectors;
84 for (i = 0; i < n_full; i++)
86 if (!ped_device_write (dev, zero, start + n_z_sectors * i, n_z_sectors))
90 PedSector rem = n - n_z_sectors * i;
92 ? 1 : ped_device_write (dev, zero, start + n_z_sectors * i, rem));
95 /* Throw an exception and return 0 if PART's starting sector number or
96 its length is greater than the maximum allowed value for LABEL_TYPE.
97 Otherwise, return 1. */
99 ptt_partition_max_start_len (char const *label_type, const PedPartition *part)
101 static char const *const max_32[] = {"msdos", "dvh", "dasd"};
104 for (i = 0; i < sizeof max_32 / sizeof *max_32; i++)
106 if (strcmp (label_type, max_32[i]) == 0)
108 /* The length (in sectors) must fit in 32 bytes. */
109 if (part->geom.length > UINT32_MAX)
111 ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
112 _("partition length of %jd sectors exceeds"
113 " the %s-partition-table-imposed maximum"
121 /* The starting sector number must fit in 32 bytes. */
122 if (part->geom.start > UINT32_MAX) {
123 ped_exception_throw (
124 PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
125 _("starting sector number, %jd exceeds"
126 " the %s-partition-table-imposed maximum"