2025-06-11 22:53:28 +02:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2017-07-24 23:46:13 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016, Bjorn Andersson <bjorn@kryo.se>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*/
|
2016-07-08 11:26:26 -07:00
|
|
|
#include <ctype.h>
|
2025-09-07 20:56:44 -05:00
|
|
|
#include <errno.h>
|
2025-11-13 18:19:11 -06:00
|
|
|
#include <fcntl.h>
|
2025-03-27 15:41:01 +01:00
|
|
|
#include <stdbool.h>
|
2016-07-08 11:26:26 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
2018-10-05 12:55:56 +08:00
|
|
|
#include <string.h>
|
2024-03-25 17:12:40 -05:00
|
|
|
#include <stdlib.h>
|
2018-10-05 12:55:56 +08:00
|
|
|
#include <libxml/parser.h>
|
|
|
|
|
#include <libxml/tree.h>
|
2025-11-13 18:19:11 -06:00
|
|
|
#include <unistd.h>
|
2016-07-08 11:26:26 -07:00
|
|
|
|
2025-11-13 18:19:11 -06:00
|
|
|
#include "oscompat.h"
|
2025-06-24 08:00:41 +02:00
|
|
|
#include "qdl.h"
|
2025-01-17 10:13:08 +01:00
|
|
|
#include "version.h"
|
|
|
|
|
|
2016-07-08 11:26:26 -07:00
|
|
|
static uint8_t to_hex(uint8_t ch)
|
|
|
|
|
{
|
|
|
|
|
ch &= 0xf;
|
|
|
|
|
return ch <= 9 ? '0' + ch : 'a' + ch - 10;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-17 10:13:08 +01:00
|
|
|
void print_version(void)
|
|
|
|
|
{
|
|
|
|
|
extern const char *__progname;
|
2025-06-18 09:39:54 +02:00
|
|
|
|
2025-01-17 10:13:08 +01:00
|
|
|
fprintf(stdout, "%s version %s\n", __progname, VERSION);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 11:26:26 -07:00
|
|
|
void print_hex_dump(const char *prefix, const void *buf, size_t len)
|
|
|
|
|
{
|
|
|
|
|
const uint8_t *ptr = buf;
|
|
|
|
|
size_t linelen;
|
|
|
|
|
uint8_t ch;
|
|
|
|
|
char line[16 * 3 + 16 + 1];
|
|
|
|
|
int li;
|
2025-11-25 14:06:22 +01:00
|
|
|
unsigned int i;
|
|
|
|
|
unsigned int j;
|
2016-07-08 11:26:26 -07:00
|
|
|
|
|
|
|
|
for (i = 0; i < len; i += 16) {
|
2025-11-27 15:24:02 +01:00
|
|
|
linelen = MIN(16u, (size_t)(len - i));
|
2016-07-08 11:26:26 -07:00
|
|
|
li = 0;
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < linelen; j++) {
|
|
|
|
|
ch = ptr[i + j];
|
|
|
|
|
line[li++] = to_hex(ch >> 4);
|
|
|
|
|
line[li++] = to_hex(ch);
|
|
|
|
|
line[li++] = ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (; j < 16; j++) {
|
|
|
|
|
line[li++] = ' ';
|
|
|
|
|
line[li++] = ' ';
|
|
|
|
|
line[li++] = ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < linelen; j++) {
|
|
|
|
|
ch = ptr[i + j];
|
|
|
|
|
line[li++] = isprint(ch) ? ch : '.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line[li] = '\0';
|
|
|
|
|
|
|
|
|
|
printf("%s %04x: %s\n", prefix, i, line);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-05 12:55:56 +08:00
|
|
|
|
2025-06-18 09:39:54 +02:00
|
|
|
unsigned int attr_as_unsigned(xmlNode *node, const char *attr, int *errors)
|
2018-10-05 12:55:56 +08:00
|
|
|
{
|
2024-12-13 13:07:27 -06:00
|
|
|
unsigned int ret;
|
2018-10-05 12:55:56 +08:00
|
|
|
xmlChar *value;
|
|
|
|
|
|
2025-06-18 09:39:54 +02:00
|
|
|
value = xmlGetProp(node, (xmlChar *)attr);
|
2018-10-05 12:55:56 +08:00
|
|
|
if (!value) {
|
|
|
|
|
(*errors)++;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 09:39:54 +02:00
|
|
|
ret = (unsigned int)strtoul((char *)value, NULL, 0);
|
2024-12-13 13:07:27 -06:00
|
|
|
xmlFree(value);
|
|
|
|
|
return ret;
|
2018-10-05 12:55:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *attr_as_string(xmlNode *node, const char *attr, int *errors)
|
|
|
|
|
{
|
|
|
|
|
xmlChar *value;
|
2024-12-13 13:07:27 -06:00
|
|
|
char *ret = NULL;
|
2018-10-05 12:55:56 +08:00
|
|
|
|
2025-06-18 09:39:54 +02:00
|
|
|
value = xmlGetProp(node, (xmlChar *)attr);
|
2018-10-05 12:55:56 +08:00
|
|
|
if (!value) {
|
|
|
|
|
(*errors)++;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 13:07:27 -06:00
|
|
|
if (value[0] != '\0')
|
2025-06-18 09:39:54 +02:00
|
|
|
ret = strdup((char *)value);
|
2018-10-05 12:55:56 +08:00
|
|
|
|
2024-12-13 13:07:27 -06:00
|
|
|
xmlFree(value);
|
|
|
|
|
return ret;
|
2018-10-05 12:55:56 +08:00
|
|
|
}
|
2025-03-27 15:41:01 +01:00
|
|
|
|
|
|
|
|
bool attr_as_bool(xmlNode *node, const char *attr, int *errors)
|
|
|
|
|
{
|
|
|
|
|
xmlChar *value;
|
2025-11-04 13:07:23 +01:00
|
|
|
bool ret = false;
|
2025-03-27 15:41:01 +01:00
|
|
|
|
|
|
|
|
if (!xmlHasProp(node, (xmlChar *)attr))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
value = xmlGetProp(node, (xmlChar *)attr);
|
|
|
|
|
if (!value) {
|
|
|
|
|
(*errors)++;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 13:07:23 +01:00
|
|
|
ret = (xmlStrcmp(value, (xmlChar *)"true") == 0);
|
|
|
|
|
xmlFree(value);
|
|
|
|
|
return ret;
|
2025-03-27 15:41:01 +01:00
|
|
|
}
|
2025-09-07 20:56:44 -05:00
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
* parse_storage_address() - parse a storage address specifier
|
|
|
|
|
* @address: specifier to be parsed
|
|
|
|
|
* @physical_partition: physical partition
|
|
|
|
|
* @start_sector: start_sector
|
|
|
|
|
* @num_sectors: number of sectors
|
2025-09-07 21:18:14 -05:00
|
|
|
* @gpt_partition: GPT name
|
2025-09-07 20:56:44 -05:00
|
|
|
*
|
|
|
|
|
* This function parses the provided address specifier and detects the
|
|
|
|
|
* following patterns:
|
|
|
|
|
*
|
|
|
|
|
* N => physical partition N, sector 0
|
|
|
|
|
* N/S => physical partition N, sector S
|
|
|
|
|
* N/S+L => physical partition N, L sectors at sector S
|
2025-09-07 21:18:14 -05:00
|
|
|
* name => GPT partition name match across all physical partitions
|
|
|
|
|
* N/name => GPT partition name match within physical partition N
|
|
|
|
|
*
|
|
|
|
|
* @physical_partition is either the requested physical partition, or -1 if
|
|
|
|
|
* none is specified. Either @start_sector and @num_sectors, or @gpt_partition
|
|
|
|
|
* will represent the equested address, the other(s) will be zeroed.
|
2025-09-07 20:56:44 -05:00
|
|
|
*
|
|
|
|
|
* Returns: 0 on success, -1 on failure
|
|
|
|
|
*/
|
|
|
|
|
int parse_storage_address(const char *address, int *physical_partition,
|
2025-09-07 21:18:14 -05:00
|
|
|
unsigned int *start_sector, unsigned int *num_sectors,
|
|
|
|
|
char **gpt_partition)
|
2025-09-07 20:56:44 -05:00
|
|
|
{
|
|
|
|
|
unsigned long length = 0;
|
|
|
|
|
const char *ptr = address;
|
|
|
|
|
unsigned long sector = 0;
|
|
|
|
|
long partition;
|
|
|
|
|
char *end;
|
2025-09-07 21:18:14 -05:00
|
|
|
char *gpt = NULL;
|
2025-09-07 20:56:44 -05:00
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
partition = strtol(ptr, &end, 10);
|
2025-09-07 21:18:14 -05:00
|
|
|
if (end == ptr) {
|
|
|
|
|
partition = -1;
|
|
|
|
|
gpt = strdup(ptr);
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
2025-09-07 20:56:44 -05:00
|
|
|
if ((errno == ERANGE && partition == LONG_MAX) || partition < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (end[0] == '\0')
|
|
|
|
|
goto done;
|
|
|
|
|
if (end[0] != '/')
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
ptr = end + 1;
|
|
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
sector = strtoul(ptr, &end, 10);
|
2025-09-07 21:18:14 -05:00
|
|
|
if (end == ptr) {
|
|
|
|
|
gpt = strdup(ptr);
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
2025-09-07 20:56:44 -05:00
|
|
|
if (errno == ERANGE && sector == ULONG_MAX)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (end[0] == '\0')
|
|
|
|
|
goto done;
|
|
|
|
|
if (end[0] != '+')
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
ptr = end + 1;
|
|
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
length = strtoul(ptr, &end, 10);
|
|
|
|
|
if (end == ptr)
|
|
|
|
|
return -1;
|
|
|
|
|
if (errno == ERANGE && length == ULONG_MAX)
|
|
|
|
|
return -1;
|
|
|
|
|
if (length == 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (end[0] != '\0')
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
*physical_partition = partition;
|
|
|
|
|
*start_sector = sector;
|
|
|
|
|
*num_sectors = length;
|
2025-09-07 21:18:14 -05:00
|
|
|
*gpt_partition = gpt;
|
2025-09-07 20:56:44 -05:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2025-11-13 18:19:11 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* load_sahara_image() - Load the content of the given file into the image
|
|
|
|
|
* @filename: file to be loaded
|
|
|
|
|
* @image: Sahara image object to be populated
|
|
|
|
|
*
|
|
|
|
|
* Read the content of the given @filename into the given @image, update the
|
|
|
|
|
* @image->len, and then populate the @image->name for debugging purposes.
|
|
|
|
|
*
|
|
|
|
|
* Returns: 0 on success, -1 on error
|
|
|
|
|
*/
|
|
|
|
|
int load_sahara_image(const char *filename, struct sahara_image *image)
|
|
|
|
|
{
|
|
|
|
|
ssize_t n;
|
|
|
|
|
off_t len;
|
|
|
|
|
void *ptr;
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY | O_BINARY);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
ux_err("failed to read \"%s\"\n", filename);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len = lseek(fd, 0, SEEK_END);
|
|
|
|
|
if (len < 0) {
|
|
|
|
|
ux_err("failed to find end of \"%s\"\n", filename);
|
|
|
|
|
goto err_close;
|
|
|
|
|
}
|
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
ptr = malloc(len);
|
2025-11-28 17:09:17 +01:00
|
|
|
if (!ptr) {
|
|
|
|
|
ux_err("failed to init buffer for content of \"%s\"\n", filename);
|
|
|
|
|
goto err_close;
|
|
|
|
|
}
|
2025-11-13 18:19:11 -06:00
|
|
|
|
|
|
|
|
n = read(fd, ptr, len);
|
|
|
|
|
if (n != len) {
|
|
|
|
|
ux_err("failed to read content of \"%s\"\n", filename);
|
|
|
|
|
free(ptr);
|
|
|
|
|
goto err_close;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
|
|
image->name = strdup(filename);
|
|
|
|
|
image->ptr = ptr;
|
|
|
|
|
image->len = len;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
err_close:
|
|
|
|
|
close(fd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2026-02-13 10:34:00 +01:00
|
|
|
|
|
|
|
|
void sahara_images_free(struct sahara_image *images, size_t count)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
|
free(images[i].name);
|
|
|
|
|
free(images[i].ptr);
|
|
|
|
|
images[i] = (struct sahara_image){};
|
|
|
|
|
}
|
|
|
|
|
}
|