Skip to content

Commit f066304

Browse files
igoropaniukkonradybcio
authored andcommitted
qdl: decouple transport logic
Decouple the flashing logic from the underlying type of communication. This is needed for introducing simulation mode, where no real flashing is performed, but firehose packets are used for other tasks, like VIP table generation. Signed-off-by: Igor Opaniuk <[email protected]>
1 parent fde56c8 commit f066304

File tree

6 files changed

+192
-47
lines changed

6 files changed

+192
-47
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ CFLAGS += -O2 -Wall -g `pkg-config --cflags libxml-2.0 libusb-1.0`
66
LDFLAGS += `pkg-config --libs libxml-2.0 libusb-1.0`
77
prefix := /usr/local
88

9-
QDL_SRCS := firehose.c qdl.c sahara.c util.c patch.c program.c read.c ufs.c usb.c ux.c oscompat.c
9+
QDL_SRCS := firehose.c io.c qdl.c sahara.c util.c patch.c program.c read.c ufs.c usb.c ux.c oscompat.c
1010
QDL_OBJS := $(QDL_SRCS:.c=.o)
1111

12-
RAMDUMP_SRCS := ramdump.c sahara.c usb.c util.c ux.c oscompat.c
12+
RAMDUMP_SRCS := ramdump.c sahara.c io.c usb.c util.c ux.c oscompat.c
1313
RAMDUMP_OBJS := $(RAMDUMP_SRCS:.c=.o)
1414

1515
KS_OUT := ks

io.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* 3. Neither the name of the copyright holder nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
#include <stdlib.h>
31+
32+
#include "qdl.h"
33+
34+
struct qdl_device *qdl_init(enum QDL_DEVICE_TYPE type)
35+
{
36+
if (type == QDL_DEVICE_USB)
37+
return usb_init();
38+
39+
return NULL;
40+
}
41+
42+
void qdl_deinit(struct qdl_device *qdl)
43+
{
44+
if (qdl)
45+
free(qdl);
46+
}
47+
48+
void qdl_set_out_chunk_size(struct qdl_device *qdl, long size)
49+
{
50+
qdl->set_out_chunk_size(qdl, size);
51+
}
52+
53+
int qdl_open(struct qdl_device *qdl, const char *serial)
54+
{
55+
return qdl->open(qdl, serial);
56+
}
57+
58+
void qdl_close(struct qdl_device *qdl)
59+
{
60+
qdl->close(qdl);
61+
}
62+
63+
int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout)
64+
{
65+
return qdl->read(qdl, buf, len, timeout);
66+
}
67+
68+
int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
69+
{
70+
return qdl->write(qdl, buf, len);
71+
}

qdl.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ enum {
6060
};
6161

6262
bool qdl_debug;
63-
static struct qdl_device qdl;
6463

6564
static int detect_type(const char *xml_file)
6665
{
@@ -127,7 +126,9 @@ int main(int argc, char **argv)
127126
bool qdl_finalize_provisioning = false;
128127
bool allow_fusing = false;
129128
bool allow_missing = false;
130-
long out_chunk_size;
129+
long out_chunk_size = 0;
130+
struct qdl_device *qdl = NULL;
131+
enum QDL_DEVICE_TYPE qdl_dev_type = QDL_DEVICE_USB;
131132

132133
static struct option options[] = {
133134
{"debug", no_argument, 0, 'd'},
@@ -164,7 +165,6 @@ int main(int argc, char **argv)
164165
break;
165166
case OPT_OUT_CHUNK_SIZE:
166167
out_chunk_size = strtol(optarg, NULL, 10);
167-
qdl_set_out_chunk_size(&qdl, out_chunk_size);
168168
break;
169169
case 's':
170170
storage = optarg;
@@ -184,6 +184,15 @@ int main(int argc, char **argv)
184184
return 1;
185185
}
186186

187+
qdl = qdl_init(qdl_dev_type);
188+
if (!qdl) {
189+
ret = -1;
190+
goto out_cleanup;
191+
}
192+
193+
if (out_chunk_size)
194+
qdl_set_out_chunk_size(qdl, out_chunk_size);
195+
187196
ux_init();
188197

189198
if (qdl_debug)
@@ -227,23 +236,25 @@ int main(int argc, char **argv)
227236
}
228237
} while (++optind < argc);
229238

230-
ret = qdl_open(&qdl, serial);
239+
ret = qdl_open(qdl, serial);
231240
if (ret)
232241
goto out_cleanup;
233242

234-
qdl.mappings[0] = prog_mbn;
235-
ret = sahara_run(&qdl, qdl.mappings, true, NULL, NULL);
243+
qdl->mappings[0] = prog_mbn;
244+
ret = sahara_run(qdl, qdl->mappings, true, NULL, NULL);
236245
if (ret < 0)
237246
goto out_cleanup;
238247

239-
ret = firehose_run(&qdl, incdir, storage, allow_missing);
248+
ret = firehose_run(qdl, incdir, storage, allow_missing);
240249
if (ret < 0)
241250
goto out_cleanup;
242251

243252
out_cleanup:
244-
qdl_close(&qdl);
253+
qdl_close(qdl);
245254
free_programs();
246255
free_patches();
247256

257+
qdl_deinit(qdl);
258+
248259
return !!ret;
249260
}

qdl.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,37 @@
1010

1111
#define MAPPING_SZ 64
1212

13-
struct libusb_device_handle;
14-
15-
struct qdl_device {
16-
struct libusb_device_handle *usb_handle;
17-
int fd;
13+
enum QDL_DEVICE_TYPE
14+
{
15+
QDL_DEVICE_USB,
16+
};
1817

19-
int in_ep;
20-
int out_ep;
18+
struct qdl_device
19+
{
20+
enum QDL_DEVICE_TYPE dev_type;
21+
int fd;
2122

22-
size_t in_maxpktsize;
23-
size_t out_maxpktsize;
24-
size_t out_chunk_size;
23+
int (*open)(struct qdl_device *qdl, const char *serial);
24+
int (*read)(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout);
25+
int (*write)(struct qdl_device *qdl, const void *buf, size_t nbytes);
26+
void (*close)(struct qdl_device *qdl);
27+
void (*set_out_chunk_size)(struct qdl_device *qdl, long size);
2528

26-
char *mappings[MAPPING_SZ]; // array index is the id from the device
29+
char *mappings[MAPPING_SZ]; // array index is the id from the device
2730
};
2831

32+
struct libusb_device_handle;
33+
34+
struct qdl_device *qdl_init(enum QDL_DEVICE_TYPE type);
35+
void qdl_deinit(struct qdl_device *qdl);
2936
int qdl_open(struct qdl_device *qdl, const char *serial);
3037
void qdl_close(struct qdl_device *qdl);
3138
int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout);
3239
int qdl_write(struct qdl_device *qdl, const void *buf, size_t len);
3340
void qdl_set_out_chunk_size(struct qdl_device *qdl, long size);
3441

42+
struct qdl_device *usb_init(void);
43+
3544
int firehose_run(struct qdl_device *qdl, const char *incdir, const char *storage, bool allow_missing);
3645
int sahara_run(struct qdl_device *qdl, char *img_arr[], bool single_image,
3746
const char *ramdump_path, const char *ramdump_filter);

ramdump.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ static void print_usage(void)
2323

2424
int main(int argc, char **argv)
2525
{
26-
struct qdl_device qdl;
26+
struct qdl_device *qdl;
27+
28+
qdl = qdl_init(QDL_DEVICE_USB);
29+
if (!qdl)
30+
return 1;
31+
2732
char *ramdump_path = ".";
2833
char *filter = NULL;
2934
char *serial = NULL;
30-
int ret;
35+
int ret = 0;
3136
int opt;
3237

3338
static struct option options[] = {
@@ -45,7 +50,8 @@ int main(int argc, char **argv)
4550
break;
4651
case 'v':
4752
print_version();
48-
return 0;
53+
ret = 0;
54+
goto out_cleanup;
4955
case 'o':
5056
ramdump_path = optarg;
5157
break;
@@ -66,13 +72,21 @@ int main(int argc, char **argv)
6672
if (qdl_debug)
6773
print_version();
6874

69-
ret = qdl_open(&qdl, serial);
70-
if (ret)
71-
return 1;
75+
ret = qdl_open(qdl, serial);
76+
if (ret) {
77+
ret = 1;
78+
goto out_cleanup;
79+
}
7280

73-
ret = sahara_run(&qdl, NULL, true, ramdump_path, filter);
74-
if (ret < 0)
75-
return 1;
81+
ret = sahara_run(qdl, NULL, true, ramdump_path, filter);
82+
if (ret < 0) {
83+
ret = 1;
84+
goto out_cleanup;
85+
}
86+
87+
out_cleanup:
88+
qdl_close(qdl);
89+
qdl_deinit(qdl);
7690

77-
return 0;
91+
return ret;
7892
}

0 commit comments

Comments
 (0)