Skip to content

[RFC] qdl: add support for dry run execution #98

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ CFLAGS += -O2 -Wall -g `pkg-config --cflags libxml-2.0 libusb-1.0`
LDFLAGS += `pkg-config --libs libxml-2.0 libusb-1.0`
prefix := /usr/local

QDL_SRCS := firehose.c qdl.c sahara.c util.c patch.c program.c read.c ufs.c usb.c ux.c
QDL_SRCS := firehose.c io.c qdl.c sahara.c util.c patch.c program.c read.c sim.c ufs.c usb.c ux.c
QDL_OBJS := $(QDL_SRCS:.c=.o)

RAMDUMP_SRCS := ramdump.c sahara.c usb.c util.c ux.c
RAMDUMP_SRCS := ramdump.c sahara.c io.c usb.c sim.c util.c ux.c
RAMDUMP_OBJS := $(RAMDUMP_SRCS:.c=.o)

KS_OUT := ks
Expand Down
6 changes: 5 additions & 1 deletion firehose.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ static int firehose_read(struct qdl_device *qdl, int timeout_ms,
gettimeofday(&now, NULL);
timeradd(&now, &delta, &timeout);

if (qdl->dev_type == QDL_DEVICE_SIM)
return 0;

do {
n = qdl_read(qdl, buf, sizeof(buf), 100);
if (n < 0) {
Expand Down Expand Up @@ -300,7 +303,8 @@ static int firehose_configure(struct qdl_device *qdl, bool skip_storage_init, co
return -1;
}

max_payload_size = size;
if (qdl->dev_type == QDL_DEVICE_USB)
max_payload_size = size;
}

ux_debug("accepted max payload size: %zu\n", max_payload_size);
Expand Down
73 changes: 73 additions & 0 deletions io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "qdl.h"

struct qdl_device *qdl_init(enum QDL_DEVICE_TYPE type)
{
if (type == QDL_DEVICE_USB)
return usb_init();

if (type == QDL_DEVICE_SIM)
return sim_init();

return NULL;
}

void qdl_deinit(struct qdl_device *qdl)
{
if (qdl)
free(qdl);
}

void qdl_set_out_chunk_size(struct qdl_device *qdl, long size)
{
qdl->set_out_chunk_size(qdl, size);
}

int qdl_open(struct qdl_device *qdl, const char *serial)
{
return qdl->open(qdl, serial);
}

void qdl_close(struct qdl_device *qdl)
{
qdl->close(qdl);
}

int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout)
{
return qdl->read(qdl, buf, len, timeout);
}

int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
{
return qdl->write(qdl, buf, len);
}
33 changes: 24 additions & 9 deletions qdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ enum {
};

bool qdl_debug;
static struct qdl_device qdl;

static int detect_type(const char *xml_file)
{
Expand Down Expand Up @@ -103,7 +102,7 @@ static void print_usage(void)
{
extern const char *__progname;
fprintf(stderr,
"%s [--debug] [--version] [--allow-missing] [--storage <emmc|nand|ufs>] [--finalize-provisioning] [--include <PATH>] [--serial <NUM>] [--out-chunk-size <SIZE>] <prog.mbn> [<program> <patch> ...]\n",
"%s [--debug] [--dry-run] [--version] [--allow-missing] [--storage <emmc|nand|ufs>] [--finalize-provisioning] [--include <PATH>] [--serial <NUM>] [--out-chunk-size <SIZE>] <prog.mbn> [<program> <patch> ...]\n",
__progname);
}

Expand All @@ -121,7 +120,9 @@ int main(int argc, char **argv)
int opt;
bool qdl_finalize_provisioning = false;
bool allow_missing = false;
long out_chunk_size;
long out_chunk_size = 0;
struct qdl_device *qdl = NULL;
enum QDL_DEVICE_TYPE qdl_dev_type = QDL_DEVICE_USB;

static struct option options[] = {
{"debug", no_argument, 0, 'd'},
Expand All @@ -132,6 +133,7 @@ int main(int argc, char **argv)
{"serial", required_argument, 0, 'S'},
{"storage", required_argument, 0, 's'},
{"allow-missing", no_argument, 0, 'f'},
{"dry-run", no_argument, 0, 'n'},
{0, 0, 0, 0}
};

Expand All @@ -140,6 +142,9 @@ int main(int argc, char **argv)
case 'd':
qdl_debug = true;
break;
case 'n':
qdl_dev_type = QDL_DEVICE_SIM;
break;
case 'v':
print_version();
return 0;
Expand All @@ -154,7 +159,6 @@ int main(int argc, char **argv)
break;
case OPT_OUT_CHUNK_SIZE:
out_chunk_size = strtol(optarg, NULL, 10);
qdl_set_out_chunk_size(&qdl, out_chunk_size);
break;
case 's':
storage = optarg;
Expand All @@ -174,6 +178,15 @@ int main(int argc, char **argv)
return 1;
}

qdl = qdl_init(qdl_dev_type);
if (!qdl) {
ret = -1;
goto out_cleanup;
}

if (out_chunk_size)
qdl_set_out_chunk_size(qdl, out_chunk_size);

ux_init();

if (qdl_debug)
Expand Down Expand Up @@ -213,23 +226,25 @@ int main(int argc, char **argv)
}
} while (++optind < argc);

ret = qdl_open(&qdl, serial);
ret = qdl_open(qdl, serial);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the below renames from qdl_ to usb_ prefix is a logically separate change from the introduction of the simulation mechanism. Please perform such trivial adjustments in a separate patch (in the same PR).

if (ret)
goto out_cleanup;

qdl.mappings[0] = prog_mbn;
ret = sahara_run(&qdl, qdl.mappings, true, NULL, NULL);
qdl->mappings[0] = prog_mbn;
ret = sahara_run(qdl, qdl->mappings, true, NULL, NULL);
if (ret < 0)
goto out_cleanup;

ret = firehose_run(&qdl, incdir, storage, allow_missing);
ret = firehose_run(qdl, incdir, storage, allow_missing);
if (ret < 0)
goto out_cleanup;

out_cleanup:
qdl_close(&qdl);
qdl_close(qdl);
free_programs();
free_patches();

qdl_deinit(qdl);

return !!ret;
}
51 changes: 42 additions & 9 deletions qdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,63 @@
#include "read.h"
#include <libxml/tree.h>

#define container_of(ptr, typecast, member) ({ \
void *_ptr = (void *)(ptr); \
((typecast *)(_ptr - offsetof(typecast, member))); })

#define MAPPING_SZ 64

enum QDL_DEVICE_TYPE
{
QDL_DEVICE_USB,
QDL_DEVICE_SIM,
};

struct qdl_device
{
enum QDL_DEVICE_TYPE dev_type;
int fd;

int (*open)(struct qdl_device *qdl, const char *serial);
int (*read)(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout);
int (*write)(struct qdl_device *qdl, const void *buf, size_t nbytes);
void (*close)(struct qdl_device *qdl);
void (*set_out_chunk_size)(struct qdl_device *qdl, long size);

char *mappings[MAPPING_SZ]; // array index is the id from the device
};

struct libusb_device_handle;

struct qdl_device {
struct libusb_device_handle *usb_handle;
int fd;
struct qdl_device_usb
{
struct qdl_device base;
struct libusb_device_handle *usb_handle;

int in_ep;
int out_ep;
int in_ep;
int out_ep;

size_t in_maxpktsize;
size_t out_maxpktsize;
size_t out_chunk_size;
size_t in_maxpktsize;
size_t out_maxpktsize;
size_t out_chunk_size;
};

char *mappings[MAPPING_SZ]; // array index is the id from the device
struct qdl_device_sim
{
struct qdl_device base;
};

struct qdl_device *qdl_init(enum QDL_DEVICE_TYPE type);
void qdl_deinit(struct qdl_device *qdl);
int qdl_open(struct qdl_device *qdl, const char *serial);
void qdl_close(struct qdl_device *qdl);
int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout);
int qdl_write(struct qdl_device *qdl, const void *buf, size_t len);
void qdl_set_out_chunk_size(struct qdl_device *qdl, long size);

struct qdl_device *usb_init(void);
struct qdl_device *sim_init(void);

int firehose_run(struct qdl_device *qdl, const char *incdir, const char *storage, bool allow_missing);
int sahara_run(struct qdl_device *qdl, char *img_arr[], bool single_image,
const char *ramdump_path, const char *ramdump_filter);
Expand Down
8 changes: 5 additions & 3 deletions ramdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ static void print_usage(void)

int main(int argc, char **argv)
{
struct qdl_device qdl;
struct qdl_device_usb qdl;
qdl.base.dev_type = QDL_DEVICE_USB;

char *ramdump_path = ".";
char *filter = NULL;
char *serial = NULL;
Expand Down Expand Up @@ -61,11 +63,11 @@ int main(int argc, char **argv)
if (qdl_debug)
print_version();

ret = qdl_open(&qdl, serial);
ret = qdl_open(&qdl.base, serial);
if (ret)
return 1;

ret = sahara_run(&qdl, NULL, true, ramdump_path, filter);
ret = sahara_run(&qdl.base, NULL, true, ramdump_path, filter);
if (ret < 0)
return 1;

Expand Down
3 changes: 3 additions & 0 deletions sahara.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ int sahara_run(struct qdl_device *qdl, char *img_arr[], bool single_image,
bool done = false;
int n;

if (qdl->dev_type == QDL_DEVICE_SIM)
return 0;

if (ramdump_path) {
ramdump_dir = open(ramdump_path, O_DIRECTORY);
if (ramdump_dir < 0)
Expand Down
77 changes: 77 additions & 0 deletions sim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>

#include "qdl.h"

static int sim_open(struct qdl_device *qdl, const char *serial)
{
ux_info("This is a dry-run execution of QDL. No actual flashing has been performed\n");

return 0;
}

static void sim_close(struct qdl_device *qdl)
{
return;
}

static int sim_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout)
{
return len;
}

static int sim_write(struct qdl_device *qdl, const void *buf, size_t len)
{
return len;
}

static void sim_set_out_chunk_size(struct qdl_device *qdl, long size)
{
return;
}

struct qdl_device *sim_init(void)
{
struct qdl_device *qdl = malloc(sizeof(struct qdl_device_sim));
if (!qdl)
return NULL;

memset(qdl, 0, sizeof(struct qdl_device_sim));

qdl->dev_type = QDL_DEVICE_SIM;
qdl->open = sim_open;
qdl->read = sim_read;
qdl->write = sim_write;
qdl->close = sim_close;
qdl->set_out_chunk_size = sim_set_out_chunk_size;

return qdl;
}
Loading