aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tools / gpt_write.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* gpt_write.h -- hosted GPT image writer for the C kernel's QEMU gates and |
| 18 | * host tests (tools/ck_gpt_image.c, dev/tests/disk_part_test.c). Test |
| 19 | * tooling only (libc), never part of an image. It writes the layout |
| 20 | * independently of the kernel parser (dev/disk_part.c); only the CRC32 |
| 21 | * routine (ck_crc32) is shared, and the host test checks it against the |
| 22 | * standard check value. |
| 23 | * |
| 24 | * Layout written: protective MBR (one 0xEE record), primary header at LBA 1, |
| 25 | * 128 entries of 128 bytes from LBA 2, backup entries just below the last |
| 26 | * LBA, backup header at the last LBA (UEFI 2.10, section 5.3). */ |
| 27 | #ifndef AIENOS_CK_GPT_WRITE_H |
| 28 | #define AIENOS_CK_GPT_WRITE_H |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | #define GW_ENTRIES 128u |
| 32 | #define GW_ESIZE 128u |
| 33 | #define GW_MAX_PARTS 8 |
| 34 | |
| 35 | /* Linux filesystem data type GUID 0FC63DAF-8483-4772-8E79-3D69D8477DE4 |
| 36 | * (on-disk byte order): used for the sentinel partitions. */ |
| 37 | extern const uint8_t gw_linux_type[16]; |
| 38 | /* EFI System Partition type GUID C12A7328-F81F-11D2-BA4B-00A0C93EC93B (on-disk |
| 39 | * byte order): used by the esp-model layout. */ |
| 40 | extern const uint8_t gw_esp_type[16]; |
| 41 | |
| 42 | typedef struct { |
| 43 | const uint8_t *type; /* 16 on-disk bytes */ |
| 44 | uint64_t first, last; /* inclusive LBAs as written into the entry */ |
| 45 | const char *name; /* ASCII, up to 36 characters */ |
| 46 | uint8_t fill; /* 0: leave zero; else fill the range with fill ^ (uint8_t)lba */ |
| 47 | } gw_part; |
| 48 | |
| 49 | enum { |
| 50 | GW_NO_GPT = 1u << 0, /* write partition fills only: no MBR, no GPT */ |
| 51 | GW_BAD_PRIMARY_CRC = 1u << 1, /* primary header CRC32 field flipped */ |
| 52 | GW_BAD_BACKUP_CRC = 1u << 2, /* backup header CRC32 field flipped */ |
| 53 | GW_BAD_PRIMARY_ENT = 1u << 3, /* primary entry array changed after its CRC */ |
| 54 | GW_BAD_BACKUP_ENT = 1u << 4, /* backup entry array changed after its CRC */ |
| 55 | GW_NO_PMBR = 1u << 5, /* GPT written, protective MBR left zero */ |
| 56 | }; |
| 57 | |
| 58 | /* Create (or truncate) path as a bs-byte-block image of `blocks` blocks and |
| 59 | * write the layout. Fills that fall outside the image are clipped. Returns 0 |
| 60 | * or -1 (message on stderr). */ |
| 61 | int gw_write(const char *path, uint32_t bs, uint64_t blocks, const gw_part *parts, int nparts, unsigned flags); |
| 62 | |
| 63 | /* Entry array blocks for block size bs (128 x 128 bytes). */ |
| 64 | uint64_t gw_entry_blocks(uint32_t bs); |
| 65 | #endif |