2025-06-26 15:25:21 -05:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2025-03-27 15:41:04 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2025, Maksim Paimushkin <maxim.paymushkin.development@gmail.com>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*/
|
|
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "sparse.h"
|
|
|
|
|
#include "qdl.h"
|
|
|
|
|
|
|
|
|
|
int sparse_header_parse(int fd, sparse_header_t *sparse_header)
|
|
|
|
|
{
|
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
if (read(fd, sparse_header, sizeof(sparse_header_t)) != sizeof(sparse_header_t)) {
|
|
|
|
|
ux_err("[SPARSE] Unable to read sparse header\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 18:06:27 -05:00
|
|
|
if (sparse_header->magic != SPARSE_HEADER_MAGIC) {
|
2025-03-27 15:41:04 +01:00
|
|
|
ux_err("[SPARSE] Invalid magic in sparse header\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 18:06:27 -05:00
|
|
|
if (sparse_header->major_version != SPARSE_HEADER_MAJOR_VER) {
|
2025-03-27 15:41:04 +01:00
|
|
|
ux_err("[SPARSE] Invalid major version in sparse header\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 18:06:27 -05:00
|
|
|
if (sparse_header->minor_version != SPARSE_HEADER_MINOR_VER) {
|
2025-03-27 15:41:04 +01:00
|
|
|
ux_err("[SPARSE] Invalid minor version in sparse header\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sparse_header->file_hdr_sz > sizeof(sparse_header_t))
|
|
|
|
|
lseek(fd, sparse_header->file_hdr_sz - sizeof(sparse_header_t), SEEK_CUR);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sparse_chunk_header_parse(int fd, sparse_header_t *sparse_header,
|
2025-08-26 17:42:48 -05:00
|
|
|
uint64_t *chunk_size,
|
2025-08-26 15:41:40 -05:00
|
|
|
uint32_t *value,
|
|
|
|
|
off_t *offset)
|
2025-03-27 15:41:04 +01:00
|
|
|
{
|
|
|
|
|
chunk_header_t chunk_header;
|
|
|
|
|
uint32_t fill_value = 0;
|
2025-08-26 18:06:27 -05:00
|
|
|
unsigned int type;
|
2025-03-27 15:41:04 +01:00
|
|
|
|
|
|
|
|
*chunk_size = 0;
|
|
|
|
|
*value = 0;
|
|
|
|
|
|
|
|
|
|
if (read(fd, &chunk_header, sizeof(chunk_header_t)) != sizeof(chunk_header_t)) {
|
|
|
|
|
ux_err("[SPARSE] Unable to read sparse chunk header\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
|
|
|
|
|
lseek(fd, sparse_header->chunk_hdr_sz - sizeof(chunk_header_t), SEEK_CUR);
|
|
|
|
|
|
2025-08-26 18:06:27 -05:00
|
|
|
type = chunk_header.chunk_type;
|
2025-08-26 17:42:48 -05:00
|
|
|
*chunk_size = (uint64_t)chunk_header.chunk_sz * sparse_header->blk_sz;
|
2025-03-27 15:41:04 +01:00
|
|
|
|
2025-08-26 18:06:27 -05:00
|
|
|
switch (type) {
|
|
|
|
|
case CHUNK_TYPE_RAW:
|
2025-03-27 15:41:04 +01:00
|
|
|
if (chunk_header.total_sz != (sparse_header->chunk_hdr_sz + *chunk_size)) {
|
|
|
|
|
ux_err("[SPARSE] Bogus chunk size, type Raw\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Save the current file offset in the 'value' variable */
|
2025-08-26 15:41:40 -05:00
|
|
|
*offset = lseek(fd, 0, SEEK_CUR);
|
2025-03-27 15:41:04 +01:00
|
|
|
|
|
|
|
|
/* Move the file cursor forward by the size of the chunk */
|
|
|
|
|
lseek(fd, *chunk_size, SEEK_CUR);
|
2025-08-26 18:06:27 -05:00
|
|
|
break;
|
|
|
|
|
case CHUNK_TYPE_DONT_CARE:
|
2025-03-27 15:41:04 +01:00
|
|
|
if (chunk_header.total_sz != sparse_header->chunk_hdr_sz) {
|
|
|
|
|
ux_err("[SPARSE] Bogus chunk size, type Don't Care\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2025-08-26 18:06:27 -05:00
|
|
|
break;
|
|
|
|
|
case CHUNK_TYPE_FILL:
|
2025-03-27 15:41:04 +01:00
|
|
|
if (chunk_header.total_sz != (sparse_header->chunk_hdr_sz + sizeof(fill_value))) {
|
|
|
|
|
ux_err("[SPARSE] Bogus chunk size, type Fill\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (read(fd, &fill_value, sizeof(fill_value)) != sizeof(fill_value)) {
|
|
|
|
|
ux_err("[SPARSE] Unable to read fill value\n");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Save the current fill value in the 'value' variable */
|
|
|
|
|
*value = fill_value;
|
2025-08-26 18:06:27 -05:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ux_err("[SPARSE] Unknown chunk type: %#x\n", type);
|
|
|
|
|
return -EINVAL;
|
2025-03-27 15:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-26 18:06:27 -05:00
|
|
|
return type;
|
2025-03-27 15:41:04 +01:00
|
|
|
}
|