[syslinux] [PATCH 03/12] core: initial implementation of fopen with multidisk acess
Andre Ericson
de.ericson at gmail.com
Mon Aug 20 01:16:45 PDT 2012
An initial implementation of fopen. Should work for GPT/MBR.
cache still needs to be modified as syslinux will break after opening
file.
Signed-off-by: Andre Ericson <de.ericson at gmail.com>
Signed-off-by: Paulo Alcantara <pcacjr at zytor.com>
---
com32/elflink/ldlinux/execute.c | 2 +-
com32/modules/Makefile | 2 +-
com32/modules/fopen_test.c | 30 ++
core/conio.c | 2 +-
core/disk.c | 2 +-
core/fs/chdir.c | 54 ++-
core/fs/fs.c | 209 +++++++++-
core/fs/lib/chdir.c | 5 +-
core/fs/pxe/pxe.c | 2 +-
core/fs/readdir.c | 2 +-
core/include/fs.h | 11 +-
core/include/multidisk.h | 8 +
core/include/partiter.h | 105 ++++++
core/multidisk.c | 52 +++
core/partiter.c | 816 ++++++++++++++++++++++++++++++++++++++++
15 files changed, 1271 insertions(+), 31 deletions(-)
create mode 100644 com32/modules/fopen_test.c
create mode 100644 core/include/multidisk.h
create mode 100644 core/include/partiter.h
create mode 100644 core/multidisk.c
create mode 100644 core/partiter.c
diff --git a/com32/elflink/ldlinux/execute.c b/com32/elflink/ldlinux/execute.c
index e7969c2..ff8393d 100644
--- a/com32/elflink/ldlinux/execute.c
+++ b/com32/elflink/ldlinux/execute.c
@@ -111,7 +111,7 @@ void execute(const char *cmdline, uint32_t type)
/* If we got anything on the command line, do a chdir */
if (*args)
- mangle_name(config_cwd, args);
+ mangle_name(config_cwd, args, NULL);
start_ldlinux(argv);
} else if (type == IMAGE_TYPE_LOCALBOOT) {
diff --git a/com32/modules/Makefile b/com32/modules/Makefile
index 8f5b769..7c396ed 100644
--- a/com32/modules/Makefile
+++ b/com32/modules/Makefile
@@ -27,7 +27,7 @@ MODULES = config.c32 ethersel.c32 dmitest.c32 cpuidtest.c32 \
meminfo.c32 sdi.c32 sanboot.c32 ifcpu64.c32 vesainfo.c32 \
kbdmap.c32 cmd.c32 vpdtest.c32 host.c32 ls.c32 gpxecmd.c32 \
ifcpu.c32 cpuid.c32 cat.c32 pwd.c32 ifplop.c32 zzjson.c32 \
- whichsys.c32 hello.c32 pxechn.c32
+ whichsys.c32 hello.c32 pxechn.c32 fopen_test.c32
TESTFILES =
diff --git a/com32/modules/fopen_test.c b/com32/modules/fopen_test.c
new file mode 100644
index 0000000..a6a2ec1
--- /dev/null
+++ b/com32/modules/fopen_test.c
@@ -0,0 +1,30 @@
+/*
+ * fopen_test.c - A simple ELF module to test fopen on a different partition.
+ *
+ * Created on: Jun 30, 2012
+ * Author: Andre Ericson <de.ericson at gmail.com>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#define FILENAME "(0 2):/andre/hello_mate"
+
+int main(int argc __unused, char **argv __unused)
+{
+ /* should have this syntax: (hd part):/path/to/file */
+ FILE *f = fopen(FILENAME, "rt");
+ char buff[50];
+ int i = 0;
+
+ if (!f)
+ printf("File not found.\n"
+ "For multidisk files use (hd part):/path/to/file\n");
+ else {
+ while ((buff[i++] = fgetc(f)) && i < 50);
+ buff[i < 49 ?i : 49] = '\0';
+
+ printf("read %s\n", buff);
+ }
+
+ return 0;
+}
diff --git a/core/conio.c b/core/conio.c
index 421dabf..c94caa1 100644
--- a/core/conio.c
+++ b/core/conio.c
@@ -445,7 +445,7 @@ static void msg_viewimage(void)
*VGAFilePtr = '\0'; /* Zero-terminate filename */
- mangle_name(VGAFileMBuf, VGAFileBuf);
+ mangle_name(VGAFileMBuf, VGAFileBuf, NULL);
f = fopen(VGAFileMBuf, "r");
if (!f) {
/* Not there */
diff --git a/core/disk.c b/core/disk.c
index 5e3ce61..9280c83 100644
--- a/core/disk.c
+++ b/core/disk.c
@@ -74,7 +74,7 @@ int disk_int13_retry(const com32sys_t * inreg, com32sys_t * outreg)
*/
int disk_get_params(int disk, struct disk_info *const diskinfo)
{
- static com32sys_t inreg, outreg;
+ com32sys_t inreg, outreg;
struct disk_ebios_eparam *eparam = NULL;
memset(diskinfo, 0, sizeof *diskinfo);
diff --git a/core/fs/chdir.c b/core/fs/chdir.c
index 903cabc..e42f0dd 100644
--- a/core/fs/chdir.c
+++ b/core/fs/chdir.c
@@ -65,7 +65,7 @@ size_t realpath(char *dst, const char *src, size_t bufsize)
if (this_fs->fs_ops->realpath) {
s = this_fs->fs_ops->realpath(this_fs, dst, src, bufsize);
} else {
- rv = searchdir(src);
+ rv = searchdir(src, NULL);
if (rv < 0) {
dprintf("realpath: searchpath failure\n");
return -1;
@@ -97,7 +97,7 @@ int chdir(const char *src)
return this_fs->fs_ops->chdir(this_fs, src);
/* Otherwise it is a "conventional filesystem" */
- rv = searchdir(src);
+ rv = searchdir(src, NULL);
if (rv < 0)
return rv;
@@ -129,3 +129,53 @@ int chdir(const char *src)
return 0;
}
+
+/* won't merge this and chdir(const char*) to not break compatibility
+ * with unistd.h */
+int multidisk_chdir(const char *src, struct fs_info *fp)
+{
+ int rv;
+ struct file *file;
+ char cwd_buf[CURRENTDIR_MAX];
+ size_t s;
+
+ dprintf("chdir: from %s (inode %p) add %s\n",
+ fp->cwd_name, fp->cwd, src);
+
+ if (fp->fs_ops->chdir)
+ return fp->fs_ops->chdir(fp, src);
+
+ /* Otherwise it is a "conventional filesystem" */
+ rv = searchdir(src, fp);
+
+ if (rv < 0)
+ return rv;
+
+ file = handle_to_file(rv);
+ if (file->inode->mode != DT_DIR) {
+ _close_file(file);
+ return -1;
+ }
+
+ put_inode(fp->cwd);
+ fp->cwd = get_inode(file->inode);
+ _close_file(file);
+
+ /* Save the current working directory */
+ s = generic_inode_to_path(fp->cwd, cwd_buf, CURRENTDIR_MAX-1);
+
+ /* Make sure the cwd_name ends in a slash, it's supposed to be a prefix */
+ if (s < 1 || cwd_buf[s-1] != '/')
+ cwd_buf[s++] = '/';
+
+ if (s >= CURRENTDIR_MAX)
+ s = CURRENTDIR_MAX - 1;
+
+ cwd_buf[s++] = '\0';
+ memcpy(fp->cwd_name, cwd_buf, s);
+
+ dprintf("chdir: final %s (inode %p)\n",
+ fp->cwd_name, fp->cwd);
+
+ return 0;
+}
diff --git a/core/fs/fs.c b/core/fs/fs.c
index 7b0f6ce..b9878fe 100644
--- a/core/fs/fs.c
+++ b/core/fs/fs.c
@@ -7,12 +7,14 @@
#include "core.h"
#include "dev.h"
#include "fs.h"
+#include "multidisk.h"
#include "cache.h"
char *PATH;
/* The currently mounted filesystem */
struct fs_info *this_fs = NULL; /* Root filesystem */
+struct fs_info *fs_array[256][4];
/* Actual file structures (we don't have malloc yet...) */
struct file files[MAX_OPEN];
@@ -113,12 +115,15 @@ void pm_mangle_name(com32sys_t *regs)
const char *src = MK_PTR(regs->ds, regs->esi.w[0]);
char *dst = MK_PTR(regs->es, regs->edi.w[0]);
- mangle_name(dst, src);
+ mangle_name(dst, src, NULL);
}
-void mangle_name(char *dst, const char *src)
+void mangle_name(char *dst, const char *src, struct fs_info *fp)
{
- this_fs->fs_ops->mangle_name(dst, src);
+ if (fp)
+ fp->fs_ops->mangle_name(dst, src);
+ else
+ this_fs->fs_ops->mangle_name(dst, src);
}
void getfssec(com32sys_t *regs)
@@ -200,12 +205,120 @@ size_t pmapi_read_file(uint16_t *handle, void *buf, size_t sectors)
return bytes_read;
}
+extern const struct fs_ops vfat_fs_ops;
+extern const struct fs_ops ext2_fs_ops;
+extern const struct fs_ops btrfs_fs_ops;
+extern const struct fs_ops ntfs_fs_ops;
+
+const struct fs_ops *fs_ops_array [4] = {
+ &vfat_fs_ops,
+ &ext2_fs_ops,
+ /* TODO: add btrfs */
+ /*&btrfs_fs_ops,*/
+ NULL,
+ NULL
+};
+
+struct fs_info *get_fs_info(uint8_t hdd, uint8_t partition)
+{
+ struct fs_info fs;
+ uint8_t disk_devno, disk_cdrom;
+ sector_t disk_offset;
+ uint16_t disk_heads, disk_sectors, maxtransfer;
+ struct part_iter *iter = NULL;
+ uint8_t buff[512];
+ struct disk_info diskinfo;
+ const struct fs_ops **ops;
+ int blk_shift = -1;
+ struct device *dev = NULL;
+
+ if (fs_array[hdd][partition - 1])
+ return fs_array[hdd][partition - 1];
+
+ disk_devno = 0x80 + hdd;
+
+ /* For some unknown reason without this call to a "nop" function
+ * an infinite loop happens, must investigate later
+ * TODO: fix this */
+ do_magic(buff);
+
+ if (find_partition(&iter, disk_devno, partition)) {
+ printf("Failed to get partition\n");
+ return NULL;
+ }
+ else
+ dprintf("part_offset 0x%llx\n", iter->start_lba);
+
+ disk_offset = iter->start_lba;
+
+ if (disk_get_params(disk_devno, &diskinfo))
+ return NULL;
+
+ disk_heads = diskinfo.head;
+ disk_sectors = diskinfo.spt;
+
+ /* force those values for now
+ * TODO: fix this */
+ disk_cdrom = 0;
+ maxtransfer = 127;
+
+ ops = fs_ops_array;
+
+
+ /* Default name for the root directory */
+ fs.cwd_name[0] = '/';
+
+ while ((blk_shift < 0) && *ops) {
+ /* set up the fs stucture */
+ fs.fs_ops = *ops;
+
+ /*
+ * This boldly assumes that we don't mix FS_NODEV filesystems
+ * with FS_DEV filesystems...
+ */
+ if (fs.fs_ops->fs_flags & FS_NODEV) {
+ fs.fs_dev = NULL;
+ } else {
+ if (!dev)
+ dev = device_init(disk_devno, disk_cdrom, disk_offset,
+ disk_heads, disk_sectors, maxtransfer);
+ fs.fs_dev = dev;
+ }
+ /* invoke the fs-specific init code */
+ blk_shift = fs.fs_ops->fs_init(&fs);
+ ops++;
+ }
+ if (blk_shift < 0) {
+ printf("No valid file system found!\n");
+ while (1)
+ ;
+ }
+ fs_array[hdd][partition - 1] = &fs;
+
+ /* initialize the cache */
+ if (fs.fs_dev && fs.fs_dev->cache_data)
+ cache_init(fs.fs_dev, blk_shift);
+
+ /* start out in the root directory */
+ if (fs.fs_ops->iget_root) {
+ fs.root = fs.fs_ops->iget_root(&fs);
+ fs.cwd = get_inode(fs.root);
+ }
+
+ if (fs.fs_ops->chdir_start) {
+ if (fs.fs_ops->chdir_start(fs_array[hdd][partition -1]) < 0)
+ printf("Failed to chdir to start directory\n");
+ }
+ return fs_array[hdd][partition - 1];
+}
+
+
void pm_searchdir(com32sys_t *regs)
{
char *name = MK_PTR(regs->ds, regs->edi.w[0]);
int rv;
- rv = searchdir(name);
+ rv = searchdir(name, NULL);
if (rv < 0) {
regs->esi.w[0] = 0;
regs->eax.l = 0;
@@ -217,7 +330,7 @@ void pm_searchdir(com32sys_t *regs)
}
}
-int searchdir(const char *name)
+int searchdir(const char *name, struct fs_info *fp)
{
struct inode *inode = NULL;
struct inode *parent = NULL;
@@ -228,10 +341,13 @@ int searchdir(const char *name)
dprintf("searchdir: %s root: %p cwd: %p\n",
name, this_fs->root, this_fs->cwd);
+ if (!fp)
+ fp = this_fs;
if (!(file = alloc_file()))
goto err_no_close;
- file->fs = this_fs;
+
+ file->fs = fp;
/* if we have ->searchdir method, call it */
if (file->fs->fs_ops->searchdir) {
@@ -245,7 +361,7 @@ int searchdir(const char *name)
/* else, try the generic-path-lookup method */
- parent = get_inode(this_fs->cwd);
+ parent = get_inode(fp->cwd);
p = pathbuf = strdup(name);
if (!pathbuf)
goto err;
@@ -254,7 +370,7 @@ int searchdir(const char *name)
got_link:
if (*p == '/') {
put_inode(parent);
- parent = get_inode(this_fs->root);
+ parent = get_inode(fp->root);
}
do {
@@ -285,8 +401,8 @@ int searchdir(const char *name)
}
}
} else if (part[0] != '.' || part[1] != '\0') {
- inode = this_fs->fs_ops->iget(part, parent);
- if (!inode)
+ inode = fp->fs_ops->iget(part, parent);
+ if (!inode)
goto err;
if (inode->mode == DT_LNK) {
char *linkbuf, *q;
@@ -294,7 +410,7 @@ int searchdir(const char *name)
int total_len = inode->size + name_len + 2;
int link_len;
- if (!this_fs->fs_ops->readlink ||
+ if (!fp->fs_ops->readlink ||
--symlink_count == 0 || /* limit check */
total_len > MAX_SYMLINK_BUF)
goto err;
@@ -303,7 +419,7 @@ int searchdir(const char *name)
if (!linkbuf)
goto err;
- link_len = this_fs->fs_ops->readlink(inode, linkbuf);
+ link_len = fp->fs_ops->readlink(inode, linkbuf);
if (link_len <= 0) {
free(linkbuf);
goto err;
@@ -373,11 +489,70 @@ int open_file(const char *name, struct com32_filedata *filedata)
int rv;
struct file *file;
char mangled_name[FILENAME_MAX];
+ uint8_t hdd = 0;
+ uint8_t partition = 0;
+ char buff[4];
+
+ if (name[0] == '(') {
+ char relative_name[FILENAME_MAX];
+ const char *c = name;
+ int i = 0;
+ int mult = 1;
+
+ /* get hdd number */
+ for (++c; *c != ' '; ++c)
+ buff[i++] = *c;
+ buff[i] = '\0';
+
+ /* str to uint8_t */
+ while (i--) {
+ hdd += (buff[i] - 48) * mult;
+ mult *= 10;
+ }
+
+ /* get partition number */
+ i = 0;
+ for (++c; *c != ')'; ++c)
+ buff[i++] = *c;
+ buff[i] = '\0';
+
+ /* str to uint8_t */
+ mult = 1;
+ while (i--) {
+ partition += (buff[i] - 48) * mult;
+ mult *= 10;
+ }
+
+ i = 0;
+ /* c was on ')' jump ':' and stop at beginning of path */
+ for (c += 2; *c; c++)
+ relative_name[i++] = *c;
+ relative_name[i] = '\0';
+
+ mangle_name(mangled_name, relative_name, get_fs_info(hdd, partition));
+
+ rv = searchdir(mangled_name, get_fs_info(hdd, partition));
+
+ if (rv < 0)
+ return rv;
+ file = handle_to_file(rv);
+
+ if (file->inode->mode != DT_REG) {
+ _close_file(file);
+ return -1;
+ }
+
+ filedata->size = file->inode->size;
+ filedata->blocklg2 = SECTOR_SHIFT(file->fs);
+ filedata->handle = rv;
+
+ return rv;
+ }
dprintf("open_file %s\n", name);
- mangle_name(mangled_name, name);
- rv = searchdir(mangled_name);
+ mangle_name(mangled_name, name, NULL);
+ rv = searchdir(mangled_name, NULL);
if (rv < 0)
return rv;
@@ -405,8 +580,8 @@ void pm_open_file(com32sys_t *regs)
dprintf("pm_open_file %s\n", name);
- mangle_name(mangled_name, name);
- rv = searchdir(mangled_name);
+ mangle_name(mangled_name, name, NULL);
+ rv = searchdir(mangled_name, NULL);
if (rv < 0) {
regs->eflags.l |= EFLAGS_CF;
} else {
@@ -503,7 +678,7 @@ void fs_init(com32sys_t *regs)
}
if (fs.fs_ops->chdir_start) {
- if (fs.fs_ops->chdir_start() < 0)
+ if (fs.fs_ops->chdir_start(NULL) < 0)
printf("Failed to chdir to start directory\n");
}
diff --git a/core/fs/lib/chdir.c b/core/fs/lib/chdir.c
index 715284b..dfbfdd8 100644
--- a/core/fs/lib/chdir.c
+++ b/core/fs/lib/chdir.c
@@ -1,7 +1,10 @@
#include <unistd.h>
#include <core.h>
+#include "fs.h"
-int generic_chdir_start(void)
+int generic_chdir_start(struct fs_info *fp)
{
+ if (fp)
+ return multidisk_chdir("/", fp);
return chdir(CurrentDirName);
}
diff --git a/core/fs/pxe/pxe.c b/core/fs/pxe/pxe.c
index 6f490ce..3d18a2b 100644
--- a/core/fs/pxe/pxe.c
+++ b/core/fs/pxe/pxe.c
@@ -1047,7 +1047,7 @@ static int pxe_chdir(struct fs_info *fs, const char *src)
return 0;
}
-static int pxe_chdir_start(void)
+static int pxe_chdir_start(struct fs_info *fp)
{
get_prefix();
return 0;
diff --git a/core/fs/readdir.c b/core/fs/readdir.c
index d071aff..ca03a80 100644
--- a/core/fs/readdir.c
+++ b/core/fs/readdir.c
@@ -12,7 +12,7 @@ DIR *opendir(const char *path)
int rv;
struct file *file;
- rv = searchdir(path);
+ rv = searchdir(path, NULL);
if (rv < 0)
return NULL;
diff --git a/core/include/fs.h b/core/include/fs.h
index 26b99c2..0d870e0 100644
--- a/core/include/fs.h
+++ b/core/include/fs.h
@@ -53,7 +53,7 @@ struct fs_ops {
/* in fact, we use fs_ops structure to find the right fs */
const char *fs_name;
enum fs_flags fs_flags;
-
+
int (*fs_init)(struct fs_info *);
void (*searchdir)(const char *, struct file *);
uint32_t (*getfssec)(struct file *, char *, int, bool *);
@@ -61,7 +61,7 @@ struct fs_ops {
void (*mangle_name)(char *, const char *);
size_t (*realpath)(struct fs_info *, char *, const char *, size_t);
int (*chdir)(struct fs_info *, const char *);
- int (*chdir_start)(void);
+ int (*chdir_start)(struct fs_info *fp);
int (*open_config)(struct com32_filedata *);
struct inode * (*iget_root)(struct fs_info *);
@@ -188,8 +188,8 @@ extern char *PATH;
/* fs.c */
void pm_mangle_name(com32sys_t *);
void pm_searchdir(com32sys_t *);
-void mangle_name(char *, const char *);
-int searchdir(const char *name);
+void mangle_name(char *, const char *, struct fs_info *);
+int searchdir(const char *name, struct fs_info *);
void _close_file(struct file *);
size_t pmapi_read_file(uint16_t *handle, void *buf, size_t sectors);
int open_file(const char *name, struct com32_filedata *filedata);
@@ -202,6 +202,7 @@ int open_config(void);
void pm_realpath(com32sys_t *regs);
size_t realpath(char *dst, const char *src, size_t bufsize);
int chdir(const char *src);
+int multidisk_chdir(const char *src, struct fs_info *fp);
/* readdir.c */
DIR *opendir(const char *pathname);
@@ -216,7 +217,7 @@ char *core_getcwd(char *buf, size_t size);
*/
/* chdir.c */
-int generic_chdir_start(void);
+int generic_chdir_start(struct fs_info *);
/* mangle.c */
void generic_mangle_name(char *, const char *);
diff --git a/core/include/multidisk.h b/core/include/multidisk.h
new file mode 100644
index 0000000..3fd9c08
--- /dev/null
+++ b/core/include/multidisk.h
@@ -0,0 +1,8 @@
+#ifndef MULTIDISK_H
+#define MULTIDISK_H
+#include "partiter.h"
+
+void do_magic(void *);
+int find_partition(struct part_iter **, int, int);
+
+#endif /* MULTIDISK_H */
diff --git a/core/include/partiter.h b/core/include/partiter.h
new file mode 100644
index 0000000..f2cc962
--- /dev/null
+++ b/core/include/partiter.h
@@ -0,0 +1,105 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2003-2010 H. Peter Anvin - All Rights Reserved
+ * Copyright 2010 Michal Soltys
+ * Copyright 2010 Shao Miller
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * partiter.h
+ *
+ * Provides disk / partition iteration.
+ */
+
+#ifndef _PARTITER_H
+#define _PARTITER_H
+
+#include <stdint.h>
+#include "disk.h"
+
+#define PI_ERRLOAD 3
+#define PI_INSANE 2
+#define PI_DONE 1
+#define PI_OK 0
+
+struct itertype;
+struct part_iter;
+
+struct itertype {
+ int (*ctor)(struct part_iter *, va_list *);
+ void (*dtor)(struct part_iter *);
+ struct part_iter *(*next) (struct part_iter *);
+};
+
+#define PI_GPTLABSIZE ((int)sizeof(((struct disk_gpt_part_entry *)0)->name))
+
+struct part_iter {
+ const struct itertype *type;
+ char *data;
+ char *record;
+ uint64_t start_lba;
+ uint64_t length;
+ int index;
+ int rawindex;
+ struct disk_info di;
+ int stepall;
+ int status;
+ /* internal */
+ int index0;
+ union _sub {
+ struct _dos {
+ uint32_t disk_sig;
+ uint32_t nebr_lba;
+ uint32_t cebr_lba;
+ /* internal */
+ uint32_t ebr_start;
+ uint32_t ebr_size;
+ uint32_t bebr_start;
+ uint32_t bebr_size;
+ int bebr_index0;
+ int skipcnt;
+ } dos;
+ struct _gpt {
+ struct guid disk_guid;
+ struct guid part_guid;
+ char part_label[PI_GPTLABSIZE/2+1];
+ int pe_count;
+ int pe_size;
+ uint64_t ufirst;
+ uint64_t ulast;
+ } gpt;
+ } sub;
+};
+
+extern const struct itertype * const typedos;
+extern const struct itertype * const typegpt;
+extern const struct itertype * const typeraw;
+
+struct part_iter *pi_begin(const struct disk_info *, int stepall);
+struct part_iter *pi_new(const struct itertype *, ...);
+void pi_del(struct part_iter **);
+int pi_next(struct part_iter **);
+
+#endif
diff --git a/core/multidisk.c b/core/multidisk.c
new file mode 100644
index 0000000..9623899
--- /dev/null
+++ b/core/multidisk.c
@@ -0,0 +1,52 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2012 Andre Ericson <de.ericson at gmail.com>
+ *
+ * Some parts borrowed from chain.c32:
+ *
+ * Copyright 2003-2009 H. Peter Anvin - All Rights Reserved
+ * Copyright 2009 Intel Corporation; author: H. Peter Anvin
+ *
+ * This file is part of Syslinux, and is made available under
+ * the terms of the GNU General Public License version 2.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include <unistd.h>
+#include <string.h>
+#include "core.h"
+#include "disk.h"
+#include "partiter.h"
+
+void do_magic(void *buff)
+{
+
+}
+
+int find_partition(struct part_iter **_iter, int drive, int partition)
+{
+ struct part_iter *iter = NULL;
+ struct disk_info diskinfo;
+
+ if (disk_get_params(drive, &diskinfo))
+ goto bail;
+ if (!(iter = pi_begin(&diskinfo, 0)))
+ goto bail;
+ do {
+ if (iter->index == partition)
+ break;
+ } while (!pi_next(&iter));
+ if (iter->status) {
+ printf("Request disk / partition combination not found.\n");
+ goto bail;
+ }
+ dprintf("found 0x%llx at idex: %i and partition %i\n", iter->start_lba, iter->index, partition);
+
+ *_iter = iter;
+
+ return 0;
+bail:
+ pi_del(&iter);
+ return -1;
+}
+
diff --git a/core/partiter.c b/core/partiter.c
new file mode 100644
index 0000000..ce66164
--- /dev/null
+++ b/core/partiter.c
@@ -0,0 +1,816 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2003-2010 H. Peter Anvin - All Rights Reserved
+ * Copyright 2010 Shao Miller
+ * Copyright 2010 Michal Soltys
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * partiter.c
+ *
+ * Provides disk / partition iteration.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include "disk.h"
+#include "partiter.h"
+
+#define ost_is_ext(type) ((type) == 0x05 || (type) == 0x0F || (type) == 0x85)
+#define ost_is_nondata(type) (ost_is_ext(type) || (type) == 0x00)
+#define sane(s,l) ((s)+(l) > (s))
+#define error(msg) printf("%s\n", msg)
+
+/* forwards */
+
+static int iter_ctor(struct part_iter *, va_list *);
+static int iter_dos_ctor(struct part_iter *, va_list *);
+static int iter_gpt_ctor(struct part_iter *, va_list *);
+static void iter_dtor(struct part_iter *);
+static struct part_iter *pi_dos_next(struct part_iter *);
+static struct part_iter *pi_gpt_next(struct part_iter *);
+static struct part_iter *pi_raw_next(struct part_iter *);
+int guid_is0(const struct guid *guid);
+
+static struct itertype types[] = {
+ [0] = {
+ .ctor = &iter_dos_ctor,
+ .dtor = &iter_dtor,
+ .next = &pi_dos_next,
+}, [1] = {
+ .ctor = &iter_gpt_ctor,
+ .dtor = &iter_dtor,
+ .next = &pi_gpt_next,
+}, [2] = {
+ .ctor = &iter_ctor,
+ .dtor = &iter_dtor,
+ .next = &pi_raw_next,
+}};
+
+const struct itertype * const typedos = types;
+const struct itertype * const typegpt = types+1;
+const struct itertype * const typeraw = types+2;
+
+#ifdef DEBUG
+static int inv_type(const void *type)
+{
+ int i, cnt = sizeof(types)/sizeof(types[0]);
+ for (i = 0; i < cnt; i++) {
+ if (type == types + i)
+ return 0;
+ }
+ return -1;
+}
+#endif
+
+/**
+ * iter_ctor() - common iterator initialization
+ * @iter: iterator pointer
+ * @args(0): disk_info structure used for disk functions
+ * @args(1): stepall modifier
+ *
+ * Second and further arguments are passed as a pointer to va_list
+ **/
+static int iter_ctor(struct part_iter *iter, va_list *args)
+{
+ const struct disk_info *di = va_arg(*args, const struct disk_info *);
+ int stepall = va_arg(*args, int);
+
+#ifdef DEBUG
+ if (!di)
+ return -1;
+#endif
+
+ memcpy(&iter->di, di, sizeof(struct disk_info));
+ iter->stepall = stepall;
+ iter->index0 = -1;
+ iter->length = di->lbacnt;
+
+ return 0;
+}
+
+/**
+ * iter_dtor() - common iterator cleanup
+ * @iter: iterator pointer
+ *
+ **/
+static void iter_dtor(struct part_iter *iter)
+{
+ free(iter->data);
+}
+
+/**
+ * iter_dos_ctor() - MBR/EBR iterator specific initialization
+ * @iter: iterator pointer
+ * @args(0): disk_info structure used for disk functions
+ * @args(1): pointer to buffer with loaded valid MBR
+ *
+ * Second and further arguments are passed as a pointer to va_list.
+ * This function only makes rudimentary checks. If user uses
+ * pi_new(), he/she is responsible for doing proper sanity checks.
+ **/
+static int iter_dos_ctor(struct part_iter *iter, va_list *args)
+{
+ const struct disk_dos_mbr *mbr;
+
+ /* uses args(0) */
+ if (iter_ctor(iter, args))
+ return -1;
+
+ mbr = va_arg(*args, const struct disk_dos_mbr *);
+
+#ifdef DEBUG
+ if (!mbr)
+ goto bail;
+#endif
+
+ if (!(iter->data = malloc(sizeof(struct disk_dos_mbr))))
+ goto bail;
+
+ memcpy(iter->data, mbr, sizeof(struct disk_dos_mbr));
+
+ iter->sub.dos.bebr_index0 = -1;
+ iter->sub.dos.disk_sig = mbr->disk_sig;
+
+ return 0;
+bail:
+ iter->type->dtor(iter);
+ return -1;
+}
+
+/**
+ * iter_gpt_ctor() - GPT iterator specific initialization
+ * @iter: iterator pointer
+ * @args(0): ptr to disk_info structure
+ * @args(1): ptr to buffer with GPT header
+ * @args(2): ptr to buffer with GPT partition list
+ *
+ * Second and further arguments are passed as a pointer to va_list.
+ * This function only makes rudimentary checks. If user uses
+ * pi_new(), he/she is responsible for doing proper sanity checks.
+ **/
+static int iter_gpt_ctor(struct part_iter *iter, va_list *args)
+{
+ uint64_t siz;
+ const struct disk_gpt_header *gpth;
+ const struct disk_gpt_part_entry *gptl;
+
+ /* uses args(0) */
+ if (iter_ctor(iter, args))
+ return -1;
+
+ gpth = va_arg(*args, const struct disk_gpt_header *);
+ gptl = va_arg(*args, const struct disk_gpt_part_entry *);
+
+#ifdef DEBUG
+ if (!gpth || !gptl)
+ goto bail;
+#endif
+
+ siz = (uint64_t)gpth->part_count * gpth->part_size;
+
+#ifdef DEBUG
+ if (!siz || (siz + iter->di.bps - 1) / iter->di.bps > 255u ||
+ gpth->part_size < sizeof(struct disk_gpt_part_entry)) {
+ goto bail;
+ }
+#endif
+
+ if (!(iter->data = malloc((size_t)siz)))
+ goto bail;
+
+ memcpy(iter->data, gptl, (size_t)siz);
+
+ iter->sub.gpt.pe_count = (int)gpth->part_count;
+ iter->sub.gpt.pe_size = (int)gpth->part_size;
+ iter->sub.gpt.ufirst = gpth->lba_first_usable;
+ iter->sub.gpt.ulast = gpth->lba_last_usable;
+
+ memcpy(&iter->sub.gpt.disk_guid, &gpth->disk_guid, sizeof(struct guid));
+
+ return 0;
+bail:
+ iter->type->dtor(iter);
+ return -1;
+}
+
+/* Logical partition must be sane, meaning:
+ * - must be data or empty
+ * - must have non-0 start and length
+ * - values must not wrap around 32bit
+ * - must be inside current EBR frame
+ */
+
+static int notsane_logical(const struct part_iter *iter)
+{
+ const struct disk_dos_part_entry *dp;
+ uint32_t end_log;
+
+ dp = ((struct disk_dos_mbr *)iter->data)->table;
+
+ if (!dp[0].ostype)
+ return 0;
+
+ if (ost_is_ext(dp[0].ostype)) {
+ error("1st EBR entry must be data or empty.\n");
+ return -1;
+ }
+
+ end_log = dp[0].start_lba + dp[0].length;
+
+ if (!dp[0].start_lba ||
+ !dp[0].length ||
+ !sane(dp[0].start_lba, dp[0].length) ||
+ end_log > iter->sub.dos.ebr_size) {
+
+ error("Insane logical partition.\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Extended partition must be sane, meaning:
+ * - must be extended or empty
+ * - must have non-0 start and length
+ * - values must not wrap around 32bit
+ * - must be inside base EBR frame
+ */
+
+static int notsane_extended(const struct part_iter *iter)
+{
+ const struct disk_dos_part_entry *dp;
+ uint32_t end_ebr;
+
+ dp = ((struct disk_dos_mbr *)iter->data)->table;
+
+ if (!dp[1].ostype)
+ return 0;
+
+ if (!ost_is_nondata(dp[1].ostype)) {
+ error("2nd EBR entry must be extended or empty.\n");
+ return -1;
+ }
+
+ end_ebr = dp[1].start_lba + dp[1].length;
+
+ if (!dp[1].start_lba ||
+ !dp[1].length ||
+ !sane(dp[1].start_lba, dp[1].length) ||
+ end_ebr > iter->sub.dos.bebr_size) {
+
+ error("Insane extended partition.\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Primary partition must be sane, meaning:
+ * - must have non-0 start and length
+ * - values must not wrap around 32bit
+ */
+
+static int notsane_primary(const struct part_iter *iter)
+{
+ const struct disk_dos_part_entry *dp;
+ dp = ((struct disk_dos_mbr *)iter->data)->table + iter->index0;
+
+ if (!dp->ostype)
+ return 0;
+
+ if (!dp->start_lba ||
+ !dp->length ||
+ !sane(dp->start_lba, dp->length) ||
+ dp->start_lba + dp->length > iter->di.lbacnt) {
+ error("Insane primary (MBR) partition.\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int notsane_gpt(const struct part_iter *iter)
+{
+ const struct disk_gpt_part_entry *gp;
+ gp = (const struct disk_gpt_part_entry *)
+ (iter->data + iter->index0 * iter->sub.gpt.pe_size);
+
+ if (guid_is0(&gp->type))
+ return 0;
+
+ if (gp->lba_first < iter->sub.gpt.ufirst ||
+ gp->lba_last > iter->sub.gpt.ulast) {
+ error("Insane GPT partition.\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int pi_dos_next_mbr(struct part_iter *iter, uint32_t *lba,
+ struct disk_dos_part_entry **_dp)
+{
+ struct disk_dos_part_entry *dp;
+
+ while (++iter->index0 < 4) {
+ dp = ((struct disk_dos_mbr *)iter->data)->table + iter->index0;
+
+ if (notsane_primary(iter)) {
+ iter->status = PI_INSANE;
+ goto bail;
+ }
+
+ if (ost_is_ext(dp->ostype)) {
+ if (iter->sub.dos.bebr_index0 >= 0) {
+ error("You have more than 1 extended partition.\n");
+ iter->status = PI_INSANE;
+ goto bail;
+ }
+ /* record base EBR index */
+ iter->sub.dos.bebr_index0 = iter->index0;
+ }
+ if (!ost_is_nondata(dp->ostype) || iter->stepall) {
+ *lba = dp->start_lba;
+ *_dp = dp;
+ break;
+ }
+ }
+
+ return 0;
+bail:
+ return -1;
+}
+
+static int prep_base_ebr(struct part_iter *iter)
+{
+ struct disk_dos_part_entry *dp;
+
+ if (iter->sub.dos.bebr_index0 < 0) /* if we don't have base extended partition at all */
+ return -1;
+ else if (!iter->sub.dos.bebr_start) { /* if not initialized yet */
+ dp = ((struct disk_dos_mbr *)iter->data)->table + iter->sub.dos.bebr_index0;
+
+ iter->sub.dos.bebr_start = dp->start_lba;
+ iter->sub.dos.bebr_size = dp->length;
+
+ iter->sub.dos.ebr_start = 0;
+ iter->sub.dos.ebr_size = iter->sub.dos.bebr_size;
+
+ iter->sub.dos.cebr_lba = 0;
+ iter->sub.dos.nebr_lba = iter->sub.dos.bebr_start;
+
+ iter->index0--;
+ }
+ return 0;
+}
+
+static int pi_dos_next_ebr(struct part_iter *iter, uint32_t *lba,
+ struct disk_dos_part_entry **_dp)
+{
+ struct disk_dos_part_entry *dp;
+
+ if (prep_base_ebr(iter)) {
+ iter->status = PI_DONE;
+ return -1;
+ }
+
+ while (++iter->index0 < 1024 && iter->sub.dos.nebr_lba) {
+ free(iter->data);
+ if (!(iter->data =
+ disk_read_sectors(&iter->di, iter->sub.dos.nebr_lba, 1))) {
+ error("Couldn't load EBR.\n");
+ iter->status = PI_ERRLOAD;
+ return -1;
+ }
+
+ if (notsane_logical(iter) || notsane_extended(iter)) {
+ iter->status = PI_INSANE;
+ return -1;
+ }
+
+ dp = ((struct disk_dos_mbr *)iter->data)->table;
+
+ iter->sub.dos.cebr_lba = iter->sub.dos.nebr_lba;
+
+ /* setup next frame values */
+ if (dp[1].ostype) {
+ iter->sub.dos.ebr_start = dp[1].start_lba;
+ iter->sub.dos.ebr_size = dp[1].length;
+ iter->sub.dos.nebr_lba = iter->sub.dos.bebr_start + dp[1].start_lba;
+ } else {
+ iter->sub.dos.ebr_start = 0;
+ iter->sub.dos.ebr_size = 0;
+ iter->sub.dos.nebr_lba = 0;
+ }
+
+ if (!dp[0].ostype)
+ iter->sub.dos.skipcnt++;
+
+ if (dp[0].ostype || iter->stepall) {
+ *lba = iter->sub.dos.cebr_lba + dp[0].start_lba;
+ *_dp = dp;
+ return 0;
+ }
+ /*
+ * This way it's possible to continue, if some crazy soft left a "hole"
+ * - EBR with a valid extended partition without a logical one. In
+ * such case, linux will not reserve a number for such hole - so we
+ * don't increase index0. If stepall flag is set, we will never reach
+ * this place.
+ */
+ }
+ iter->status = PI_DONE;
+ return -1;
+}
+
+static struct part_iter *pi_dos_next(struct part_iter *iter)
+{
+ uint32_t start_lba = 0;
+ struct disk_dos_part_entry *dos_part = NULL;
+
+ if (iter->status)
+ goto bail;
+
+ /* look for primary partitions */
+ if (iter->index0 < 4 &&
+ pi_dos_next_mbr(iter, &start_lba, &dos_part))
+ goto bail;
+
+ /* look for logical partitions */
+ if (iter->index0 >= 4 &&
+ pi_dos_next_ebr(iter, &start_lba, &dos_part))
+ goto bail;
+
+ /*
+ * note special index handling, if we have stepall set -
+ * this is made to keep index consistent with non-stepall
+ * iterators
+ */
+
+ if (iter->index0 >= 4 && !dos_part->ostype)
+ iter->index = -1;
+ else
+ iter->index = iter->index0 - iter->sub.dos.skipcnt + 1;
+ iter->rawindex = iter->index0 + 1;
+ iter->start_lba = start_lba;
+ iter->length = dos_part->length;
+ iter->record = (char *)dos_part;
+
+#ifdef DEBUG
+ disk_dos_part_dump(dos_part);
+#endif
+
+ return iter;
+bail:
+ return NULL;
+}
+
+static void gpt_conv_label(struct part_iter *iter)
+{
+ const struct disk_gpt_part_entry *gp;
+ const int16_t *orig_lab;
+ int i;
+
+ gp = (const struct disk_gpt_part_entry *)
+ (iter->data + iter->index0 * iter->sub.gpt.pe_size);
+ orig_lab = (const int16_t *)gp->name;
+
+ /* caveat: this is very crude conversion */
+ for (i = 0; i < PI_GPTLABSIZE/2; i++) {
+ iter->sub.gpt.part_label[i] = (char)orig_lab[i];
+ }
+ iter->sub.gpt.part_label[PI_GPTLABSIZE/2] = 0;
+}
+
+static struct part_iter *pi_gpt_next(struct part_iter *iter)
+{
+ const struct disk_gpt_part_entry *gpt_part = NULL;
+
+ if (iter->status)
+ goto bail;
+
+ while (++iter->index0 < iter->sub.gpt.pe_count) {
+ gpt_part = (const struct disk_gpt_part_entry *)
+ (iter->data + iter->index0 * iter->sub.gpt.pe_size);
+
+ if (notsane_gpt(iter)) {
+ iter->status = PI_INSANE;
+ goto bail;
+ }
+
+ if (!guid_is0(&gpt_part->type) || iter->stepall)
+ break;
+ }
+ /* no more partitions ? */
+ if (iter->index0 == iter->sub.gpt.pe_count) {
+ iter->status = PI_DONE;
+ goto bail;
+ }
+ /* gpt_part is guaranteed to be valid here */
+ iter->index = iter->index0 + 1;
+ iter->rawindex = iter->index0 + 1;
+ iter->start_lba = gpt_part->lba_first;
+ iter->length = gpt_part->lba_last - gpt_part->lba_first + 1;
+ iter->record = (char *)gpt_part;
+ memcpy(&iter->sub.gpt.part_guid, &gpt_part->uid, sizeof(struct guid));
+ gpt_conv_label(iter);
+
+#ifdef DEBUG
+ disk_gpt_part_dump(gpt_part);
+#endif
+
+ return iter;
+bail:
+ return NULL;
+}
+
+static struct part_iter *pi_raw_next(struct part_iter *iter)
+{
+ iter->status = PI_DONE;
+ return NULL;
+}
+
+int guid_is0(const struct guid *guid)
+{
+ return !*(const uint64_t *)guid && !*((const uint64_t *)guid + 1);
+}
+
+/* won't work as zlib is not on core */
+#if 0
+static int check_crc(uint32_t crc_match, const uint8_t *buf, unsigned int siz)
+{
+ uint32_t crc;
+
+ crc = crc32(0, NULL, 0);
+ crc = crc32(crc, buf, siz);
+
+ return crc_match != crc;
+}
+
+static int gpt_check_hdr_crc(const struct disk_info * const diskinfo, struct disk_gpt_header **_gh)
+{
+ struct disk_gpt_header *gh = *_gh;
+ uint64_t lba_alt;
+ uint32_t hold_crc32;
+
+ hold_crc32 = gh->chksum;
+ gh->chksum = 0;
+ if (check_crc(hold_crc32, (const uint8_t *)gh, gh->hdr_size)) {
+ error("WARNING: Primary GPT header checksum invalid.\n");
+ /* retry with backup */
+ lba_alt = gh->lba_alt;
+ free(gh);
+ if (!(gh = *_gh = disk_read_sectors(diskinfo, lba_alt, 1))) {
+ error("Couldn't read backup GPT header.\n");
+ return -1;
+ }
+ hold_crc32 = gh->chksum;
+ gh->chksum = 0;
+ if (check_crc(hold_crc32, (const uint8_t *)gh, gh->hdr_size)) {
+ error("Secondary GPT header checksum invalid.\n");
+ return -1;
+ }
+ }
+ /* restore old checksum */
+ gh->chksum = hold_crc32;
+
+ return 0;
+}
+#endif
+
+/*
+ * ----------------------------------------------------------------------------
+ * Following functions are for users to call.
+ * ----------------------------------------------------------------------------
+ */
+
+
+int pi_next(struct part_iter **_iter)
+{
+ struct part_iter *iter;
+
+ if(!_iter || !*_iter)
+ return 0;
+ iter = *_iter;
+#ifdef DEBUG
+ if (inv_type(iter->type)) {
+ error("This is not a valid iterator.\n");
+ return 0;
+ }
+#endif
+ if ((iter = iter->type->next(iter))) {
+ *_iter = iter;
+ }
+ return (*_iter)->status;
+}
+
+/**
+ * pi_new() - get new iterator
+ * @itertype: iterator type
+ * @...: variable arguments passed to ctors
+ *
+ * Variable arguments depend on the type. Please see functions:
+ * iter_gpt_ctor() and iter_dos_ctor() for details.
+ **/
+struct part_iter *pi_new(const struct itertype *type, ...)
+{
+ int badctor = 0;
+ struct part_iter *iter = NULL;
+ va_list ap;
+
+ va_start(ap, type);
+
+#ifdef DEBUG
+ if (inv_type(type)) {
+ error("Unknown iterator requested.\n");
+ goto bail;
+ }
+#endif
+
+ if (!(iter = malloc(sizeof(struct part_iter)))) {
+ error("Couldn't allocate memory for the iterator.\n");
+ goto bail;
+ }
+
+ memset(iter, 0, sizeof(struct part_iter));
+ iter->type = type;
+
+ if (type->ctor(iter, &ap)) {
+ badctor = -1;
+ error("Cannot initialize the iterator.\n");
+ goto bail;
+ }
+
+bail:
+ va_end(ap);
+ if (badctor) {
+ free(iter);
+ iter = NULL;
+ }
+ return iter;
+}
+
+/**
+ * pi_del() - delete iterator
+ * @iter: iterator double pointer
+ *
+ **/
+
+void pi_del(struct part_iter **_iter)
+{
+ struct part_iter *iter;
+
+ if(!_iter || !*_iter)
+ return;
+ iter = *_iter;
+
+#ifdef DEBUG
+ if (inv_type(iter->type)) {
+ error("This is not a valid iterator.\n");
+ return;
+ }
+#endif
+
+ iter->type->dtor(iter);
+ free(iter);
+ *_iter = NULL;
+}
+
+/**
+ * pi_begin() - check disk, validate, and get proper iterator
+ * @di: diskinfo struct pointer
+ *
+ * This function checks the disk for GPT or legacy partition table and allocates
+ * an appropriate iterator.
+ **/
+struct part_iter *pi_begin(const struct disk_info *di, int stepall)
+{
+ int setraw = 0;
+ struct part_iter *iter = NULL;
+ struct disk_dos_mbr *mbr = NULL;
+ struct disk_gpt_header *gpth = NULL;
+ struct disk_gpt_part_entry *gptl = NULL;
+
+ /* Read MBR */
+ if (!(mbr = disk_read_sectors(di, 0, 1))) {
+ error("Couldn't read first disk sector.\n");
+ goto bail;
+ }
+
+ setraw = -1;
+
+ /* Check for MBR magic*/
+ if (mbr->sig != disk_mbr_sig_magic) {
+ error("No MBR magic.\n");
+ goto bail;
+ }
+
+ /* Check for GPT protective MBR */
+ if (mbr->table[0].ostype == 0xEE) {
+ if (!(gpth = disk_read_sectors(di, 1, 1))) {
+ error("Couldn't read potential GPT header.\n");
+ goto bail;
+ }
+ }
+
+ if (gpth && gpth->rev.uint32 == 0x00010000 &&
+ !memcmp(gpth->sig, disk_gpt_sig_magic, sizeof(disk_gpt_sig_magic))) {
+ /* looks like GPT v1.0 */
+ uint64_t gpt_loff; /* offset to GPT partition list in sectors */
+ uint64_t gpt_lsiz; /* size of GPT partition list in bytes */
+ uint64_t gpt_lcnt; /* size of GPT partition in sectors */
+#ifdef DEBUG
+ puts("Looks like a GPT v1.0 disk.");
+ disk_gpt_header_dump(gpth);
+#endif
+ /* Verify checksum, fallback to backup, then bail if invalid */
+ /* TODO: mv zlib for core or rewrite the md5 check
+ if (gpt_check_hdr_crc(di, &gpth))
+ goto bail;
+
+ */
+ gpt_loff = gpth->lba_table;
+ gpt_lsiz = (uint64_t)gpth->part_size * gpth->part_count;
+ gpt_lcnt = (gpt_lsiz + di->bps - 1) / di->bps;
+
+ /*
+ * disk_read_sectors allows reading of max 255 sectors, so we use
+ * it as a sanity check base. EFI doesn't specify max (AFAIK).
+ * Apart from that, some extensive sanity checks.
+ */
+ if (!gpt_loff || !gpt_lsiz || gpt_lcnt > 255u ||
+ gpth->lba_first_usable > gpth->lba_last_usable ||
+ !sane(gpt_loff, gpt_lcnt) ||
+ gpt_loff + gpt_lcnt > gpth->lba_first_usable ||
+ !sane(gpth->lba_last_usable, gpt_lcnt) ||
+ gpth->lba_last_usable + gpt_lcnt >= gpth->lba_alt ||
+ gpth->lba_alt >= di->lbacnt ||
+ gpth->part_size < sizeof(struct disk_gpt_part_entry)) {
+ error("Invalid GPT header's values.\n");
+ goto bail;
+ }
+ if (!(gptl = disk_read_sectors(di, gpt_loff, (uint8_t)gpt_lcnt))) {
+ error("Couldn't read GPT partition list.\n");
+ goto bail;
+ }
+#if 0
+ /* This won't work for now as zlib is not on core */
+ /* Check array checksum(s). */
+ if (check_crc(gpth->table_chksum, (const uint8_t *)gptl, (unsigned int)gpt_lsiz)) {
+ error("WARNING: GPT partition list checksum invalid, trying backup.\n");
+ free(gptl);
+ /* secondary array directly precedes secondary header */
+ if (!(gptl = disk_read_sectors(di, gpth->lba_alt - gpt_lcnt, (uint8_t)gpt_lcnt))) {
+ error("Couldn't read backup GPT partition list.\n");
+ goto bail;
+ }
+ if (check_crc(gpth->table_chksum, (const uint8_t *)gptl, (unsigned int)gpt_lsiz)) {
+ error("Backup GPT partition list checksum invalid.\n");
+ goto bail;
+ }
+ }
+#endif
+ /* allocate iterator and exit */
+ iter = pi_new(typegpt, di, stepall, gpth, gptl);
+ } else {
+ /* looks like MBR */
+ iter = pi_new(typedos, di, stepall, mbr);
+ }
+
+ setraw = 0;
+bail:
+ if (setraw) {
+ error("WARNING: treating disk as raw.\n");
+ iter = pi_new(typeraw, di, stepall);
+ }
+ free(mbr);
+ free(gpth);
+ free(gptl);
+
+ return iter;
+}
--
1.7.11.3
More information about the Syslinux
mailing list