From 8b21123310fcc8f37f24de5a30c20815875cac4e Mon Sep 17 00:00:00 2001 From: vit9696 Date: Fri, 25 Jan 2019 10:34:42 +0300 Subject: [PATCH] Add disk label reading tool --- .gitignore | 1 + Tools/readlabel/Makefile | 13 +++ Tools/readlabel/readlabel.c | 165 ++++++++++++++++++++++++++++++++++++ macbuild.tool | 1 + 4 files changed, 180 insertions(+) create mode 100644 Tools/readlabel/Makefile create mode 100644 Tools/readlabel/readlabel.c diff --git a/.gitignore b/.gitignore index 4611417..5133fc2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ UDK Binaries Tools/EfiResTool/EfiResTool Tools/AppleEfiSignTool/AppleEfiSignTool +Tools/readlabel/readlabel *.o diff --git a/Tools/readlabel/Makefile b/Tools/readlabel/Makefile new file mode 100644 index 0000000..1207f9d --- /dev/null +++ b/Tools/readlabel/Makefile @@ -0,0 +1,13 @@ +CC ?= gcc +CFLAGS=-c -Wall -Wextra -pedantic -O3 + +all: readlabel + +readlabel: readlabel.o + $(CC) readlabel.o -o readlabel + +.c: + $(CC) $(CFLAGS) $< -o $@ + +clean: + rm -rf *.o readlabel diff --git a/Tools/readlabel/readlabel.c b/Tools/readlabel/readlabel.c new file mode 100644 index 0000000..4ca4d82 --- /dev/null +++ b/Tools/readlabel/readlabel.c @@ -0,0 +1,165 @@ +/** @file + +Read macOS .disk_label (.disk_label_2x) file and convert to .ppm. +Reference: http://refit.sourceforge.net/info/vollabel.html + +Copyright (c) 2019, vit9696 + +All rights reserved. + +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ +#include +#include +#include + +static uint8_t palette[256] = { + [0x00] = 255, + [0xf6] = 238, + [0xf7] = 221, + [0x2a] = 204, + [0xf8] = 187, + [0xf9] = 170, + [0x55] = 153, + [0xfa] = 136, + [0xfb] = 119, + [0x80] = 102, + [0xfc] = 85, + [0xfd] = 68, + [0xab] = 51, + [0xfe] = 34, + [0xff] = 17, + [0xd6] = 0 +}; + +static int read_file(const char *filename, uint8_t **buffer, size_t *size) { + FILE *fh = fopen(filename, "rb"); + if (!fh) { + fprintf(stderr, "Missing file %s!\n", filename); + return -1; + } + + if (fseek(fh, 0, SEEK_END)) { + fprintf(stderr, "Failed to find end of %s!\n", filename); + fclose(fh); + return -1; + } + + long pos = ftell(fh); + + if (pos <= 0) { + fprintf(stderr, "Invalid file size (%ld) of %s!\n", pos, filename); + fclose(fh); + return -1; + } + + if (fseek(fh, 0, SEEK_SET)) { + fprintf(stderr, "Failed to rewind %s!\n", filename); + fclose(fh); + return -1; + } + + *size = (size_t)pos; + *buffer = (uint8_t *)malloc(*size); + + if (!*buffer) { + fprintf(stderr, "Failed to allocate %zu bytes for %s!\n", *size, filename); + fclose(fh); + return -1; + } + + if (fread(*buffer, *size, 1, fh) != 1) { + fprintf(stderr, "Failed to read %zu bytes from %s!\n", *size, filename); + fclose(fh); + free(*buffer); + return -1; + } + + fclose(fh); + return 0; +} + +static int write_ppm(const char *filename, size_t width, size_t height, uint8_t *pixel) { + FILE *fh = fopen(filename, "wb"); + if (!fh) { + fprintf(stderr, "Failed to open out file %s!\n", filename); + return -1; + } + + if (fprintf(fh, "P6\n%zu %zu\n255\n", width, height) < 0) { + fprintf(stderr, "Failed to write ppm header to %s!\n", filename); + fclose(fh); + return -1; + } + + for (size_t y = 0; y < height; ++y) { + for (size_t x = 0; x < width; ++x) { + uint8_t col[3]; + col[0] = col[1] = col[2] = palette[*pixel]; + if (fwrite(col, sizeof(col), 1, fh) != 1) { + fprintf(stderr, "Failed to write ppm pixel %zux%zu to %s!\n", x, y, filename); + fclose(fh); + return -1; + } + ++pixel; + } + } + + fclose(fh); + + return 0; +} + +int main(int argc, char *argv[]) { + if (argc < 3) { + fprintf(stderr, "Pass file and outfile!\n"); + return -1; + } + + uint8_t *buffer; + size_t size; + + if (read_file(argv[1], &buffer, &size)) { + return -1; + } + + size_t header_size = sizeof(uint8_t) + sizeof(uint16_t)*2; + + if (size < header_size) { + fprintf(stderr, "Too low size %zu!\n", size); + free(buffer); + return -1; + } + + if (buffer[0] != 1) { + fprintf(stderr, "Invalid magic %02X!\n", buffer[0]); + free(buffer); + return -1; + } + + size_t width = ((size_t) buffer[1] << 8U) | (size_t) buffer[2]; + size_t height = ((size_t) buffer[3] << 8U) | (size_t) buffer[4]; + + if (width * height == 0 || size - header_size != width*height) { + fprintf(stderr, "Image mismatch %zux%zu with size %zu!\n", width, height, size); + free(buffer); + return -1; + } + + (void)remove(argv[2]); + + if (write_ppm(argv[2], width, height, buffer + header_size)) { + free(buffer); + return -1; + } + + free(buffer); + return 0; +} diff --git a/macbuild.tool b/macbuild.tool index 003eec5..784aeec 100755 --- a/macbuild.tool +++ b/macbuild.tool @@ -96,6 +96,7 @@ if [ ! -d "Binaries" ]; then cd Binaries || exit 1 ln -s ../UDK/Build/AppleSupportPkg/RELEASE_XCODE5/X64 RELEASE || exit 1 ln -s ../UDK/Build/AppleSupportPkg/DEBUG_XCODE5/X64 DEBUG || exit 1 + ln -s ../UDK/Build/AppleSupportPkg/NOOPT_XCODE5/X64 NOOPT || exit 1 cd .. || exit 1 fi