interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / gpt_add.h

78 lines · 4044 bytes Raw Blame History
1/* gpt_add.h -- the one GPT change the AIENOS kernel may make: add a partition
2 * (Allen's AIENOS-type memory section, disk_part.h, or the boot section,
3 * dev/fat_boot.h) in space no partition uses, on a disk whose GPT already
4 * passes the strict check of disk_part.h with "no partition of that type".
5 * Existing entries are never moved, resized or rewritten; the
6 * protective MBR (LBA 0) is never written. Writes, in this order: the backup
7 * entry array and backup header, flush, then the primary entry array and
8 * primary header, flush; then the whole GPT is checked again and must show
9 * the new partition exactly. A power cut part way leaves one copy old and
10 * one new; the strict check then refuses both until a person looks, so
11 * nothing further is written (the firmware keeps reading the primary).
12 *
13 * Also ck_gpt_list: every used entry, read only, for the boot report.
14 * Freestanding: no libc, no heap. Host tested in svc/tests/allen_memory_test.c and dev/tests/fat_boot_test.c. */
15#ifndef AIENOS_CK_GPT_ADD_H
16#define AIENOS_CK_GPT_ADD_H
17#include <stdint.h>
18#include "disk.h"
19
20enum {
21 CK_GPT_ENOSPACE = -420, /* no unused gap holds the partition */
22 CK_GPT_ENOSLOT = -421, /* no free entry in the array */
23 CK_GPT_EBIG = -422, /* entry array larger than this file handles (64 KiB) */
24 CK_GPT_EWRITE = -423, /* a write or flush failed */
25 CK_GPT_EVERIFY = -424, /* after writing, the GPT does not show the partition exactly */
26};
27
28typedef struct {
29 uint32_t index;
30 uint8_t type[16]; /* on-disk byte order */
31 uint8_t uguid[16]; /* unique partition GUID, on-disk byte order */
32 uint64_t first, last; /* inclusive LBAs */
33 char name[37]; /* the UTF-16 name as ASCII, '?' for anything else */
34} ck_gpt_entry;
35
36/* Calls fn for every used entry when both GPT copies are valid (any
37 * ck_gpt_find_aienos answer that got that far). The number of used entries,
38 * or the CK_GPT_* refusal. */
39int ck_gpt_list(const disk_dev *d, void (*fn)(void *ctx, const ck_gpt_entry *e), void *ctx);
40
41typedef struct {
42 uint64_t first, last; /* the AIENOS partition, inclusive */
43 uint32_t index; /* its entry */
44 int created; /* 1: added now; 0: it was already there */
45} ck_gpt_added;
46
47/* What ck_gpt_add writes: the partition type, its name and the label that
48 * seeds its unique GUID (with the disk GUID and the first LBA), so the same
49 * partition can later be recognised as one this file added. */
50typedef struct {
51 const uint8_t *type; /* on-disk byte order */
52 const char *name; /* ASCII, at most 36 characters */
53 const char *label;
54} ck_gpt_new;
55
56/* Makes sure the disk has exactly one partition of spec->type: an existing
57 * one is reported (nothing written, whoever made it); otherwise one of
58 * `blocks` blocks is added at the start of the first unused gap that holds
59 * it, its first LBA rounded up to a multiple of `align` blocks. CK_GPT_OK or
60 * the refusal (CK_GPT_EMULTI: two of that type; nothing written unless the
61 * code is CK_GPT_EWRITE or CK_GPT_EVERIFY). */
62int ck_gpt_add(const disk_dev *d, const ck_gpt_new *spec, uint64_t blocks, uint64_t align, ck_gpt_added *out);
63
64/* 1 when entry `index` is the only partition of spec->type and carries the
65 * unique GUID and name ck_gpt_add would have given it (this file added it);
66 * 0 when it is someone else's; or the CK_GPT_* refusal. Reads only. */
67int ck_gpt_is_ours(const disk_dev *d, const ck_gpt_new *spec, uint32_t index);
68
69/* Makes sure the disk has an AIENOS partition: an existing one is reported
70 * (nothing written); otherwise one of `blocks` blocks is added at the start
71 * of the first unused gap that holds it, its first LBA rounded up to a
72 * multiple of `align` blocks. CK_GPT_OK or the refusal (nothing written
73 * unless the code is CK_GPT_EWRITE or CK_GPT_EVERIFY). */
74int ck_gpt_add_aienos(const disk_dev *d, uint64_t blocks, uint64_t align, ck_gpt_added *out);
75
76/* Text for the codes here and those of disk_part.h. */
77const char *ck_gpt_add_strerror(int rc);
78#endif