1 /* pcpart.c - manipulate fdisk partitions */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <grub/types.h>
22 #include <grub/misc.h>
25 #include <grub/msdos_partition.h>
26 #include <grub/device.h>
27 #include <grub/disk.h>
28 #include <grub/partition.h>
29 #include <grub/parttool.h>
30 #include <grub/i18n.h>
32 GRUB_MOD_LICENSE ("GPLv2+");
34 static int activate_table_handle
= -1;
35 static int type_table_handle
= -1;
37 static struct grub_parttool_argdesc grub_pcpart_bootargs
[] =
39 {"boot", N_("Make partition active"), GRUB_PARTTOOL_ARG_BOOL
},
43 static grub_err_t
grub_pcpart_boot (const grub_device_t dev
,
44 const struct grub_parttool_args
*args
)
47 grub_partition_t part
;
48 struct grub_msdos_partition_mbr mbr
;
50 if (dev
->disk
->partition
->offset
)
51 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("not a primary partition"));
53 index
= dev
->disk
->partition
->index
;
54 part
= dev
->disk
->partition
;
55 dev
->disk
->partition
= part
->parent
;
58 if (grub_disk_read (dev
->disk
, 0, 0, sizeof (mbr
), &mbr
))
60 dev
->disk
->partition
= part
;
64 if (args
[0].set
&& args
[0].bool)
66 for (i
= 0; i
< 4; i
++)
67 mbr
.entries
[i
].flag
= 0x0;
68 mbr
.entries
[index
].flag
= 0x80;
69 grub_printf_ (N_("Partition %d is active now. \n"), index
);
73 mbr
.entries
[index
].flag
= 0x0;
74 grub_printf (N_("Cleared active flag on %d. \n"), index
);
78 grub_disk_write (dev
->disk
, 0, 0, sizeof (mbr
), &mbr
);
80 dev
->disk
->partition
= part
;
85 static struct grub_parttool_argdesc grub_pcpart_typeargs
[] =
87 {"type", N_("Change partition type"), GRUB_PARTTOOL_ARG_VAL
},
88 {"hidden", N_("Set `hidden' flag in partition type"), GRUB_PARTTOOL_ARG_BOOL
},
92 static grub_err_t
grub_pcpart_type (const grub_device_t dev
,
93 const struct grub_parttool_args
*args
)
97 grub_partition_t part
;
98 struct grub_msdos_partition_mbr mbr
;
100 index
= dev
->disk
->partition
->index
;
101 part
= dev
->disk
->partition
;
102 dev
->disk
->partition
= part
->parent
;
104 /* Read the parttable. */
105 if (grub_disk_read (dev
->disk
, part
->offset
, 0,
108 dev
->disk
->partition
= part
;
113 type
= grub_strtoul (args
[0].str
, 0, 0);
115 type
= mbr
.entries
[index
].type
;
120 type
|= GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG
;
122 type
&= ~GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG
;
125 if (grub_msdos_partition_is_empty (type
)
126 || grub_msdos_partition_is_extended (type
))
128 dev
->disk
->partition
= part
;
129 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
130 N_("the partition type 0x%x isn't "
134 mbr
.entries
[index
].type
= type
;
135 /* TRANSLATORS: In this case we're actually writing to the disk and actively
136 modifying partition type rather than just defining it. */
137 grub_printf_ (N_("Setting partition type to 0x%x\n"), type
);
139 /* Write the parttable. */
140 grub_disk_write (dev
->disk
, part
->offset
, 0,
143 dev
->disk
->partition
= part
;
145 return GRUB_ERR_NONE
;
148 GRUB_MOD_INIT (msdospart
)
150 activate_table_handle
= grub_parttool_register ("msdos",
152 grub_pcpart_bootargs
);
153 type_table_handle
= grub_parttool_register ("msdos",
155 grub_pcpart_typeargs
);
158 GRUB_MOD_FINI(msdospart
)
160 grub_parttool_unregister (activate_table_handle
);
161 grub_parttool_unregister (type_table_handle
);