return valid_pmbr(cxt) == GPT_MBR_HYBRID;
}
+/**
+ * fdisk_gpt_get_partition_attrs:
+ * @cxt: context
+ * @partnum: partition number
+ * @attrs: GPT partition attributes
+ *
+ * Sets @attrs for the given partition
+ *
+ * Returns: 0 on success, <0 on error.
+ */
+int fdisk_gpt_get_partition_attrs(
+ struct fdisk_context *cxt,
+ size_t partnum,
+ uint64_t *attrs)
+{
+ struct fdisk_gpt_label *gpt;
+
+ assert(cxt);
+ assert(cxt->label);
+ assert(fdisk_is_label(cxt, GPT));
+
+ gpt = self_label(cxt);
+
+ if ((uint32_t) partnum >= le32_to_cpu(gpt->pheader->npartition_entries))
+ return -EINVAL;
+
+ *attrs = le64_to_cpu(gpt->ents[partnum].attrs);
+ return 0;
+}
+
+/**
+ * fdisk_gpt_set_partition_attrs:
+ * @cxt: context
+ * @partnum: partition number
+ * @attrs: GPT partition attributes
+ *
+ * Sets the GPT partition attributes field to @attrs.
+ *
+ * Returns: 0 on success, <0 on error.
+ */
+int fdisk_gpt_set_partition_attrs(
+ struct fdisk_context *cxt,
+ size_t partnum,
+ uint64_t attrs)
+{
+ struct fdisk_gpt_label *gpt;
+
+ assert(cxt);
+ assert(cxt->label);
+ assert(fdisk_is_label(cxt, GPT));
+
+ DBG(LABEL, ul_debug("GPT entry attributes change requested partno=%zu", partnum));
+ gpt = self_label(cxt);
+
+ if ((uint32_t) partnum >= le32_to_cpu(gpt->pheader->npartition_entries))
+ return -EINVAL;
+
+ gpt->ents[partnum].attrs = cpu_to_le64(attrs);
+ fdisk_info(cxt, _("The attributes on partition %zu changed to 0x%016" PRIx64 "."),
+ partnum + 1, attrs);
+
+ gpt_recompute_crc(gpt->pheader, gpt->ents);
+ gpt_recompute_crc(gpt->bheader, gpt->ents);
+ fdisk_label_set_changed(cxt->label, 1);
+ return 0;
+}
+
static int gpt_toggle_partition_flag(
struct fdisk_context *cxt,
size_t i,
return lb;
}
+
+#ifdef TEST_PROGRAM
+int test_getattr(struct fdisk_test *ts, int argc, char *argv[])
+{
+ const char *disk = argv[1];
+ size_t part = strtoul(argv[2], NULL, 0) - 1;
+ struct fdisk_context *cxt;
+ uint64_t atters = 0;
+
+ cxt = fdisk_new_context();
+ fdisk_assign_device(cxt, disk, 1);
+
+ if (!fdisk_is_label(cxt, GPT))
+ return EXIT_FAILURE;
+
+ if (fdisk_gpt_get_partition_attrs(cxt, part, &atters))
+ return EXIT_FAILURE;
+
+ printf("%s: 0x%016" PRIx64 "\n", argv[2], atters);
+
+ fdisk_unref_context(cxt);
+ return 0;
+}
+
+int test_setattr(struct fdisk_test *ts, int argc, char *argv[])
+{
+ const char *disk = argv[1];
+ size_t part = strtoul(argv[2], NULL, 0) - 1;
+ uint64_t atters = strtoull(argv[3], NULL, 0);
+ struct fdisk_context *cxt;
+
+ cxt = fdisk_new_context();
+ fdisk_assign_device(cxt, disk, 0);
+
+ if (!fdisk_is_label(cxt, GPT))
+ return EXIT_FAILURE;
+
+ if (fdisk_gpt_set_partition_attrs(cxt, part, atters))
+ return EXIT_FAILURE;
+
+ if (fdisk_write_disklabel(cxt))
+ return EXIT_FAILURE;
+
+ fdisk_unref_context(cxt);
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ struct fdisk_test tss[] = {
+ { "--getattr", test_getattr, "<disk> <partition> print attributes" },
+ { "--setattr", test_setattr, "<disk> <partition> <value> set attributes" },
+ { NULL }
+ };
+
+ return fdisk_run_test(tss, argc, argv);
+}
+
+#endif
--- /dev/null
+Checking that no-one is using this disk right now ... OK
+
+Disk <removed>: 10 MiB, 10485760 bytes, 20480 sectors
+Units: sectors of 1 * 512 = 512 bytes
+Sector size (logical/physical): 512 bytes / 512 bytes
+I/O size (minimum/optimal): 512 bytes / 512 bytes
+
+>>> Script header accepted.
+>>> Script header accepted.
+>>> Created a new <removed>.
+Created a new <removed>.
+<removed>2: Created a new <removed>.
+<removed>3:
+New situation:
+
+Device Start End Sectors Size Type
+<removed>1 2048 12287 10240 5M Linux filesystem
+<removed>2 12288 20446 8159 4M Linux filesystem
+
+The partition table has been altered.
+Syncing disks.
--- /dev/null
+#!/bin/bash
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+#
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="GPT"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+TESTPROG="$TS_HELPER_LIBFDISK_GPT"
+ts_check_test_command "$TESTPROG"
+ts_check_test_command "$TS_CMD_SFDISK"
+
+TEST_IMAGE_NAME=$(ts_image_init 10)
+
+ts_init_subtest "all-defaults"
+$TS_CMD_SFDISK --unit S ${TEST_IMAGE_NAME} >> $TS_OUTPUT 2>&1 <<EOF
+label: gpt
+label-id: b181c399-4711-4c52-8b65-9e764541218d
+
+size=5M, attrs="RequiredPartiton,48"
+attrs="52,53,62"
+EOF
+ts_fdisk_clean ${TEST_IMAGE_NAME}
+ts_finalize_subtest
+
+ts_init_subtest "getattr"
+ts_valgrind $TESTPROG --getattr ${TEST_IMAGE_NAME} 1 >> $TS_OUTPUT 2>&1
+ts_valgrind $TESTPROG --getattr ${TEST_IMAGE_NAME} 2 >> $TS_OUTPUT 2>&1
+ts_finalize_subtest
+
+ts_init_subtest "setattr"
+ts_valgrind $TESTPROG --setattr ${TEST_IMAGE_NAME} 1 0xff00000000000000 >> $TS_OUTPUT 2>&1
+ts_valgrind $TESTPROG --setattr ${TEST_IMAGE_NAME} 2 0x4 >> $TS_OUTPUT 2>&1
+$TS_CMD_SFDISK --part-attrs ${TEST_IMAGE_NAME} 1 >> $TS_OUTPUT 2>&1
+$TS_CMD_SFDISK --part-attrs ${TEST_IMAGE_NAME} 2 >> $TS_OUTPUT 2>&1
+ts_finalize_subtest
+
+ts_finalize