rulimine/stage23/lib/part.h

70 lines
2.0 KiB
C
Raw Normal View History

2020-04-15 14:21:44 +03:00
#ifndef __LIB__PART_H__
#define __LIB__PART_H__
#include <stdint.h>
#include <stddef.h>
2020-10-18 07:23:39 +03:00
#include <stdbool.h>
2020-11-02 11:20:34 +03:00
#include <lib/guid.h>
2021-03-02 12:23:43 +03:00
#if defined (uefi)
# include <efi.h>
#endif
2020-04-15 14:21:44 +03:00
#define NO_PARTITION (-1)
#define INVALID_TABLE (-2)
#define END_OF_TABLE (-3)
2021-03-02 12:23:43 +03:00
#if defined (bios)
typedef int drive_t;
#elif defined (uefi)
typedef EFI_HANDLE drive_t;
2021-03-02 12:23:43 +03:00
#endif
struct volume {
2021-03-02 12:23:43 +03:00
drive_t drive;
2020-11-01 23:25:35 +03:00
int partition;
int sector_size;
int cache_status;
uint8_t *cache;
uint64_t cached_block;
2020-10-18 07:23:39 +03:00
uint64_t first_sect;
uint64_t sect_count;
2020-11-01 23:25:35 +03:00
bool guid_valid;
2020-10-18 07:23:39 +03:00
struct guid guid;
bool part_guid_valid;
struct guid part_guid;
2020-04-15 14:21:44 +03:00
};
extern struct volume *volume_index;
extern size_t volume_index_i;
void volume_create_index(void);
2020-10-18 07:23:39 +03:00
2021-03-02 12:23:43 +03:00
bool gpt_get_guid(struct guid *guid, struct volume *volume);
int part_get(struct volume *part, struct volume *volume, int partition);
bool volume_get_by_guid(struct volume *part, struct guid *guid);
2021-03-02 12:23:43 +03:00
bool volume_get_by_coord(struct volume *part, drive_t drive, int partition);
2020-11-02 12:17:20 +03:00
bool volume_read(struct volume *part, void *buffer, uint64_t loc, uint64_t count);
2020-11-02 12:17:20 +03:00
2021-03-04 00:38:28 +03:00
#define volume_iterate_parts(_VOLUME_, _BODY_) ({ \
bool _OK_ = true; \
struct volume _PART_ = _VOLUME_; \
for (int i = 0; ; i++) { \
int _PARTNUM_ = i - 1; \
_BODY_ ; \
switch (part_get(&_PART_, &_VOLUME_, i)) { \
case INVALID_TABLE: \
case END_OF_TABLE: \
_OK_ = false; \
break; \
default: \
continue; \
} \
break; \
} \
_OK_; \
})
2020-04-15 14:21:44 +03:00
#endif