pax_global_header00006660000000000000000000000064152115602230014507gustar00rootroot0000000000000052 comment=f540e59dbeb462242239778ae0993549304f3abb linux-msm-qdl-f540e59/000077500000000000000000000000001521156022300145615ustar00rootroot00000000000000linux-msm-qdl-f540e59/.checkpatch.conf000066400000000000000000000020061521156022300176010ustar00rootroot00000000000000--no-tree --strict --max-line-length=120 --ignore FILE_PATH_CHANGES --ignore EMAIL_SUBJECT --ignore SPLIT_STRING # NEW_TYPEDEFS reports "do not add new typedefs" # typedef struct __attribute__((__packed__)) sparse_header { --ignore NEW_TYPEDEFS # PREFER_DEFINED_ATTRIBUTE_MACRO reports this kind of messages: # WARNING: Prefer __packed over __attribute__((__packed__)) --ignore PREFER_DEFINED_ATTRIBUTE_MACRO # PREFER_KERNEL_TYPES reports this kind of messages (when using --strict): # "Prefer kernel type 'u32' over 'uint32_t'" --ignore PREFER_KERNEL_TYPES # BRACES reports this kind of messages: # braces {} are not necessary for any arm of this statement --ignore BRACES # CAMELCASE reports this kind of messages: # Avoid CamelCase: --ignore CAMELCASE # AVOID_EXTERNS reports this kind of messages: # externs should be avoided in .c files # extern const char *__progname; --ignore AVOID_EXTERNS # COMMIT_LOG_LONG_LINE reports line lengths > 75 in commit log # Lets ignore this --ignore COMMIT_LOG_LONG_LINE linux-msm-qdl-f540e59/.github/000077500000000000000000000000001521156022300161215ustar00rootroot00000000000000linux-msm-qdl-f540e59/.github/workflows/000077500000000000000000000000001521156022300201565ustar00rootroot00000000000000linux-msm-qdl-f540e59/.github/workflows/build.yml000066400000000000000000000124571521156022300220110ustar00rootroot00000000000000name: Buildtest on: pull_request: push: jobs: build-linux: strategy: fail-fast: false matrix: include: - { arch: x64, os: 24, runner: ubuntu-24.04 } - { arch: arm64, os: 24, runner: ubuntu-24.04-arm } - { arch: x64, os: 22, runner: ubuntu-22.04 } - { arch: arm64, os: 22, runner: ubuntu-22.04-arm } runs-on: ${{ matrix.runner }} steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Dependencies run: | sudo apt-get update sudo apt-get install -y libcmocka-dev libxml2-dev libusb-1.0-0-dev libzip-dev help2man pipx zip pipx ensurepath pipx install meson==1.4.0 - name: Configure run: meson setup build - name: Build run: meson compile -C build - name: Run tests run: meson test -C build --print-errorlogs - name: Generate man pages run: meson compile manpages -C build - name: Package run: | mkdir dist cp `pkg-config --variable=libdir libusb-1.0`/libusb-1.0.so.0 dist chmod 0644 dist/* cp ./build/qdl dist patchelf --set-rpath '$ORIGIN' dist/qdl - name: Upload artifact uses: actions/upload-artifact@v4 with: name: qdl-binary-ubuntu-${{matrix.os}}-${{ matrix.arch }} path: dist/* build-mac: strategy: fail-fast: false matrix: include: - { sys: macos-14, arch: arm64 } - { sys: macos-15-intel, arch: intel } runs-on: ${{ matrix.sys }} steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Dependencies run: | brew install cmocka libxml2 libzip meson ninja help2man zip - name: Configure run: meson setup build - name: Build run: meson compile -C build - name: Generate man pages run: meson compile manpages -C build - name: Run tests run: meson test -C build --print-errorlogs - name: Package run: | set -x mkdir dist cp `pkg-config --variable=libdir libusb-1.0`/libusb-1.0.0.dylib dist cp `pkg-config --variable=libdir liblzma`/liblzma.5.dylib dist chmod 0644 dist/* cp ./build/qdl dist if uname -a | grep -q arm64; then LIBUSB_DIR=/opt/homebrew/opt/libusb/lib LIBLZMA_DIR=/usr/lib else LIBUSB_DIR=/usr/local/opt/libusb/lib LIBLZMA_DIR=/usr/local/opt/xz/lib fi install_name_tool -add_rpath @executable_path dist/qdl install_name_tool -change $LIBUSB_DIR/libusb-1.0.0.dylib @rpath/libusb-1.0.0.dylib dist/qdl install_name_tool -change $LIBLZMA_DIR/liblzma.5.dylib @rpath/liblzma.5.dylib dist/qdl otool -L dist/qdl dist/qdl || true - name: Upload artifact uses: actions/upload-artifact@v4 with: name: qdl-binary-macos-${{ matrix.arch }} path: dist/* build-windows: strategy: fail-fast: false matrix: include: - { sys: windows-latest, arch: x64 } - { sys: windows-11-arm, arch: arm64 } runs-on: ${{ matrix.sys }} steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Setup MSYS2 id: msys2 uses: msys2/setup-msys2@v2 with: msystem: MINGW64 install: > base-devel git help2man mingw-w64-x86_64-gcc mingw-w64-x86_64-meson mingw-w64-x86_64-ninja mingw-w64-x86_64-cmocka mingw-w64-x86_64-libxml2 mingw-w64-x86_64-libusb mingw-w64-x86_64-libzip mingw-w64-x86_64-xz zip - name: Configure run: meson setup build shell: msys2 {0} - name: Build run: | git config --global core.autocrlf true meson compile -C build shell: msys2 {0} - name: Generate man pages run: meson compile manpages -C build shell: msys2 {0} - name: Run tests run: meson test -C build --print-errorlogs shell: msys2 {0} - name: Package shell: pwsh run: | $MSYS2_LOCATION = "${{ steps.msys2.outputs.msys2-location }}" $BIN_DIR = Join-Path $MSYS2_LOCATION "mingw64\bin" $DistDir = "dist" New-Item -ItemType Directory -Path $DistDir | Out-Null Copy-Item (Join-Path $BIN_DIR "zlib1.dll") $DistDir Copy-Item (Join-Path $BIN_DIR "libxml2-16.dll") $DistDir Copy-Item (Join-Path $BIN_DIR "libusb-1.0.dll") $DistDir Copy-Item (Join-Path $BIN_DIR "libzip.dll") $DistDir Copy-Item (Join-Path $BIN_DIR "liblzma-5.dll") $DistDir Copy-Item (Join-Path $BIN_DIR "libiconv-2.dll") $DistDir Copy-Item (Join-Path $BIN_DIR "libbz2-1.dll") $DistDir Copy-Item (Join-Path $BIN_DIR "libzstd.dll") $DistDir Copy-Item "./build/qdl.exe" $DistDir - name: Upload artifact uses: actions/upload-artifact@v4 with: name: qdl-binary-windows-${{ matrix.arch }} path: dist/* linux-msm-qdl-f540e59/.github/workflows/checkpatch.yml000066400000000000000000000007461521156022300230050ustar00rootroot00000000000000name: Checkpatch Review on: [pull_request] jobs: check-patch: name: checkpatch review runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: ref: ${{ github.event.pull_request.head.sha }} fetch-depth: 0 - name: Run checkpatch review env: CHECKPATCH_BASE: ${{ github.event.pull_request.base.sha }} CHECKPATCH_HEAD: ${{ github.event.pull_request.head.sha }} run: ./scripts/checkpatch_wrapper.sh check-range linux-msm-qdl-f540e59/.github/workflows/codeql.yml000066400000000000000000000013551521156022300221540ustar00rootroot00000000000000name: CodeQL on: pull_request: push: jobs: codeql: permissions: # required for all workflows security-events: write # required to fetch internal or private CodeQL packs packages: read runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Dependencies run: | sudo apt-get update sudo apt-get install -y libxml2-dev libusb-1.0-0-dev - name: CodeQL init uses: github/codeql-action/init@v3 with: languages: c-cpp build-mode: autobuild - name: CodeQL build uses: github/codeql-action/autobuild@v3 - name: CodeQL analysis uses: github/codeql-action/analyze@v3 linux-msm-qdl-f540e59/.github/workflows/commitlint.yml000066400000000000000000000005651521156022300230660ustar00rootroot00000000000000name: Commit message lint on: pull_request: types: [opened, synchronize, reopened, edited] jobs: commitlint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - name: Lint commit messages uses: wagoid/commitlint-github-action@v6 with: configFile: commitlint.config.mjs linux-msm-qdl-f540e59/.github/workflows/markdown-lint.yml000066400000000000000000000004721521156022300234720ustar00rootroot00000000000000name: Markdown Lint on: [pull_request] jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Install mdl run: | sudo apt-get update sudo apt-get install -y markdownlint - name: Run markdown-lint run: ./scripts/markdownlint_wrapper.sh check linux-msm-qdl-f540e59/.gitignore000066400000000000000000000002461521156022300165530ustar00rootroot00000000000000/build/ /tests/data/*.elf /tests/data/*.bin /tests/data/*.img *.o qdl qdl-ramdump ks *.exe compile_commands.json .cache version.h .scripts .checkpatch-camelcase.git. linux-msm-qdl-f540e59/LICENSE000066400000000000000000000031341521156022300155670ustar00rootroot00000000000000/* * Copyright (c) 2016-2017, Linaro Ltd. * Copyright (c) 2016, Bjorn Andersson * 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. */ linux-msm-qdl-f540e59/README.md000066400000000000000000000350751521156022300160520ustar00rootroot00000000000000# Qualcomm Download [![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) [![Build on push](https://github.com/linux-msm/qdl/actions/workflows/build.yml/badge.svg)](https://github.com/linux-msm/qdl/actions/workflows/build.yml/badge.svg) This tool communicates with Qualcomm EDL USB devices (Vendor ID `05c6`, Product IDs `9008`, `900e`, `901d`) to upload a flash loader and use it to flash images. ## Build ### Linux ```bash sudo apt install libxml2-dev libusb-1.0-0-dev libzip-dev meson ninja-build help2man meson setup build meson compile -C build ``` ### MacOS For Homebrew users: ```bash brew install libxml2 libusb libzip meson ninja help2man meson setup build meson compile -C build ``` For MacPorts users: ```bash sudo port install libxml2 libusb libzip meson ninja help2man meson setup build meson compile -C build ``` ### Windows First, install the [MSYS2 environment](https://www.msys2.org/). Then, run the MSYS2 MinGW64 terminal (located at `\mingw64.exe`) and install additional packages needed for QDL compilation using the `pacman` tool: ```bash pacman -S base-devel --needed pacman -S git pacman -S help2man pacman -S mingw-w64-x86_64-gcc pacman -S mingw-w64-x86_64-meson pacman -S mingw-w64-x86_64-ninja pacman -S mingw-w64-x86_64-libusb pacman -S mingw-w64-x86_64-libxml2 pacman -S mingw-w64-x86_64-libzip ``` Then use the `meson` tool to build QDL: ```bash meson setup build meson compile -C build ``` ## Use QDL ### EDL mode The device intended for flashing must be booted into **Emergency Download (EDL)** mode. EDL is a special boot mode available on Qualcomm-based devices that provides low-level access for firmware flashing and recovery. It bypasses the standard boot process, allowing operations such as flashing firmware even on unresponsive devices or those with locked bootloaders. Please consult your device’s documentation for instructions on how to enter EDL mode. ### Flash device Run QDL with the `--help` option to view detailed usage information. Below is an example of how to invoke QDL to flash a FLAT build: ```bash qdl prog_firehose_ddr.elf rawprogram*.xml patch*.xml ``` If you have multiple boards connected to the host, provide the serial number of the board to flash through the `--serial` option: ```bash qdl --serial=0AA94EFD prog_firehose_ddr.elf rawprogram*.xml patch*.xml ``` ### Flashing installer packages If you have an installer package instead of individual binaries and XML definitions, you can flash this using the *flash* subcommand: ```bash qdl flash ``` If the *installer package* is unpacked it can be installed as: ```bash qdl flash flashmap.json ``` These can of course be combined with e.g. *--serial*. A subset of the installer package can be selected for installation by appending a **::storage1[,storage2...]** suffix to the file name. ### Flashing contents.xml QDL also supports flashing builds described by *contents.xml* files: ```bash qdl flash contents.xml ``` As the contents XML can describe the content for multiple storage types and multiple flavors, it might be necessary to select which content to flash. This is done by appending the **::specifier1,specifier2...** suffix to the file name. The specifier is matched against **storage types** and **flavors**. At most one resolved specifier per storage is allowed, and only the selected parts are flashed. As an example: ```bash qdl flash contents.xml::ufs,safe_rtos ``` will flash the UFS storage with the only applicable flavor, and will flash *safe_rtos* onto the spinor. ### Flash simulation (dry run) Use the `--dry-run` option to run QDL without connecting to or flashing any device. This is useful for validating your XML descriptors and programmer arguments, or for generating VIP digest tables (see below): ```bash qdl --dry-run prog_firehose_ddr.elf rawprogram*.xml patch*.xml ``` ### Reading and writing raw binaries In addition to flashing builds using their XML-based descriptions, QDL supports reading and writing binaries directly. ```bash qdl prog_firehose_ddr.elf [read | write] [address specifier] ... ``` Multiple read and write commands can be specified at once. The ***address specifier*** can take the forms: - N - single number, specifies the physical partition number N, starting at sector 0. To read data, the number of sectors must be specified explicitly using the N/S+L form. - N/S - two numbers, specifies the physical partition number N and the start sector S. To read data, the number of sectors must be specified explicitly using the N/S+L form. - N/S+L - three numbers, specifies the physical partition number N, the start sector S and the number of sectors L, that ***binary*** should be written to, or which should be read into ***binary***. - partition name - a string, will match against partition names across the GPT partition tables on all physical partitions. - N/partition_name - a number followed by a string, will match against partition names of the GPT partition table in the specified physical partition N. ### Validated Image Programming (VIP) QDL supports **Validated Image Programming (VIP)** mode, which is activated when Secure Boot is enabled on the target. VIP controls which packets are allowed to be issued to the target by hashing all received data and comparing each resulting digest against the next entry in a pre-loaded digest table. If the digest matches, the packet is accepted; otherwise, the packet is rejected, and the target halts. To use VIP programming, a digest table must be generated prior to flashing the device. To generate a table of digests, run QDL with the `--create-digests` option, providing a path to store the VIP tables. Note that `--create-digests` implicitly enables dry-run mode, so no device connection is required: ```bash mkdir vip qdl --create-digests=./vip prog_firehose_ddr.elf rawprogram*.xml patch*.xml ``` As a result, three types of files are generated: - `DIGEST_TABLE.bin` - contains the SHA256 table of digests for all Firehose packets to be sent to the target. It is an intermediary table and is used only for the subsequent generation of `DigestsToSign.bin` and `ChainedTableOfDigests.bin` files, and is not used directly by QDL for VIP programming. - `DigestsToSign.bin` - first 53 digests + SHA256 hash of `ChainedTableOfDigests0.bin`. This file must be converted to MBN format and then signed with sectools: ```bash sectools mbn-tool generate --data DigestsToSign.bin --mbn-version 6 --outfile DigestsToSign.bin.mbn sectools secure-image --sign DigestsToSign.bin.mbn --image-id=VIP ``` Please check the security profile for your SoC to determine which version of the MBN format should be used. - `ChainedTableOfDigests.bin` - contains the remaining digests, split across multiple files of up to 255 digests each. Non-final files have the SHA256 hash of the next chained table appended. The final file has a trailing zero byte appended to ensure its size is not a multiple of the sector size. To flash a board using VIP mode, provide the path where the previously generated and signed tables are stored using the `--vip-table-path` option: ```bash qdl --vip-table-path=./vip prog_firehose_ddr.elf rawprogram*.xml patch*.xml ``` Note that `--vip-table-path` and `--create-digests` are mutually exclusive. #### Validating VIP tables without hardware Before flashing a real device it is possible to verify that the signed digest tables match the data that will be sent, using `--dry-run` together with `--vip-table-path`: ```bash qdl --dry-run --vip-table-path=./vip prog_firehose_ddr.elf rawprogram*.xml patch*.xml ``` QDL will simulate the full Firehose session, compute SHA256 over every packet it would send, and compare each hash against the corresponding entry in the loaded digest tables. Any mismatch is reported to stderr. All mismatches are printed before the run exits so that every problem is visible at once. This catches table/data mismatches early -- before committing to a real flash -- and is useful both as a local sanity check and as a step in CI pipelines. ### Multi-programmer targets On some targets multiple files need to be loaded in order to reach the Firehose programmer; these targets will request multiple images over Sahara. Three mechanisms for providing these images are provided: #### Command line argument The *programmer* argument allows specifying a comma-separated list of colon-separated "id" and "filename" pairs. Each filename should refer to the Sahara image of the specified Sahara image id. ```bash qdl 13:prog_firehose_ddr.elf,42:the-answer rawprogram.xml ``` #### Sahara configuration XML file Flattened METAs include the various images that need to be loaded to enter Firehose mode, as well as a sahara_config XML file, which defines the Sahara image id for each of these images. If the specified device programmer is determined to be a Sahara configuration XML file, it will be parsed and the referenced files will be loaded and serviced to the device upon request. ```bash qdl sahara_programmer.xml rawprogram.xml ``` #### Programmer archive Directly providing a list of ids and filenames is cumbersome and error-prone, so QDL accepts a "*programmer archive*". This allows the user to use the tool in the same fashion as was done for single-programmer targets. The *programmer archive* is a CPIO archive containing the Sahara images to be loaded, identified by the filename **id[:filename]** (*filename* is optional, but useful for debugging). Each included file will be used to serve requests for the given Sahara *id*. Such an archive can be created by putting the target's programmer images in an empty directory, then executing the following command from that directory: ```bash ls | cpio -o -H newc > ../programmer.cpio ``` *programmer.cpio* can now be passed to QDL and the included images will be served in order to reach Firehose mode. ## Collect crash dump When a Qualcomm target crashes or is forced into crash dump mode, the bootloader re-enumerates the device over USB with Product ID `900e` and offers memory segments for collection via the Sahara protocol. A kernel crash can be triggered on the target with: ```bash echo c > /proc/sysrq-trigger ``` Use `qdl ramdump` on the host to collect the dump: ```bash qdl ramdump -o ./ramdump ``` The same functionality is also available as a standalone `qdl-ramdump` binary, useful when only crash-dump collection is needed: ```bash qdl-ramdump -o ./ramdump ``` Either form writes each offered memory segment to a separate file under `./ramdump`. To collect only specific segments, pass a comma-separated filter: ```bash qdl ramdump -o ./ramdump OCIMEM,CODERAM ``` ## Sahara kickstart for flashless-boot devices (qdl-ks) The `qdl-ks` ("kickstart") helper uses the Sahara protocol to load images from the host to the device. It targets *flashless boot* devices such as the Qualcomm Cloud AI 100, which fetch their runtime firmware from the host on every boot rather than storing it on-device. Although it shares the Sahara protocol with `qdl`, `qdl-ks` is a deliberately separate tool: it does not use USB or Firehose, and instead talks to a kernel-provided device node using plain open/read/write operations. Its argument set is correspondingly minimal. Two arguments are required: `-p` selects the Sahara port (a device node) and `-s id:path` registers an image mapping. The `-s` option may be specified more than once, one mapping per Sahara image id the device may request. ```bash qdl-ks -p /dev/mhi0_QAIC_SAHARA \ -s 1:/opt/qti-aic/firmware/fw1.bin \ -s 2:/opt/qti-aic/firmware/fw2.bin ``` The mapped files do not need to exist at invocation time. If `qdl-ks` cannot open a requested file, the device decides the next action. This makes it possible to wire `qdl-ks` into a single udev rule that covers multiple device configurations (for example, an optional DDR training image that is only present on some setups). ## Run tests To run the integration test suite for QDL, use the `meson` tool with `test` param: ```bash meson test -C build ``` If `cmocka` is installed at configure time, Meson also builds and runs the unit test suite (including `program_load_xml` path-resolution tests). You can run only unit tests with: ```bash meson test -C build --suite unit ``` ## Generate man pages Manpages can be generated using `manpages` target: ```bash meson compile manpages -C build ``` ## Contributing Please submit any patches to the qdl (`master` branch) by using the GitHub pull request feature. Fork the repo, create a branch, do the work, rebase with upstream, and submit the pull request. The preferred coding style for this tool is [Linux kernel coding style](https://www.kernel.org/doc/html/v6.15/process/coding-style.html). Before creating a commit, please ensure that your changes adhere to the coding style by using the `meson compile check-cached -C build` target, for example: ```bash $ git status On branch improvements Changes to be committed: (use "git restore --staged ..." to unstage) modified: qdl.c modified: qdl.h $ meson compile check-cached -C build [0/1] Running external command check-cached (wrapped by meson to set env) Running checkpatch on staged changes... ERROR: trailing whitespace #28: FILE: qdl.h:32: +^IQDL_DEVICE_USB, $ total: 1 errors, 0 warnings, 0 checks, 27 lines checked NOTE: For some of the reported defects, checkpatch may be able to mechanically convert to the typical style using --fix or --fix-inplace. NOTE: Whitespace errors detected. You may wish to use scripts/cleanpatch or scripts/cleanfile Your patch has style problems, please review. ``` To verify a series of commits the same way the CI does (per-commit, in patch mode), use the `check-range` target. It runs checkpatch on every commit in `$CHECKPATCH_BASE..$CHECKPATCH_HEAD` (defaulting to `origin/master..HEAD`): ```bash meson compile check-range -C build ``` To restrict the range explicitly, set the environment variables before invoking meson: ```bash CHECKPATCH_BASE=origin/master CHECKPATCH_HEAD=HEAD \ meson compile check-range -C build ``` The full file-mode check (run against every tracked C/H/sh source) is also available: ```bash meson compile check -C build ``` Markdown sources are linted with [mdl](https://github.com/markdownlint/markdownlint). Install it via `sudo apt install markdownlint` (or `gem install mdl`) and run: ```bash meson compile markdown-lint -C build ``` The wrapper script is invoked from the GitHub Actions workflow as well, so a green local run reflects what CI will report. ## License This tool is licensed under the BSD 3-Clause license. Check out [LICENSE](LICENSE) for more details. linux-msm-qdl-f540e59/auto.c000066400000000000000000000071671521156022300157100ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. * * QDL_DEVICE_AUTO: meta-backend that defers transport selection to the * wait loop. Each 250 ms tick it runs both the libusb open attempt and * (on Windows) a QUD SetupAPI probe; whichever first reaches an EDL * device wins, and its concrete qdl_device is bound as the inner. All * subsequent qdl_read/write/close calls on the outer forward to the * inner. */ #include #include #include #include #include #include "qdl.h" struct qdl_device_auto { struct qdl_device base; struct qdl_device *inner; long pending_chunk_size; bool chunk_size_set; }; static struct qdl_device_auto *to_auto(struct qdl_device *qdl) { return container_of(qdl, struct qdl_device_auto, base); } static void auto_bind_inner(struct qdl_device_auto *wrap, struct qdl_device *inner) { wrap->inner = inner; wrap->base.max_payload_size = inner->max_payload_size; if (wrap->chunk_size_set) inner->set_out_chunk_size(inner, wrap->pending_chunk_size); } static int auto_open(struct qdl_device *qdl, const char *serial) { struct qdl_device_auto *wrap = to_auto(qdl); struct qdl_device *usb_dev; #ifdef _WIN32 struct qdl_device *qud_dev; int qud_count; #endif int visible_prev = -1; int visible; int ret; usb_dev = usb_init(); if (!usb_dev) return -1; #ifdef _WIN32 qud_dev = qud_init(); if (!qud_dev) { qdl_deinit(usb_dev); return -1; } #endif for (;;) { ret = try_usb_open(usb_dev, serial, &visible); if (ret == 0) { #ifdef _WIN32 qdl_deinit(qud_dev); #endif auto_bind_inner(wrap, usb_dev); return 0; } if (ret == -EIO) goto fail; #ifdef _WIN32 qud_count = qud_probe_present(); if (qud_count > 0 || ret == -EBUSY) { if (qud_dev->open(qud_dev, serial) == 0) { qdl_deinit(usb_dev); auto_bind_inner(wrap, qud_dev); return 0; } } visible += qud_count; #endif if (visible != visible_prev) { if (visible == 0) ux_info("Waiting for EDL device\n"); else if (serial) ux_info("%d EDL device(s) visible, none match serial \"%s\"\n", visible, serial); else ux_info("%d EDL device(s) visible, none could be opened\n", visible); visible_prev = visible; } usleep(250000); } fail: qdl_deinit(usb_dev); #ifdef _WIN32 qdl_deinit(qud_dev); #endif return -1; } static int auto_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout) { struct qdl_device *inner = to_auto(qdl)->inner; return inner->read(inner, buf, len, timeout); } static int auto_write(struct qdl_device *qdl, const void *buf, size_t len, unsigned int timeout) { struct qdl_device *inner = to_auto(qdl)->inner; return inner->write(inner, buf, len, timeout); } static void auto_close(struct qdl_device *qdl) { struct qdl_device_auto *wrap = to_auto(qdl); if (!wrap->inner) return; wrap->inner->close(wrap->inner); qdl_deinit(wrap->inner); wrap->inner = NULL; } static void auto_set_out_chunk_size(struct qdl_device *qdl, long size) { struct qdl_device_auto *wrap = to_auto(qdl); if (wrap->inner) { wrap->inner->set_out_chunk_size(wrap->inner, size); return; } wrap->pending_chunk_size = size; wrap->chunk_size_set = true; } struct qdl_device *auto_init(void) { struct qdl_device_auto *wrap = calloc(1, sizeof(*wrap)); if (!wrap) return NULL; wrap->base.dev_type = QDL_DEVICE_AUTO; wrap->base.open = auto_open; wrap->base.read = auto_read; wrap->base.write = auto_write; wrap->base.close = auto_close; wrap->base.set_out_chunk_size = auto_set_out_chunk_size; wrap->base.max_payload_size = 1048576; return &wrap->base; } linux-msm-qdl-f540e59/commitlint.config.mjs000066400000000000000000000031061521156022300207170ustar00rootroot00000000000000// Commitlint config tuned for Linux kernel-style commit messages. // // Expected format: // subsystem: short description (max 72 chars) // // Body text wrapped at 72 chars. // // Signed-off-by: Name export default { rules: { // Subject line limit (Linux kernel style uses up to 72) 'header-max-length': [2, 'always', 72], // Body and footer lines wrapped at 72 chars. // Footer limit is relaxed to 200 to accommodate long Signed-off-by // addresses without false positives. 'body-max-line-length': [2, 'always', 72], 'footer-max-line-length': [2, 'always', 200], // Require a blank line between subject and body, and before footers 'body-leading-blank': [2, 'always'], 'footer-leading-blank': [2, 'always'], // Disable Conventional Commits type rules - this project uses // Linux kernel–style "subsystem: description" subjects instead. 'type-enum': [0], 'type-case': [0], 'type-empty': [0], 'scope-empty': [0], 'subject-case': [0], }, // Teach the parser which trailers terminate the body and start the // footer. Without this, kernel-style trailers like `Fixes:` are // classified as body lines and trip body-max-line-length, while the // relaxed footer-max-line-length (200) never gets a chance to apply. parserPreset: { parserOpts: { noteKeywords: [ 'BREAKING CHANGE', 'BREAKING-CHANGE', 'Fixes', 'Signed-off-by', 'Co-authored-by', 'Reviewed-by', 'Tested-by', 'Acked-by', 'Reported-by', 'Suggested-by', ], }, }, }; linux-msm-qdl-f540e59/contents.c000066400000000000000000000576101521156022300165730ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #include #include #include #include #include #include #include #include #include #include "contents.h" #include "file.h" #include "firehose.h" #include "pathbuf.h" #include "qdl.h" #ifdef _WIN32 #define ROOT_PATH_TAG "windows_root_path" #else #define ROOT_PATH_TAG "linux_root_path" #endif enum contents_file_type { CONTENTS_FILE_OTHER, CONTENTS_FILE_PROGRAM, CONTENTS_FILE_PATCH, CONTENTS_FILE_DEVICE_PROGRAMMER, CONTENTS_FILE_PROGRAMMER_XML, }; enum firehose_type { FIREHOSE_TYPE_NONE, FIREHOSE_TYPE_LITE, FIREHOSE_TYPE_TRUE, }; struct contents_entry { enum contents_file_type file_type; enum qdl_storage_type storage_type; char *flavor; char *filename; struct pathbuf path; enum firehose_type firehose_type; struct list_head node; }; struct contents { struct list_head entries; struct pathbuf base_dir; char **flavors; size_t num_flavors; }; struct contents_filter { struct contents *contents; enum qdl_storage_type storage_type; const char *flavor; }; struct contents_selector { enum qdl_storage_type storage_type; const char *flavor; }; static const char *contents_storage_name(enum qdl_storage_type storage) { const char *name = encode_storage_type(storage); return name ? name : "unknown"; } static char *contents_node_get_text(xmlNode *node) { const xmlChar *start; const xmlChar *end; xmlChar *str; size_t len; char *ret; str = xmlNodeGetContent(node); if (!str) return NULL; for (start = str; *start && isspace((unsigned char)*start); start++) ; for (end = start + xmlStrlen(start); end > start && isspace((unsigned char)end[-1]); end--) ; len = end - start; ret = calloc(1, len + 1); if (!ret) goto out_free_str; memcpy(ret, start, len); out_free_str: xmlFree(str); return ret; } static bool contents_entry_is_ignored(xmlNode *node) { xmlChar *ignore; bool ret; ignore = xmlGetProp(node, (xmlChar *)"ignore"); if (!ignore) return false; ret = !xmlStrcmp(ignore, (xmlChar *)"true"); xmlFree(ignore); return ret; } static int contents_parse_pf(struct contents *contents, xmlNode *node) { char **new_flavors; char *name = NULL; for (; node; node = node->next) { if (node->type == XML_ELEMENT_NODE && !xmlStrcmp(node->name, (xmlChar *)"name")) { name = contents_node_get_text(node); break; } } if (!name) return -1; new_flavors = realloc(contents->flavors, (contents->num_flavors + 1) * sizeof(*new_flavors)); if (!new_flavors) { free(name); return -1; } contents->flavors = new_flavors; contents->flavors[contents->num_flavors++] = name; return 0; } static int contents_parse_pfs(struct contents *contents, xmlNode *node) { int ret; for (; node; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (!xmlStrcmp(node->name, (xmlChar *)"pf")) { ret = contents_parse_pf(contents, node->children); if (ret < 0) { ux_err("failed to parse product flavor definition\n"); return -1; } } } return 0; } static enum contents_file_type contents_detect_file_type(xmlNode *node, enum firehose_type *fh_type) { enum contents_file_type type = CONTENTS_FILE_OTHER; xmlChar *firehose_type; if (!xmlStrcmp(node->name, (xmlChar *)"partition_file")) type = CONTENTS_FILE_PROGRAM; if (!xmlStrcmp(node->name, (xmlChar *)"partition_patch_file")) type = CONTENTS_FILE_PATCH; if (!xmlStrcmp(node->name, (xmlChar *)"device_programmer")) type = CONTENTS_FILE_DEVICE_PROGRAMMER; firehose_type = xmlGetProp(node, (xmlChar *)"firehose_type"); if (firehose_type) { if (!xmlStrcmp(firehose_type, (xmlChar *)"true")) { type = CONTENTS_FILE_PROGRAMMER_XML; *fh_type = FIREHOSE_TYPE_TRUE; } else if (!xmlStrcmp(firehose_type, (xmlChar *)"lite")) { *fh_type = FIREHOSE_TYPE_LITE; } else { *fh_type = FIREHOSE_TYPE_NONE; } } xmlFree(firehose_type); return type; } static enum qdl_storage_type contents_detect_storage_type(xmlNode *node) { enum qdl_storage_type type = QDL_STORAGE_UNKNOWN; xmlChar *storage; storage = xmlGetProp(node, (xmlChar *)"storage_type"); if (storage) { type = decode_storage_type((char *)storage); xmlFree(storage); } return type; } static int contents_expand_path_vars(struct pathbuf *path) { char *head = &path->buf[0]; char *tail = head; char *colon; char *end; /* * Paths can contain "${var:default}" entries, which allow overriding * portions of the path. As this is not supported, replace each with * "default". */ while (*head) { if (head[0] == '$' && head[1] == '{') { colon = strchr(head + 2, ':'); end = colon ? strchr(colon + 1, '}') : NULL; if (colon && end) { head = colon + 1; while (head < end) *tail++ = *head++; head = end + 1; continue; } } *tail++ = *head++; } *tail = '\0'; path->len = tail - path->buf; return 0; } static int contents_parse_file_names(struct contents *contents, xmlNode *node, enum contents_file_type file_type, struct pathbuf *current_path, char *path_flavor, enum qdl_storage_type storage, enum firehose_type firehose_type) { struct contents_entry *entry; struct pathbuf full_path; xmlNode *child; char *filename; int ret; for (child = node->children; child; child = child->next) { if (child->type != XML_ELEMENT_NODE) continue; if (xmlStrcmp(child->name, (xmlChar *)"file_name")) continue; filename = contents_node_get_text(child); if (!filename) return -1; /* Ignore filenames with wildcards */ if (strstr(filename, "*")) { free(filename); continue; } qdl_pathbuf_dup(&full_path, current_path); ret = qdl_pathbuf_push(&full_path, filename); if (ret < 0) { free(filename); return -1; } contents_expand_path_vars(&full_path); entry = calloc(1, sizeof(*entry)); if (!entry) { free(filename); return -1; } entry->file_type = file_type; entry->storage_type = storage; if (path_flavor) { entry->flavor = strdup(path_flavor); if (!entry->flavor) { free(entry); free(filename); return -1; } } entry->firehose_type = firehose_type; entry->filename = filename; qdl_pathbuf_dup(&entry->path, &full_path); list_append(&contents->entries, &entry->node); } return 0; } static int contents_parse_entry(struct contents *contents, xmlNode *node, struct pathbuf *build_root) { enum contents_file_type file_type; enum firehose_type firehose_type = FIREHOSE_TYPE_NONE; enum qdl_storage_type storage; struct pathbuf file_path; xmlNode *child; char *flavor; char *path; int ret; if (!build_root) { ux_err("entry has no build root path in contents.xml\n"); return -1; } if (contents_entry_is_ignored(node)) return 0; file_type = contents_detect_file_type(node, &firehose_type); storage = contents_detect_storage_type(node); for (child = node->children; child; child = child->next) { if (child->type != XML_ELEMENT_NODE) continue; if (xmlStrcmp(child->name, (xmlChar *)"file_path")) continue; path = contents_node_get_text(child); if (!path) return -1; flavor = (char *)xmlGetProp(child, (xmlChar *)"flavor"); qdl_pathbuf_dup(&file_path, build_root); ret = qdl_pathbuf_push(&file_path, path); if (ret < 0) { xmlFree(flavor); free(path); return -1; } ret = contents_parse_file_names(contents, node, file_type, &file_path, flavor, storage, firehose_type); xmlFree(flavor); free(path); if (ret < 0) return ret; } return 0; } static bool contents_is_entry_node(xmlNode *node) { return !xmlStrcmp(node->name, (xmlChar *)"download_file") || !xmlStrcmp(node->name, (xmlChar *)"file_ref") || !xmlStrcmp(node->name, (xmlChar *)"partition_file") || !xmlStrcmp(node->name, (xmlChar *)"partition_patch_file") || !xmlStrcmp(node->name, (xmlChar *)"device_programmer"); } static int contents_parse_builds(struct contents *contents, xmlNode *node, struct pathbuf *root_path) { char *root; int ret; for (; node; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (!xmlStrcmp(node->name, (xmlChar *)"build")) { struct pathbuf build_root = {0}; ret = contents_parse_builds(contents, node->children, &build_root); if (ret < 0) return ret; continue; } if (!xmlStrcmp(node->name, (xmlChar *)ROOT_PATH_TAG)) { root = contents_node_get_text(node); if (!root) return -1; if (!root_path) { ux_err("linux_root_path without active build context\n"); free(root); return -1; } qdl_pathbuf_dup(root_path, &contents->base_dir); ret = qdl_pathbuf_push(root_path, root); free(root); if (ret < 0) return -1; } else if (contents_is_entry_node(node)) { ret = contents_parse_entry(contents, node, root_path); if (ret < 0) return ret; } } return 0; } static int contents_parse_nodes(struct contents *contents, xmlNode *node) { int ret; for (; node; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (!xmlStrcmp(node->name, (xmlChar *)"product_flavors")) { ret = contents_parse_pfs(contents, node->children); if (ret < 0) return ret; } else if (!xmlStrcmp(node->name, (xmlChar *)"builds_flat")) { ret = contents_parse_builds(contents, node->children, NULL); if (ret < 0) return ret; } } return 0; } static int contents_get_base_dir(struct pathbuf *base_dir, const char *filename) { qdl_pathbuf_reset(base_dir); qdl_pathbuf_push(base_dir, filename); qdl_pathbuf_dirname(base_dir); return 0; } int contents_load_xml(struct contents *contents, const char *filename) { xmlNode *root; xmlDoc *doc; int ret; doc = xmlReadFile(filename, NULL, 0); if (!doc) { ux_err("failed to parse contents file \"%s\"\n", filename); return -1; } root = xmlDocGetRootElement(doc); if (!root || xmlStrcmp(root->name, (xmlChar *)"contents")) { ux_err("specified file \"%s\" is not a contents.xml document\n", filename); xmlFreeDoc(doc); return -1; } ret = contents_get_base_dir(&contents->base_dir, filename); if (ret < 0) goto err_free_doc; ret = contents_parse_nodes(contents, root->children); if (ret < 0) goto err_free_doc; xmlFreeDoc(doc); if (list_empty(&contents->entries)) ux_info("contents: no file entries parsed from \"%s\"\n", filename); return 0; err_free_doc: xmlFreeDoc(doc); return -1; } static int contents_find_programmers(struct contents *contents, struct sahara_image *images) { struct contents_filter filter = { .contents = contents }; struct contents_entry *entry; struct sahara_image blob; int ret; list_for_each_entry(entry, &contents->entries, node) { if (entry->file_type != CONTENTS_FILE_PROGRAMMER_XML) continue; ret = load_sahara_image(NULL, qdl_pathbuf_str(&entry->path), &blob); if (ret < 0) { ux_err("unable to open \"%s\" for reading\n", qdl_pathbuf_str(&entry->path)); continue; } ret = decode_sahara_config(&blob, images, &filter); if (ret == 0) { ux_err("%s is not a programmer xml\n", qdl_pathbuf_str(&entry->path)); sahara_images_free(&blob, 1); continue; } else if (ret < 0) { ux_err("failed to parse programmer xml \"%s\"\n", qdl_pathbuf_str(&entry->path)); return -1; } return 0; } list_for_each_entry(entry, &contents->entries, node) { if (entry->file_type != CONTENTS_FILE_DEVICE_PROGRAMMER) continue; if (entry->firehose_type == FIREHOSE_TYPE_LITE) continue; ret = load_sahara_image(NULL, qdl_pathbuf_str(&entry->path), &images[SAHARA_ID_EHOSTDL_IMG]); if (ret < 0) { ux_err("unable to open \"%s\" for reading\n", qdl_pathbuf_str(&entry->path)); continue; } return 0; } ux_err("no programmer definitions found\n"); return -1; } static bool contents_flavor_matches(const char *a, const char *b) { return (!a && !b) || (a && b && !strcmp(a, b)); } static bool contents_selector_is_known(struct contents_selector *selectors, size_t count, enum qdl_storage_type storage_type, const char *flavor) { size_t i; for (i = 0; i < count; i++) { if (selectors[i].storage_type == storage_type && contents_flavor_matches(selectors[i].flavor, flavor)) return true; } return false; } static bool contents_storage_is_selected(struct contents_selector *selectors, size_t count, enum qdl_storage_type storage_type) { size_t selector_idx; for (selector_idx = 0; selector_idx < count; selector_idx++) { if (selectors[selector_idx].storage_type == storage_type) return true; } return false; } static bool contents_storage_has_flavored_entries(struct contents *contents, enum qdl_storage_type storage_type) { struct contents_entry *entry; list_for_each_entry(entry, &contents->entries, node) { if (entry->file_type != CONTENTS_FILE_PROGRAM && entry->file_type != CONTENTS_FILE_PATCH) continue; if (entry->storage_type != storage_type) continue; if (entry->flavor) return true; } return false; } static size_t contents_collect_valid_selectors(struct contents *contents, struct contents_selector **contents_selectors) { struct contents_selector *new_selectors; struct contents_selector *selectors = NULL; struct contents_entry *entry; size_t count = 0; list_for_each_entry(entry, &contents->entries, node) { if (entry->file_type != CONTENTS_FILE_PROGRAM && entry->file_type != CONTENTS_FILE_PATCH) continue; if (entry->storage_type == QDL_STORAGE_UNKNOWN) continue; if (!entry->flavor && contents_storage_has_flavored_entries(contents, entry->storage_type)) continue; if (contents_selector_is_known(selectors, count, entry->storage_type, entry->flavor)) continue; new_selectors = realloc(selectors, (count + 1) * sizeof(*selectors)); if (!new_selectors) { free(selectors); return 0; } selectors = new_selectors; selectors[count].storage_type = entry->storage_type; selectors[count].flavor = entry->flavor; count++; } *contents_selectors = selectors; return count; } static void contents_print_valid_selectors(struct contents_selector *selectors, size_t count) { size_t selector_idx; ux_err("valid storage/flavor combinations:\n"); for (selector_idx = 0; selector_idx < count; selector_idx++) { if (selectors[selector_idx].flavor) ux_err(" %s/%s\n", contents_storage_name(selectors[selector_idx].storage_type), selectors[selector_idx].flavor); else ux_err(" %s\n", contents_storage_name(selectors[selector_idx].storage_type)); } } static bool contents_flavor_is_valid(struct contents *contents, const char *flavor) { size_t flavor_idx; for (flavor_idx = 0; flavor_idx < contents->num_flavors; flavor_idx++) { if (!strcmp(flavor, contents->flavors[flavor_idx])) return true; } return false; } static size_t contents_select_by_storage(struct contents_selector *valid_selectors, size_t num_valid_selectors, enum qdl_storage_type storage_type, struct contents_selector *selector) { size_t valid_idx; size_t matches = 0; for (valid_idx = 0; valid_idx < num_valid_selectors; valid_idx++) { if (valid_selectors[valid_idx].storage_type != storage_type) continue; *selector = valid_selectors[valid_idx]; matches++; } return matches; } static size_t contents_select_by_flavor(struct contents_selector *valid_selectors, size_t num_valid_selectors, const char *flavor, struct contents_selector *selector) { size_t valid_idx; size_t matches = 0; for (valid_idx = 0; valid_idx < num_valid_selectors; valid_idx++) { if (!valid_selectors[valid_idx].flavor || strcmp(valid_selectors[valid_idx].flavor, flavor)) continue; *selector = valid_selectors[valid_idx]; matches++; } return matches; } static int contents_decode_selectors(struct contents *contents, char *pattern, struct contents_selector **contents_selectors) { struct contents_selector *valid_selectors = NULL; struct contents_selector *new_selectors; struct contents_selector *selectors = NULL; struct contents_selector selector; enum qdl_storage_type storage; size_t num_valid_selectors; char *flavor; size_t count = 0; char *token; char *save; char *sep; size_t matches; num_valid_selectors = contents_collect_valid_selectors(contents, &valid_selectors); if (!num_valid_selectors) { ux_err("contents.xml does not provide any valid storage/flavor combinations\n"); return -1; } if (!pattern) { if (num_valid_selectors == 1) { *contents_selectors = valid_selectors; return 1; } if (num_valid_selectors > 1) { ux_err("contents.xml contains multiple storage/flavor combinations; select one or more with ::/\n"); contents_print_valid_selectors(valid_selectors, num_valid_selectors); free(valid_selectors); return -1; } free(valid_selectors); return -1; } if (!pattern[0]) { ux_err("empty storage/flavor selector\n"); goto err_free_valid_selectors; } for (token = strtok_r(pattern, ",", &save); token; token = strtok_r(NULL, ",", &save)) { new_selectors = realloc(selectors, (count + 1) * sizeof(*selectors)); if (!new_selectors) goto err_free_selectors; selectors = new_selectors; if (!token[0]) { ux_err("empty storage/flavor selector\n"); goto err_free_selectors; } sep = strchr(token, '/'); if (!sep) { storage = decode_storage_type(token); if (storage != QDL_STORAGE_UNKNOWN) { matches = contents_select_by_storage(valid_selectors, num_valid_selectors, storage, &selector); if (matches == 1) goto append_selector; if (!matches) ux_err("storage type \"%s\" has no valid flavor in contents.xml\n", token); else ux_err("storage type \"%s\" is ambiguous; specify a flavor\n", token); contents_print_valid_selectors(valid_selectors, num_valid_selectors); goto err_free_selectors; } matches = contents_select_by_flavor(valid_selectors, num_valid_selectors, token, &selector); if (matches == 1) goto append_selector; if (matches > 1) ux_err("flavor \"%s\" is ambiguous; specify a storage type\n", token); else if (contents_flavor_is_valid(contents, token)) ux_err("flavor \"%s\" has no valid storage type in contents.xml\n", token); else ux_err("unknown storage type or flavor \"%s\"\n", token); contents_print_valid_selectors(valid_selectors, num_valid_selectors); goto err_free_selectors; } *sep = '\0'; flavor = sep + 1; if (!token[0]) { ux_err("missing storage selector for flavor \"%s\"\n", flavor); goto err_free_selectors; } if (!flavor[0]) { ux_err("invalid flavor selection for storage \"%s\"\n", token); goto err_free_selectors; } if (strchr(flavor, '/')) { ux_err("invalid flavor selector \"%s\"\n", flavor); goto err_free_selectors; } if (!contents_flavor_is_valid(contents, flavor)) { ux_err("invalid flavor \"%s\" requested\n", flavor); ux_err("valid flavors:\n"); for (size_t flavor_idx = 0; flavor_idx < contents->num_flavors; flavor_idx++) ux_err(" %s\n", contents->flavors[flavor_idx]); goto err_free_selectors; } storage = decode_storage_type(token); if (storage == QDL_STORAGE_UNKNOWN) { ux_err("unknown storage type \"%s\"\n", token); goto err_free_selectors; } if (!contents_selector_is_known(valid_selectors, num_valid_selectors, storage, flavor)) { ux_err("storage/flavor combination \"%s/%s\" not mentioned in contents.xml\n", contents_storage_name(storage), flavor); contents_print_valid_selectors(valid_selectors, num_valid_selectors); goto err_free_selectors; } selector.storage_type = storage; selector.flavor = flavor; append_selector: if (contents_storage_is_selected(selectors, count, selector.storage_type)) { ux_err("storage type \"%s\" selected multiple times\n", contents_storage_name(selector.storage_type)); goto err_free_selectors; } selectors[count] = selector; count++; } *contents_selectors = selectors; free(valid_selectors); return count; err_free_selectors: free(selectors); err_free_valid_selectors: free(valid_selectors); return -1; } int contents_load(struct list_head *ops, const char *filename, char *specifier, struct sahara_image *images, const char *incdir) { struct contents_filter filter = {}; struct contents_entry *entry; struct contents_entry *next; struct contents contents = {}; enum qdl_storage_type storage_type; struct contents_selector *selectors = NULL; struct firehose_op *op; const char *flavor; int num_selectors; char *pattern = specifier; size_t flavor_idx; int ret; int i; list_init(&contents.entries); ret = contents_load_xml(&contents, filename); if (ret < 0) goto out_free_contents; ret = contents_decode_selectors(&contents, pattern, &selectors); if (ret < 0) goto out_free_contents; num_selectors = ret; if (num_selectors == 0) { ux_err("contents.xml does not provide any valid storage/flavor combinations\n"); ret = -1; goto out_free_contents; } ret = contents_find_programmers(&contents, images); if (ret < 0) goto out_free_contents; for (i = 0; i < num_selectors; i++) { storage_type = selectors[i].storage_type; flavor = selectors[i].flavor; op = firehose_alloc_op(FIREHOSE_OP_CONFIGURE); op->storage_type = storage_type; list_append(ops, &op->node); list_for_each_entry(entry, &contents.entries, node) { if (entry->file_type != CONTENTS_FILE_PROGRAM) continue; if (entry->storage_type != QDL_STORAGE_UNKNOWN && entry->storage_type != storage_type) continue; if (entry->flavor && (!flavor || strcmp(entry->flavor, flavor))) continue; filter.contents = &contents; filter.storage_type = storage_type; filter.flavor = flavor; ret = program_load(ops, qdl_pathbuf_str(&entry->path), storage_type == QDL_STORAGE_NAND, false, &filter, incdir); if (ret < 0) { ux_err("failed to load program: %s\n", entry->filename); goto out_free_contents; } } list_for_each_entry(entry, &contents.entries, node) { if (entry->file_type != CONTENTS_FILE_PATCH) continue; if (entry->storage_type != QDL_STORAGE_UNKNOWN && entry->storage_type != storage_type) continue; if (entry->flavor && (!flavor || strcmp(entry->flavor, flavor))) continue; ret = patch_load(ops, qdl_pathbuf_str(&entry->path)); if (ret < 0) { ux_err("failed to load %s\n", entry->filename); goto out_free_contents; } } } out_free_contents: for (flavor_idx = 0; flavor_idx < contents.num_flavors; flavor_idx++) free(contents.flavors[flavor_idx]); free(contents.flavors); list_for_each_entry_safe(entry, next, &contents.entries, node) { free(entry->filename); free(entry->flavor); free(entry); } free(selectors); return ret; } int contents_resolve_path(struct contents_filter *filter, const char *filename, struct pathbuf *path) { enum qdl_storage_type storage_type; struct contents_entry *entry; struct contents *contents; struct pathbuf probe; const char *flavor; if (!filter) return 0; contents = filter->contents; storage_type = filter->storage_type; flavor = filter->flavor; /* Look for a match */ list_for_each_entry(entry, &contents->entries, node) { if (entry->storage_type != QDL_STORAGE_UNKNOWN && entry->storage_type != storage_type) { continue; } if (entry->flavor && flavor && strcmp(entry->flavor, flavor)) { continue; } if (strcmp(entry->filename, filename)) continue; qdl_pathbuf_dup(path, &entry->path); return 1; } /* * fh_loader adds all applicable to the search path, to find * files adjacent to those described in the contents.xml. So if we * didn't find an exact match, search adjacent to all other files... */ list_for_each_entry(entry, &contents->entries, node) { if (entry->storage_type != QDL_STORAGE_UNKNOWN && entry->storage_type != storage_type) { continue; } if (entry->flavor && flavor && strcmp(entry->flavor, flavor)) { continue; } qdl_pathbuf_dup(&probe, &entry->path); qdl_pathbuf_dirname(&probe); qdl_pathbuf_push(&probe, filename); if (!access(probe.buf, F_OK)) { qdl_pathbuf_dup(path, &probe); return 1; } } return 0; } linux-msm-qdl-f540e59/contents.h000066400000000000000000000010301521156022300165610ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #ifndef __CONTENTS_H__ #define __CONTENTS_H__ #include #include #include "list.h" struct sahara_image; struct pathbuf; struct contents_filter; int contents_load(struct list_head *ops, const char *filename, char *specifier, struct sahara_image *images, const char *incdir); int contents_resolve_path(struct contents_filter *filter, const char *filename, struct pathbuf *path); #endif linux-msm-qdl-f540e59/file.c000066400000000000000000000111211521156022300156400ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #include #include #include #include #include "oscompat.h" #include "qdl.h" #include "file.h" struct qdl_zip { zip_t *zip; unsigned int refcount; }; int qdl_file_open(struct qdl_zip *qdl_zip, const char *filename, struct qdl_file *file) { struct zip_stat st; zip_int64_t idx; zip_file_t *zf; off_t len; zip_t *zip; int fd; if (qdl_zip) { zip = qdl_zip->zip; idx = zip_name_locate(zip, filename, 0); if (idx < 0) { ux_err("unable to locate \"%s\" in zip archive\n", filename); return -1; } if (zip_stat_index(zip, idx, 0, &st) < 0) { ux_err("unable to stat \"%s\" in zip archive\n", filename); return -1; } zf = zip_fopen_index(zip, idx, 0); if (!zf) { ux_err("unable to open \"%s\" in zip archive\n", filename); return -1; } file->type = QDL_FILE_TYPE_ZIP; file->fd = -1; file->size = st.size; file->zip_file = zf; } else { fd = open(filename, O_RDONLY | O_BINARY); if (fd < 0) { ux_err("failed to open \"%s\" for reading\n", filename); return -1; } len = lseek(fd, 0, SEEK_END); if (len < 0) { ux_err("failed to find end of \"%s\"\n", filename); close(fd); return -1; } lseek(fd, 0, SEEK_SET); file->type = QDL_FILE_TYPE_POSIX; file->fd = fd; file->size = len; file->zip_file = NULL; } return 0; } void *qdl_file_load(struct qdl_file *file, size_t *len) { ssize_t n; void *buf; buf = malloc(file->size); if (!buf) { ux_err("unable to allocate memory to load file\n"); return NULL; } switch (file->type) { case QDL_FILE_TYPE_UNKNOWN: ux_err("internal error: attempting to load unknown file type\n"); return NULL; case QDL_FILE_TYPE_POSIX: n = read(file->fd, buf, file->size); if ((size_t)n != file->size) { ux_err("failed to load content of file\n"); goto err_free_buf; } break; case QDL_FILE_TYPE_ZIP: n = zip_fread(file->zip_file, buf, file->size); if ((size_t)n != file->size) { ux_err("failed to load zip file member\n"); goto err_free_buf; } break; }; *len = file->size; return buf; err_free_buf: free(buf); return NULL; } void qdl_file_close(struct qdl_file *file) { switch (file->type) { case QDL_FILE_TYPE_UNKNOWN: break; case QDL_FILE_TYPE_POSIX: close(file->fd); file->fd = -1; break; case QDL_FILE_TYPE_ZIP: zip_fclose(file->zip_file); file->zip_file = NULL; break; }; file->type = QDL_FILE_TYPE_UNKNOWN; } size_t qdl_file_getsize(struct qdl_file *file) { return file->size; } ssize_t qdl_file_read(struct qdl_file *file, void *buf, size_t len) { switch (file->type) { case QDL_FILE_TYPE_UNKNOWN: break; case QDL_FILE_TYPE_POSIX: return read(file->fd, buf, len); case QDL_FILE_TYPE_ZIP: return zip_fread(file->zip_file, buf, len); }; return -1; } /* * Loop around qdl_file_read() until @len bytes have been read into * @buf. Returns the number of bytes read, which equals @len for full * reads and is less than @len only on EOF; negative on a read error. * * Wraps the underlying read() / zip_fread() so callers that don't * tolerate short reads don't have to roll their own loop. The bytes * past the returned length are left untouched - callers that need a * fixed-size buffer must zero them themselves. */ ssize_t qdl_file_read_exact(struct qdl_file *file, void *buf, size_t len) { uint8_t *p = buf; size_t got = 0; ssize_t n; while (got < len) { n = qdl_file_read(file, p + got, len - got); if (n < 0) return -1; if (n == 0) break; got += (size_t)n; } return (ssize_t)got; } off_t qdl_file_seek(struct qdl_file *file, off_t offset, int whence) { switch (file->type) { case QDL_FILE_TYPE_UNKNOWN: return -1; case QDL_FILE_TYPE_POSIX: return lseek(file->fd, offset, whence); case QDL_FILE_TYPE_ZIP: if (offset == 0 && whence == SEEK_SET) return 0; ux_err("seek not implemented for zip files\n"); return -1; }; return -1; } int qdl_zip_open(const char *filename, struct qdl_zip **__qdl_zip) { struct qdl_zip *qdl_zip; zip_t *zip; zip = zip_open(filename, ZIP_RDONLY, NULL); if (!zip) { *__qdl_zip = NULL; return 0; } qdl_zip = calloc(1, sizeof(*qdl_zip)); if (!qdl_zip) { zip_close(zip); return -1; } qdl_zip->zip = zip; qdl_zip->refcount = 1; *__qdl_zip = qdl_zip; return 0; } struct qdl_zip *qdl_zip_get(struct qdl_zip *qdl_zip) { if (qdl_zip) qdl_zip->refcount++; return qdl_zip; } void qdl_zip_put(struct qdl_zip *qdl_zip) { if (qdl_zip) { if (--qdl_zip->refcount == 0) { zip_close(qdl_zip->zip); free(qdl_zip); } } } linux-msm-qdl-f540e59/file.h000066400000000000000000000020131521156022300156450ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #ifndef __QDL_FILE_H__ #define __QDL_FILE_H__ #include struct zip_file; enum qdl_file_type { QDL_FILE_TYPE_UNKNOWN, QDL_FILE_TYPE_POSIX, QDL_FILE_TYPE_ZIP, }; struct qdl_file { enum qdl_file_type type; size_t size; int fd; struct zip_file *zip_file; }; struct qdl_zip; int qdl_file_open(struct qdl_zip *qzip, const char *filename, struct qdl_file *file); void *qdl_file_load(struct qdl_file *file, size_t *len); void qdl_file_close(struct qdl_file *file); size_t qdl_file_getsize(struct qdl_file *file); ssize_t qdl_file_read(struct qdl_file *file, void *buf, size_t len); ssize_t qdl_file_read_exact(struct qdl_file *file, void *buf, size_t len); off_t qdl_file_seek(struct qdl_file *file, off_t offset, int whence); int qdl_zip_open(const char *filename, struct qdl_zip **__qdl_zip); struct qdl_zip *qdl_zip_get(struct qdl_zip *qzip); void qdl_zip_put(struct qdl_zip *qzip); #endif linux-msm-qdl-f540e59/firehose.c000066400000000000000000001204431521156022300165350ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2016-2017, Linaro Ltd. * Copyright (c) 2018, The Linux Foundation. All rights reserved. * All rights reserved. */ #include "list.h" #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "qdl.h" #include "file.h" #include "firehose.h" #include "sha2.h" #include "ufs.h" #include "oscompat.h" #include "vip.h" #include "sparse.h" #include "gpt.h" enum { FIREHOSE_ACK = 0, FIREHOSE_NAK, }; /* * Substring emitted by the Firehose programmer's startup logs when VIP is * active on-device (e.g. "INFO: VIP is enabled, receiving the signed table * of size 8192"). Matching a stable prefix avoids coupling to the trailing * byte count, which varies per programmer build. */ #define VIP_PROGRAMMER_MARKER "VIP is enabled, receiving the signed table" static void firehose_check_vip_marker(struct qdl_device *qdl, xmlNode *node) { xmlChar *value; if (qdl->vip_data.programmer_requires_vip) return; if (xmlStrcmp(node->name, (xmlChar *)"log") != 0) return; value = xmlGetProp(node, (xmlChar *)"value"); if (!value) return; if (strstr((const char *)value, VIP_PROGRAMMER_MARKER)) qdl->vip_data.programmer_requires_vip = true; xmlFree(value); } static void xml_setpropf(xmlNode *node, const char *attr, const char *fmt, ...) { xmlChar buf[128]; va_list ap; va_start(ap, fmt); vsnprintf((char *)buf, sizeof(buf), fmt, ap); xmlSetProp(node, (xmlChar *)attr, buf); va_end(ap); } static xmlNode *firehose_response_parse(const void *buf, size_t len, int *error) { xmlNode *node; xmlNode *root; xmlDoc *doc; doc = xmlReadMemory(buf, len, NULL, NULL, 0); if (!doc) { ux_err("failed to parse firehose response\n"); *error = -EINVAL; return NULL; } root = xmlDocGetRootElement(doc); for (node = root; node; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (xmlStrcmp(node->name, (xmlChar *)"data") == 0) break; } if (!node) { ux_err("firehose response without data tag\n"); *error = -EINVAL; xmlFreeDoc(doc); return NULL; } for (node = node->children; node && node->type != XML_ELEMENT_NODE; node = node->next) ; if (!node) { ux_err("empty firehose response\n"); *error = -EINVAL; } return node; } static int firehose_generic_parser(xmlNode *node, void *data __unused, bool *rawmode) { xmlChar *value; int ret = -EINVAL; value = xmlGetProp(node, (xmlChar *)"value"); if (!value) return -EINVAL; if (xmlStrcmp(node->name, (xmlChar *)"log") == 0) { ux_log("LOG: %s\n", value); ret = -EAGAIN; } else if (xmlStrcmp(value, (xmlChar *)"ACK") == 0) { ret = FIREHOSE_ACK; } else if (xmlStrcmp(value, (xmlChar *)"NAK") == 0) { ret = FIREHOSE_NAK; } xmlFree(value); value = xmlGetProp(node, (xmlChar *)"rawmode"); if (value) { if (xmlStrcmp(value, (xmlChar *)"true") == 0) *rawmode = true; xmlFree(value); } return ret; } /* * Scan @str for a contiguous run of exactly SHA256_DIGEST_STRING_LENGTH-1 * (64) lowercase or uppercase hex characters bounded by a non-hex character * or string boundary. On match, decode into @digest and return true. */ static bool extract_sha256_hex(const char *str, uint8_t digest[SHA256_DIGEST_LENGTH]) { const size_t hex_len = SHA256_DIGEST_LENGTH * 2; const char *p = str; size_t i; while (*p) { size_t run = 0; const char *start; while (*p && !isxdigit((unsigned char)*p)) p++; start = p; while (isxdigit((unsigned char)*p)) { run++; p++; } if (run == hex_len) { for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { char byte[3] = { start[i * 2], start[i * 2 + 1], '\0' }; digest[i] = (uint8_t)strtoul(byte, NULL, 16); } return true; } } return false; } static int firehose_sha256_parser(xmlNode *node, void *data, bool *rawmode) { struct firehose_op *op = data; xmlChar *value; int ret; if (xmlStrcmp(node->name, (xmlChar *)"log") == 0) { value = xmlGetProp(node, (xmlChar *)"value"); if (!value) return -EINVAL; if (!op->digest_valid && extract_sha256_hex((const char *)value, op->digest)) op->digest_valid = true; ux_log("LOG: %s\n", value); xmlFree(value); return -EAGAIN; } ret = firehose_generic_parser(node, NULL, rawmode); /* * Some Firehose implementations attach the digest as an attribute on * the response itself rather than emitting it via . Try a few * known attribute names. */ if (!op->digest_valid) { static const char * const attrs[] = { "Digest", "SHA256", "sha256" }; size_t i; for (i = 0; i < ARRAY_SIZE(attrs); i++) { value = xmlGetProp(node, (xmlChar *)attrs[i]); if (!value) continue; if (extract_sha256_hex((const char *)value, op->digest)) op->digest_valid = true; xmlFree(value); if (op->digest_valid) break; } } return ret; } static int firehose_read(struct qdl_device *qdl, int timeout_ms, int (*response_parser)(xmlNode *node, void *data, bool *rawmode), void *data) { char buf[4096]; xmlNode *node; int error; int resp = -EIO; int ret = -EAGAIN; int n; bool rawmode = false; struct timeval timeout; struct timeval now; struct timeval delta = { .tv_sec = timeout_ms / 1000, .tv_usec = (timeout_ms % 1000) * 1000 }; gettimeofday(&now, NULL); timeradd(&now, &delta, &timeout); /* * The goal of firehose_read() is to find a response to a request among * one or more incoming messages AND to consume all incoming messages * (otherwise subsequent writes will time out). * The messages can be one of: * - * - * - * * Generally messages are coming prior to the , but * on MSM8916 (at least) it's been observed that messages can * arrive after the . * * We therefor need to consume messages until there are no more * (timeout) and we have been able to parse out a response (using * @response_parser). * * In the special case that the contain an attribute * "rawmode=true", the device signals that it has entered a mode where * it will not send/receive XML-formatted commands. So, (at least for * reads) we need to shortcircuit the logic and directly terminate the * consumption of incoming data. */ for (;;) { n = qdl_read(qdl, buf, sizeof(buf), 100); /* Timeout after seeing a response, we're done waiting for logs */ if (n == -ETIMEDOUT && resp >= 0) break; /* We want to return resp on error, to not lose the reset response */ else if (n == -EIO) break; if (n == -ETIMEDOUT || n == 0) { gettimeofday(&now, NULL); if (timercmp(&now, &timeout, <)) continue; return -ETIMEDOUT; } buf[n] = '\0'; ux_debug("FIREHOSE READ: %s\n", buf); /* * On stream-oriented transports (Windows COM port via the * QDLoader driver, virtio-console, ...) a single read can * deliver multiple back-to-back Firehose responses * concatenated, since the driver doesn't preserve USB bulk- * transfer boundaries. Walk the buffer using the "" envelope to bound each message; the closing tag * is what really delimits the document so that any rawmode * binary payload that arrives spliced onto the same read * doesn't end up fed into libxml2 as if it were XML. * * libusb preserves transfer boundaries, so on that path each * read still contains exactly one document and the loop runs * once. */ char *cursor = buf; char *bufend = buf + n; while (cursor < bufend) { char *start = strstr(cursor, " tag. If it's * missing the message was either truncated or doesn't * fit the schema we know how to parse; hand the rest * of the buffer to libxml2 and let it error out * gracefully. */ xml_end = strstr(start, ""); if (xml_end) { xml_end += sizeof("") - 1; chunk = (size_t)(xml_end - start); } else { chunk = (size_t)(bufend - start); } node = firehose_response_parse(start, chunk, &error); if (!node) return error; firehose_check_vip_marker(qdl, node); ret = response_parser(node, data, &rawmode); xmlFreeDoc(node->doc); if (ret >= 0) resp = ret; cursor = start + chunk; /* * The response we just parsed told the host to switch * to raw mode (e.g. the ACK that precedes the binary * sectors of a ). On a stream transport the * first chunk of that binary payload can have arrived * tacked onto this same read. Push it back so the * next qdl_read() picks it up before the transport * is touched again. */ if (rawmode) { if (cursor < bufend) qdl_push_back(qdl, cursor, (size_t)(bufend - cursor)); break; } } if (rawmode) break; } return resp; } static int firehose_vip_send_table(struct qdl_device *qdl) { int ret; ret = vip_transfer_handle_tables(qdl); if (ret) { ux_err("VIP: error occurred during VIP table transmission\n"); return -1; } if (!vip_transfer_status_check_needed(qdl)) return 0; ret = firehose_read(qdl, 30000, firehose_generic_parser, NULL); if (ret) { ux_err("VIP: sending of digest table failed\n"); return -1; } ux_info("VIP: digest table has been sent successfully\n"); vip_transfer_clear_status(qdl); return 0; } static int firehose_write(struct qdl_device *qdl, xmlDoc *doc) { int saved_errno; xmlChar *s; int len; int ret; xmlDocDumpMemory(doc, &s, &len); ret = firehose_vip_send_table(qdl); if (ret) return -1; vip_gen_chunk_init(qdl); for (;;) { ux_debug("FIREHOSE WRITE: %s\n", s); vip_gen_chunk_update(qdl, s, len); ret = qdl_write(qdl, s, len, 1000); saved_errno = errno; /* * db410c sometimes sense a followed by * entries and won't accept write commands until these are * drained, so attempt to read any pending data and then retry * the write. */ if (ret < 0 && errno == ETIMEDOUT) { firehose_read(qdl, 100, firehose_generic_parser, NULL); } else { break; } } xmlFree(s); vip_gen_chunk_store(qdl); return ret < 0 ? -saved_errno : 0; } /** * firehose_configure_response_parser() - parse a configure response * @node: response xmlNode * * Return: max size supported by the remote, or negative errno on failure */ static int firehose_configure_response_parser(xmlNode *node, void *data, bool *rawmode __unused) { xmlChar *payload; xmlChar *value; size_t max_size; value = xmlGetProp(node, (xmlChar *)"value"); if (!value) return -EINVAL; if (xmlStrcmp(node->name, (xmlChar *)"log") == 0) { ux_log("LOG: %s\n", value); xmlFree(value); return -EAGAIN; } payload = xmlGetProp(node, (xmlChar *)"MaxPayloadSizeToTargetInBytes"); if (!payload) { xmlFree(value); return -EINVAL; } max_size = strtoul((char *)payload, NULL, 10); xmlFree(payload); /* * When receiving an ACK the remote may indicate that we should attempt * a larger payload size */ if (!xmlStrcmp(value, (xmlChar *)"ACK")) { payload = xmlGetProp(node, (xmlChar *)"MaxPayloadSizeToTargetInBytesSupported"); if (!payload) return -EINVAL; max_size = strtoul((char *)payload, NULL, 10); xmlFree(payload); } *(size_t *)data = max_size; xmlFree(value); return FIREHOSE_ACK; } static int firehose_send_configure(struct qdl_device *qdl, size_t payload_size, bool skip_storage_init, enum qdl_storage_type storage, size_t *max_payload_size) { const char *memory_name; xmlNode *root; xmlNode *node; xmlDoc *doc; memory_name = encode_storage_type(storage); if (!memory_name) return -EINVAL; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); node = xmlNewChild(root, NULL, (xmlChar *)"configure", NULL); xml_setpropf(node, "MemoryName", memory_name); xml_setpropf(node, "MaxPayloadSizeToTargetInBytes", "%lu", payload_size); xml_setpropf(node, "Verbose", "%d", 0); xml_setpropf(node, "ZlpAwareHost", "%d", 1); xml_setpropf(node, "SkipStorageInit", "%d", skip_storage_init); firehose_write(qdl, doc); xmlFreeDoc(doc); return firehose_read(qdl, 100, firehose_configure_response_parser, max_payload_size); } static int firehose_try_configure(struct qdl_device *qdl, bool skip_storage_init, enum qdl_storage_type storage) { size_t max_sector_size; size_t sector_sizes[] = { 512, 4096 }; struct firehose_op op; size_t size = 0; void *buf; int ret; unsigned int i; ret = firehose_send_configure(qdl, qdl->max_payload_size, skip_storage_init, storage, &size); if (ret < 0) return ret; /* Retry if remote proposed different size */ if (size != qdl->max_payload_size) { ret = firehose_send_configure(qdl, size, skip_storage_init, storage, &size); if (ret != FIREHOSE_ACK) { ux_err("configure request with updated payload size failed\n"); return -1; } qdl->max_payload_size = size; } ux_debug("accepted max payload size: %zu\n", qdl->max_payload_size); /* * Skip sector size probing when VIP is active: the probe read commands * are not included in the pre-built VIP digest table (the dry-run that * builds it exits before reaching this code via the SIM early-return * above), so sending them would cause a VIP hash mismatch on the device. */ if (storage != QDL_STORAGE_NAND && qdl->vip_data.state == VIP_DISABLED && !qdl->sector_size) { max_sector_size = sector_sizes[ARRAY_SIZE(sector_sizes) - 1]; buf = alloca(max_sector_size); memset(&op, 0, sizeof(op)); op.partition = 0; op.start_sector = "1"; op.num_sectors = 1; /* * Testing has shown that the loader will fail gracefully if a * read is issued with the wrong sector size, use this to attempt * to discover the storage device's sector size. */ for (i = 0; i < ARRAY_SIZE(sector_sizes); i++) { op.sector_size = sector_sizes[i]; ret = firehose_read_buf(qdl, &op, buf, max_sector_size); if (ret == 0) { qdl->sector_size = sector_sizes[i]; break; } } } if (qdl->sector_size) ux_debug("detected sector size of: %zd\n", qdl->sector_size); return 0; } static int firehose_erase(struct qdl_device *qdl, struct firehose_op *program) { unsigned int sector_size; xmlNode *root; xmlNode *node; xmlDoc *doc; int ret; sector_size = program->sector_size ? : qdl->sector_size; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); node = xmlNewChild(root, NULL, (xmlChar *)"erase", NULL); xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%d", sector_size); xml_setpropf(node, "physical_partition_number", "%d", program->partition); /* * Omitting num_sectors and start_sector attributes tells the programmer * to erase the full physical partition. */ if (program->num_sectors > 0) { xml_setpropf(node, "num_partition_sectors", "%d", program->num_sectors); xml_setpropf(node, "start_sector", "%s", program->start_sector); } if (qdl->slot != UINT_MAX) { xml_setpropf(node, "slot", "%u", qdl->slot); } if (program->is_nand) { xml_setpropf(node, "PAGES_PER_BLOCK", "%d", program->pages_per_block); } ret = firehose_write(qdl, doc); if (ret < 0) { ux_err("failed to send program request\n"); goto out; } ret = firehose_read(qdl, 30000, firehose_generic_parser, NULL); if (ret) ux_err("failed to erase %s+0x%x\n", program->start_sector, program->num_sectors); else ux_info("successfully erased %s+0x%x\n", program->start_sector, program->num_sectors); out: xmlFreeDoc(doc); return ret == FIREHOSE_ACK ? 0 : -1; } static int firehose_getsha256digest(struct qdl_device *qdl, struct firehose_op *op); /* * Decide whether the @program op can be skipped because its bytes are * already on flash. Returns 1 if the program path can be bypassed, 0 * to defer to the normal write. @buf is a scratch buffer of size * qdl->max_payload_size owned by the caller. * * Hash the bytes that would have been written, ask the device for the * digest of the same region, and compare. Any failure on the way (read error, * digest query failure, mismatch) returns 0 so the caller proceeds with * the normal program flow; the file pointer is restored by the * qdl_file_seek() at the top of the write loop. * * Restricted to non-sparse, non-NAND programs with VIP disabled: * - Sparse chunks would each need their own digest; v1 keeps them on * the normal program path. * - NAND has spare/OOB bytes whose semantics differ across * programmers. * - is not part of pre-built VIP digest tables. */ static int qdl_should_skipblock(struct qdl_device *qdl, struct firehose_op *program, struct qdl_file *file, unsigned int num_sectors, unsigned int sector_size, void *buf) { uint8_t local_digest[SHA256_DIGEST_LENGTH]; size_t total = (size_t)num_sectors * sector_size; struct firehose_op digest_op; size_t hashed = 0; SHA2_CTX ctx; ssize_t hn; if (qdl->skipblock_mode != QDL_SKIPBLOCK_SHA256 || program->sparse || program->is_nand || qdl->vip_data.state != VIP_DISABLED) return 0; SHA256Init(&ctx); qdl_file_seek(file, (off_t)program->file_offset * sector_size, SEEK_SET); while (hashed < total) { size_t want = MIN(qdl->max_payload_size, total - hashed); hn = qdl_file_read_exact(file, buf, want); if (hn < 0) return 0; if (hn == 0 || (size_t)hn < want) { /* * EOF before completing the requested region: the * device's digest is over `total` bytes from flash, * so we can't construct a matching local digest. * Defer to the normal program path. */ if (hn > 0) SHA256Update(&ctx, buf, hn); return 0; } SHA256Update(&ctx, buf, hn); hashed += (size_t)hn; } SHA256Final(local_digest, &ctx); digest_op = (struct firehose_op){ .type = FIREHOSE_OP_GET_SHA256_DIGEST, .sector_size = sector_size, .num_sectors = num_sectors, .partition = program->partition, .start_sector = program->start_sector, }; if (firehose_getsha256digest(qdl, &digest_op) == 0 && !memcmp(local_digest, digest_op.digest, SHA256_DIGEST_LENGTH)) return 1; return 0; } static int firehose_program(struct qdl_device *qdl, struct firehose_op *program) { unsigned int num_sectors; unsigned int sector_size; unsigned int zlp_timeout = 10000; struct qdl_file file; size_t chunk_size; xmlNode *root; xmlNode *node; xmlDoc *doc; void *buf; time_t t0; time_t t; size_t left; int ret; int n; size_t i; uint32_t fill_value; /* * ZLP has been measured to take up to 15 seconds on SPINOR devices, * let's double it to be on the safe side... */ if (qdl->current_storage_type == QDL_STORAGE_SPINOR) zlp_timeout = 60000; if (!program->filename) return 0; ret = qdl_file_open(program->zip, program->filename, &file); if (ret < 0) { ux_err("unable to open %s\n", program->filename); return -1; } num_sectors = program->num_sectors; sector_size = program->sector_size ? : qdl->sector_size; if (!program->sparse) { num_sectors = (qdl_file_getsize(&file) + sector_size - 1) / sector_size; if (program->num_sectors && num_sectors > program->num_sectors) { ux_err("%s to big for %s truncated to %d\n", program->filename, program->label, program->num_sectors * sector_size); num_sectors = program->num_sectors; } } buf = malloc(qdl->max_payload_size); if (!buf) { ux_err("failed to allocate sector buffer\n"); goto err_close_fd; } if (qdl_should_skipblock(qdl, program, &file, num_sectors, sector_size, buf)) { ux_info("skipped \"%s\" (sha256 match)\n", program->label); free(buf); qdl_file_close(&file); return 0; } doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); node = xmlNewChild(root, NULL, (xmlChar *)"program", NULL); xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%d", sector_size); xml_setpropf(node, "num_partition_sectors", "%d", num_sectors); xml_setpropf(node, "physical_partition_number", "%d", program->partition); xml_setpropf(node, "start_sector", "%s", program->start_sector); if (qdl->slot != UINT_MAX) { xml_setpropf(node, "slot", "%u", qdl->slot); } if (program->filename) xml_setpropf(node, "filename", "%s", program->filename); if (program->is_nand) { xml_setpropf(node, "PAGES_PER_BLOCK", "%d", program->pages_per_block); xml_setpropf(node, "last_sector", "%d", program->last_sector); } ret = firehose_write(qdl, doc); if (ret < 0) { ux_err("failed to send program request\n"); goto err_free_doc; } ret = firehose_read(qdl, 10000, firehose_generic_parser, NULL); if (ret) { ux_err("failed to setup programming\n"); goto err_free_doc; } t0 = time(NULL); if (!program->sparse) { qdl_file_seek(&file, (off_t)program->file_offset * sector_size, SEEK_SET); } else { switch (program->sparse_chunk_type) { case CHUNK_TYPE_RAW: qdl_file_seek(&file, program->sparse_offset, SEEK_SET); break; case CHUNK_TYPE_FILL: fill_value = program->sparse_fill_value; for (i = 0; i < qdl->max_payload_size; i += sizeof(fill_value)) memcpy(buf + i, &fill_value, sizeof(fill_value)); break; default: ux_err("[SPARSE] invalid chunk type\n"); goto err_free_doc; } } left = num_sectors; ux_debug("FIREHOSE RAW BINARY WRITE: %s, %d bytes\n", program->filename, sector_size * num_sectors); while (left > 0) { /* * We should calculate hash for every raw packet sent, * not for the whole binary. */ vip_gen_chunk_init(qdl); chunk_size = MIN(qdl->max_payload_size / sector_size, left); if (!program->sparse || program->sparse_chunk_type != CHUNK_TYPE_FILL) { n = qdl_file_read_exact(&file, buf, chunk_size * sector_size); if (n < 0) { ux_err("failed to read %s\n", program->filename); goto err_free_doc; } /* * qdl_file_read_exact() only returns short on true * EOF. The wire protocol expects exactly * chunk_size * sector_size bytes, so zero-pad the * residue (which is at most the trailing partial * sector of the file). */ if ((size_t)n < chunk_size * sector_size) memset(buf + n, 0, chunk_size * sector_size - n); } vip_gen_chunk_update(qdl, buf, chunk_size * sector_size); ret = firehose_vip_send_table(qdl); if (ret) return -1; n = qdl_write(qdl, buf, chunk_size * sector_size, zlp_timeout); if (n < 0) { ux_err("USB write failed for data chunk\n"); ret = firehose_read(qdl, 30000, firehose_generic_parser, NULL); if (ret) ux_err("flashing of chunk failed\n"); goto err_free_doc; } if ((size_t)n != chunk_size * sector_size) { ux_err("USB write truncated\n"); ret = -1; goto err_free_doc; } left -= chunk_size; vip_gen_chunk_store(qdl); ux_progress("%s", num_sectors - left, num_sectors, program->label); } t = time(NULL) - t0; ret = firehose_read(qdl, 120000, firehose_generic_parser, NULL); if (ret != FIREHOSE_ACK) { ux_err("flashing of %s failed\n", program->label); } else if (t) { ux_info("flashed \"%s\" successfully at %lukB/s\n", program->label, (unsigned long)sector_size * num_sectors / t / 1024); } else { ux_info("flashed \"%s\" successfully\n", program->label); } xmlFreeDoc(doc); free(buf); qdl_file_close(&file); return 0; err_free_doc: xmlFreeDoc(doc); free(buf); err_close_fd: qdl_file_close(&file); return -1; } static int firehose_issue_read(struct qdl_device *qdl, struct firehose_op *read_op, int fd, void *out_buf, size_t out_len, bool quiet) { unsigned int sector_size; size_t chunk_size; size_t out_offset = 0; xmlNode *root; xmlNode *node; xmlDoc *doc; void *buf; time_t t0; time_t t; size_t left; int ret; int n; buf = malloc(qdl->max_payload_size); if (!buf) { ux_err("failed to allocate sector buffer\n"); return -1; } doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); sector_size = read_op->sector_size ? : qdl->sector_size; node = xmlNewChild(root, NULL, (xmlChar *)"read", NULL); xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%d", sector_size); xml_setpropf(node, "num_partition_sectors", "%d", read_op->num_sectors); xml_setpropf(node, "physical_partition_number", "%d", read_op->partition); xml_setpropf(node, "start_sector", "%s", read_op->start_sector); if (qdl->slot != UINT_MAX) { xml_setpropf(node, "slot", "%u", qdl->slot); } if (read_op->filename) xml_setpropf(node, "filename", "%s", read_op->filename); ret = firehose_write(qdl, doc); if (ret < 0) { ux_err("failed to send read command\n"); goto out; } ret = firehose_read(qdl, 10000, firehose_generic_parser, NULL); if (ret) { if (!quiet) ux_err("failed to setup reading operation\n"); goto out; } t0 = time(NULL); left = read_op->num_sectors; while (left > 0) { size_t want; size_t got; chunk_size = MIN(qdl->max_payload_size / sector_size, left); want = chunk_size * sector_size; /* * Accumulate the chunk across qdl_read() calls. libusb usually * delivers an entire bulk transfer in one shot, but stream * transports (QUD's Windows COM port, virtio-console, ...) can * fragment it - including the rawmode tail that firehose_read() * pushed back from the same buffer as the ACK response. */ got = 0; while (got < want) { n = qdl_read(qdl, (char *)buf + got, want - got, 30000); if (n < 0) { ux_err("failed to read sector data\n"); ret = -1; goto out; } if (n == 0) { ux_err("unexpected EOF while reading sector data\n"); ret = -1; goto out; } got += (size_t)n; } if (out_buf) { size_t copy = want; if (copy > out_len - out_offset) copy = out_len - out_offset; memcpy((char *)out_buf + out_offset, buf, copy); out_offset += copy; } else { n = write(fd, buf, want); if (n < 0 || (size_t)n != want) { ux_err("failed to write sector data\n"); ret = -1; goto out; } } left -= chunk_size; if (!quiet) ux_progress("%s", read_op->num_sectors - left, read_op->num_sectors, read_op->filename); } ret = firehose_read(qdl, 10000, firehose_generic_parser, NULL); if (ret) { ux_err("read operation failed\n"); goto out; } t = time(NULL) - t0; if (!quiet) { if (t) { ux_info("read \"%s\" successfully at %ldkB/s\n", read_op->filename, (unsigned long)sector_size * read_op->num_sectors / t / 1024); } else { ux_info("read \"%s\" successfully\n", read_op->filename); } } out: xmlFreeDoc(doc); free(buf); return ret; } int firehose_read_buf(struct qdl_device *qdl, struct firehose_op *read_op, void *out_buf, size_t out_size) { return firehose_issue_read(qdl, read_op, -1, out_buf, out_size, true); } static int firehose_read_op(struct qdl_device *qdl, struct firehose_op *op) { int ret; int fd; fd = open(op->filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); if (fd < 0) { ux_info("unable to open %s...\n", op->filename); return -1; } ret = firehose_issue_read(qdl, op, fd, NULL, 0, false); close(fd); return ret; } /* * Issue a request and store the result on @op. On * success op->digest is populated and op->digest_valid is set; callers * that want to surface the digest (the qdl `sha256` subcommand prints * it after firehose_run() returns; the --skipblock=sha256 fast-path * compares it against a locally-computed digest) consume the field * themselves. */ static int firehose_getsha256digest(struct qdl_device *qdl, struct firehose_op *op) { unsigned int sector_size; xmlNode *root; xmlNode *node; xmlDoc *doc; int ret; sector_size = op->sector_size ? : qdl->sector_size; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); node = xmlNewChild(root, NULL, (xmlChar *)"getsha256digest", NULL); xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%d", sector_size); xml_setpropf(node, "num_partition_sectors", "%d", op->num_sectors); xml_setpropf(node, "physical_partition_number", "%d", op->partition); xml_setpropf(node, "start_sector", "%s", op->start_sector); if (qdl->slot != UINT_MAX) xml_setpropf(node, "slot", "%u", qdl->slot); op->digest_valid = false; ret = firehose_write(qdl, doc); if (ret < 0) { ux_err("failed to send getsha256digest command\n"); goto out; } ret = firehose_read(qdl, 30000, firehose_sha256_parser, op); if (ret != FIREHOSE_ACK) { ux_err("getsha256digest failed for %s+0x%x\n", op->start_sector, op->num_sectors); ret = -1; goto out; } if (!op->digest_valid) { ux_err("getsha256digest returned no digest for %s+0x%x\n", op->start_sector, op->num_sectors); ret = -1; goto out; } ret = 0; out: xmlFreeDoc(doc); return ret; } static int firehose_apply_patch(struct qdl_device *qdl, struct firehose_op *patch) { xmlNode *root; xmlNode *node; xmlDoc *doc; int ret; if (!patch->filename) return 0; if (strcmp(patch->filename, "DISK")) return 0; ux_debug("applying patch \"%s\"\n", patch->what); doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); node = xmlNewChild(root, NULL, (xmlChar *)"patch", NULL); xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%d", patch->sector_size); xml_setpropf(node, "byte_offset", "%d", patch->byte_offset); xml_setpropf(node, "filename", "%s", patch->filename); xml_setpropf(node, "physical_partition_number", "%d", patch->partition); xml_setpropf(node, "size_in_bytes", "%d", patch->size_in_bytes); xml_setpropf(node, "start_sector", "%s", patch->start_sector); xml_setpropf(node, "value", "%s", patch->value); if (qdl->slot != UINT_MAX) { xml_setpropf(node, "slot", "%u", qdl->slot); } ret = firehose_write(qdl, doc); if (ret < 0) goto out; ret = firehose_read(qdl, 5000, firehose_generic_parser, NULL); if (ret) ux_err("patch application failed\n"); out: xmlFreeDoc(doc); return ret == FIREHOSE_ACK ? 0 : -1; } static int firehose_send_single_tag(struct qdl_device *qdl, xmlNode *node) { xmlNode *root; xmlDoc *doc; int ret; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); xmlAddChild(root, node); ret = firehose_write(qdl, doc); if (ret < 0) goto out; ret = firehose_read(qdl, 5000, firehose_generic_parser, NULL); if (ret) { ux_err("ufs request failed\n"); ret = -EINVAL; } out: xmlFreeDoc(doc); return ret; } int firehose_apply_ufs_common(struct qdl_device *qdl, struct ufs_common *ufs) { xmlNode *node_to_send; int ret; node_to_send = xmlNewNode(NULL, (xmlChar *)"ufs"); xml_setpropf(node_to_send, "bNumberLU", "%d", ufs->bNumberLU); xml_setpropf(node_to_send, "bBootEnable", "%d", ufs->bBootEnable); xml_setpropf(node_to_send, "bDescrAccessEn", "%d", ufs->bDescrAccessEn); xml_setpropf(node_to_send, "bInitPowerMode", "%d", ufs->bInitPowerMode); xml_setpropf(node_to_send, "bHighPriorityLUN", "%d", ufs->bHighPriorityLUN); xml_setpropf(node_to_send, "bSecureRemovalType", "%d", ufs->bSecureRemovalType); xml_setpropf(node_to_send, "bInitActiveICCLevel", "%d", ufs->bInitActiveICCLevel); xml_setpropf(node_to_send, "wPeriodicRTCUpdate", "%d", ufs->wPeriodicRTCUpdate); xml_setpropf(node_to_send, "bConfigDescrLock", "%d", ufs->bConfigDescrLock); if (qdl->slot != UINT_MAX) { xml_setpropf(node_to_send, "slot", "%u", qdl->slot); } if (ufs->wb) { xml_setpropf(node_to_send, "bWriteBoosterBufferPreserveUserSpaceEn", "%d", ufs->bWriteBoosterBufferPreserveUserSpaceEn); xml_setpropf(node_to_send, "bWriteBoosterBufferType", "%d", ufs->bWriteBoosterBufferType); xml_setpropf(node_to_send, "shared_wb_buffer_size_in_kb", "%d", ufs->shared_wb_buffer_size_in_kb); } ret = firehose_send_single_tag(qdl, node_to_send); if (ret) ux_err("failed to send ufs common tag\n"); return ret == FIREHOSE_ACK ? 0 : -1; } int firehose_apply_ufs_body(struct qdl_device *qdl, struct ufs_body *ufs) { xmlNode *node_to_send; int ret; node_to_send = xmlNewNode(NULL, (xmlChar *)"ufs"); xml_setpropf(node_to_send, "LUNum", "%d", ufs->LUNum); xml_setpropf(node_to_send, "bLUEnable", "%d", ufs->bLUEnable); xml_setpropf(node_to_send, "bBootLunID", "%d", ufs->bBootLunID); xml_setpropf(node_to_send, "size_in_kb", "%d", ufs->size_in_kb); xml_setpropf(node_to_send, "bDataReliability", "%d", ufs->bDataReliability); xml_setpropf(node_to_send, "bLUWriteProtect", "%d", ufs->bLUWriteProtect); xml_setpropf(node_to_send, "bMemoryType", "%d", ufs->bMemoryType); xml_setpropf(node_to_send, "bLogicalBlockSize", "%d", ufs->bLogicalBlockSize); xml_setpropf(node_to_send, "bProvisioningType", "%d", ufs->bProvisioningType); xml_setpropf(node_to_send, "wContextCapabilities", "%d", ufs->wContextCapabilities); if (qdl->slot != UINT_MAX) { xml_setpropf(node_to_send, "slot", "%u", qdl->slot); } if (ufs->desc) xml_setpropf(node_to_send, "desc", "%s", ufs->desc); ret = firehose_send_single_tag(qdl, node_to_send); if (ret) ux_err("failed to apply ufs body tag\n"); return ret == FIREHOSE_ACK ? 0 : -1; } int firehose_apply_ufs_epilogue(struct qdl_device *qdl, struct ufs_epilogue *ufs, bool commit) { xmlNode *node_to_send; int ret; node_to_send = xmlNewNode(NULL, (xmlChar *)"ufs"); xml_setpropf(node_to_send, "LUNtoGrow", "%d", ufs->LUNtoGrow); xml_setpropf(node_to_send, "commit", "%d", commit); if (qdl->slot != UINT_MAX) { xml_setpropf(node_to_send, "slot", "%u", qdl->slot); } ret = firehose_send_single_tag(qdl, node_to_send); if (ret) ux_err("failed to apply ufs epilogue\n"); return ret == FIREHOSE_ACK ? 0 : -1; } static int firehose_set_bootable(struct qdl_device *qdl, int part) { xmlNode *root; xmlNode *node; xmlDoc *doc; int ret; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); node = xmlNewChild(root, NULL, (xmlChar *)"setbootablestoragedrive", NULL); xml_setpropf(node, "value", "%d", part); ret = firehose_write(qdl, doc); xmlFreeDoc(doc); if (ret < 0) return -1; ret = firehose_read(qdl, 5000, firehose_generic_parser, NULL); if (ret) { ux_err("failed to mark partition %d as bootable\n", part); return -1; } ux_info("partition %d is now bootable\n", part); return 0; } static int firehose_reset(struct qdl_device *qdl) { xmlNode *root; xmlNode *node; xmlDoc *doc; int ret; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewNode(NULL, (xmlChar *)"data"); xmlDocSetRootElement(doc, root); node = xmlNewChild(root, NULL, (xmlChar *)"power", NULL); xml_setpropf(node, "value", "reset"); xml_setpropf(node, "DelayInSeconds", "10"); // Add a delay to prevent reboot fail ret = firehose_write(qdl, doc); xmlFreeDoc(doc); if (ret < 0) return -1; ret = firehose_read(qdl, 5000, firehose_generic_parser, NULL); if (ret < 0) ux_err("failed to request device reset\n"); /* drain any remaining log messages for reset */ else firehose_read(qdl, 1000, firehose_generic_parser, NULL); return ret == FIREHOSE_ACK ? 0 : -1; } static int firehose_detect_and_configure(struct qdl_device *qdl, bool skip_storage_init, enum qdl_storage_type storage, unsigned int timeout_s) { struct timeval timeout = { .tv_sec = timeout_s }; struct timeval now; int ret; /* Track the currently configured storage type */ qdl->current_storage_type = storage; /* * The speculative retry loop below sends configure (and therefore the * VIP table) before the programmer has had a chance to start up and * request it. The VIP table can only be sent once per session * (VIP_INIT -> VIP_SEND_DATA is a one-way transition), so a premature * send leaves the programmer waiting for a table that was already * consumed, breaking the VIP session. * * When VIP is active, restore the original behaviour: drain all * startup log messages first, then do a single configure attempt. */ if (qdl->vip_data.state != VIP_DISABLED) { firehose_read(qdl, timeout_s * 1000, firehose_generic_parser, NULL); /* * The startup-log drain above is our only chance to learn * whether the programmer expects a VIP table before configure * is sent. configure goes through firehose_write(), which * unconditionally pushes the signed table when state is not * VIP_DISABLED; if the programmer isn't in VIP mode it will * try to parse those bytes as XML and reject the configure. * Demote VIP here so the table is never sent. */ if (!qdl->vip_data.programmer_requires_vip) { ux_info("WARNING: --vip-table-path was provided but programmer did not announce VIP; continuing without VIP\n"); qdl->vip_data.state = VIP_DISABLED; } ret = firehose_try_configure(qdl, false, storage); if (ret != FIREHOSE_ACK) { ux_err("configure request failed\n"); return -1; } return 0; } gettimeofday(&now, NULL); timeradd(&now, &timeout, &timeout); for (;;) { ret = firehose_try_configure(qdl, skip_storage_init, storage); /* * If the programmer's startup logs announced that VIP is * active but no --vip-table-path was provided, bail out now * rather than burning the full configure timeout waiting for * a signed table that will never be sent. */ if (qdl->vip_data.programmer_requires_vip) { ux_err("programmer requires VIP, but no --vip-table-path was provided\n"); return -1; } if (ret == FIREHOSE_ACK) { break; } else if (ret != -ETIMEDOUT) { ux_err("configure request failed\n"); return -1; } gettimeofday(&now, NULL); if (timercmp(&now, &timeout, >)) { ux_err("failed to detect firehose programmer\n"); return -1; } } return 0; } int firehose_provision(struct qdl_device *qdl, bool skip_reset) { int ret; ret = firehose_detect_and_configure(qdl, true, QDL_STORAGE_UFS, 5); if (ret) return ret; ret = ufs_provisioning_execute(qdl, firehose_apply_ufs_common, firehose_apply_ufs_body, firehose_apply_ufs_epilogue); if (!ret) ux_info("UFS provisioning succeeded\n"); else ux_info("UFS provisioning failed\n"); if (!skip_reset) firehose_reset(qdl); return ret; } struct firehose_op *firehose_alloc_op(int type) { struct firehose_op *op; op = calloc(1, sizeof(*op)); if (!op) return NULL; op->type = type; return op; } void firehose_free_ops(struct list_head *ops) { struct firehose_op *next; struct firehose_op *op; list_for_each_entry_safe(op, next, ops, node) { list_del(&op->node); qdl_zip_put(op->zip); free((void *)op->filename); free((void *)op->label); free((void *)op->start_sector); free((void *)op->gpt_partition); free((void *)op->value); free((void *)op->what); free(op); } } static int firehose_execute_ops(struct qdl_device *qdl, struct list_head *ops) { unsigned int patch_count = 0; struct firehose_op *status_patch = NULL; struct firehose_op *tmp; struct firehose_op *op; unsigned int patch_idx = 0; int ret; list_for_each_entry(op, ops, node) { switch (op->type) { case FIREHOSE_OP_CONFIGURE: ret = firehose_detect_and_configure(qdl, false, op->storage_type, 5); if (ret) return ret; ret = gpt_resolve_deferrals(qdl, ops); if (ret) return ret; /* Update the number of patches for this storage device */ patch_count = 0; patch_idx = 0; tmp = op; list_for_each_entry_continue(tmp, ops, node) { if (tmp->type == FIREHOSE_OP_CONFIGURE) break; if (tmp->type != FIREHOSE_OP_PATCH) continue; if (tmp->filename && !strcmp(tmp->filename, "DISK")) { patch_count++; status_patch = tmp; } } break; case FIREHOSE_OP_PROGRAM: ret = firehose_program(qdl, op); if (ret < 0) return ret; break; case FIREHOSE_OP_ERASE: ret = firehose_erase(qdl, op); if (ret < 0) return ret; break; case FIREHOSE_OP_READ: ret = firehose_read_op(qdl, op); if (ret < 0) return ret; break; case FIREHOSE_OP_GET_SHA256_DIGEST: ret = firehose_getsha256digest(qdl, op); if (ret < 0) return ret; break; case FIREHOSE_OP_PATCH: ret = firehose_apply_patch(qdl, op); if (ret) return ret; if (op->filename && !strcmp(op->filename, "DISK")) ux_progress("Applying patches", ++patch_idx, patch_count); if (op == status_patch) ux_info("%d patches applied\n", patch_idx); break; case FIREHOSE_OP_SET_BOOTABLE: firehose_set_bootable(qdl, op->partition); break; case FIREHOSE_OP_RESET: ret = firehose_reset(qdl); if (ret < 0) return ret; break; default: ux_err("internal error: unknown firehose operation %d\n", op->type); return -1; } } return 0; } int firehose_run(struct qdl_device *qdl, struct list_head *ops) { ux_info("waiting for Firehose programmer...\n"); return firehose_execute_ops(qdl, ops); } linux-msm-qdl-f540e59/firehose.h000066400000000000000000000025371521156022300165450ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __FIREHOSE_H__ #define __FIREHOSE_H__ #include #include #include #include "list.h" #include "qdl.h" #include "sha2.h" enum firehose_op_type { FIREHOSE_OP_NONE, FIREHOSE_OP_CONFIGURE, FIREHOSE_OP_PROGRAM, FIREHOSE_OP_ERASE, FIREHOSE_OP_READ, FIREHOSE_OP_PATCH, FIREHOSE_OP_SET_BOOTABLE, FIREHOSE_OP_RESET, FIREHOSE_OP_GET_SHA256_DIGEST, }; struct firehose_op { enum firehose_op_type type; struct list_head node; /* program, read, patch, set_bootable */ int partition; /* program, read, patch */ unsigned int sector_size; struct qdl_zip *zip; const char *filename; const char *start_sector; /* program, read */ unsigned int num_sectors; const char *gpt_partition; /* program, erase */ unsigned int pages_per_block; unsigned int file_offset; const char *label; bool sparse; unsigned int last_sector; bool is_nand; unsigned int sparse_chunk_type; uint32_t sparse_fill_value; off_t sparse_offset; /* patch */ unsigned int byte_offset; unsigned int size_in_bytes; const char *value; const char *what; /* configure */ enum qdl_storage_type storage_type; /* getsha256digest */ uint8_t digest[SHA256_DIGEST_LENGTH]; bool digest_valid; }; struct firehose_op *firehose_alloc_op(int type); void firehose_free_ops(struct list_head *ops); #endif linux-msm-qdl-f540e59/flashmap.c000066400000000000000000000245221521156022300165250ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #include #include #include #include #include #include #include #include "file.h" #include "json.h" #include "list.h" #include "firehose.h" #include "qdl.h" enum { QDL_FILE_UNKNOWN, QDL_FILE_PATCH, QDL_FILE_PROGRAM, }; static int flashmap_resolve_path(char *path, size_t path_size, const char *filename, const char *incdir) { if (!filename) { ux_err("flashmap: filename is null\n"); return -1; } if (incdir) { snprintf(path, path_size, "%s/%s", incdir, filename); if (access(path, F_OK)) snprintf(path, path_size, "%s", filename); } else { snprintf(path, path_size, "%s", filename); } return 0; } static int flashmap_get_legacy_programmer(struct qdl_zip *zip, struct json_value *layout, struct sahara_image *images, const char *incdir) { struct json_value *programmers; const char *filename; char path[PATH_MAX]; int count; programmers = json_get_child(layout, "programmer"); if (!programmers) { ux_err("flashmap: parse error when decoding programmer\n"); return -1; } count = json_count_children(programmers); if (count != 1) { ux_err("flashmap: single programmer expected, found %d\n", count); return -1; } filename = json_get_element_string(programmers, 0); if (!filename) { ux_err("flashmap: parse error when decoding programmer\n"); return -1; } if (flashmap_resolve_path(path, sizeof(path), filename, incdir)) return -1; ux_debug("flashmap: selected programmer: %s\n", path); return load_sahara_image(zip, path, &images[SAHARA_ID_EHOSTDL_IMG]); } static int flashmap_get_programmer_map(struct qdl_zip *zip, struct json_value *layout, struct sahara_image *images, const char *incdir) { struct json_value *programmers; struct json_value *entry; unsigned long image_id; const char *filename; char *end; char path[PATH_MAX]; int count = 0; int ret; programmers = json_get_child(layout, "programmer"); if (!programmers || programmers->type != JSON_TYPE_OBJECT) { ux_err("flashmap: programmer map must be an object for version 1.2.0-qdl\n"); return -1; } for (entry = programmers->u.value; entry; entry = entry->next) { errno = 0; image_id = strtoul(entry->key, &end, 0); if (errno || end == entry->key || *end || image_id == 0 || image_id >= MAPPING_SZ) { ux_err("flashmap: invalid programmer image id \"%s\"\n", entry->key); return -1; } if (entry->type != JSON_TYPE_STRING || !entry->u.string) { ux_err("flashmap: programmer entry \"%s\" must be a filename string\n", entry->key); return -1; } filename = entry->u.string; ret = flashmap_resolve_path(path, sizeof(path), filename, incdir); if (ret) return ret; ux_debug("flashmap: selected programmer %lu: %s\n", image_id, path); ret = load_sahara_image(zip, path, &images[image_id]); if (ret) return ret; count++; } if (!count) { ux_err("flashmap: programmer map is empty\n"); return -1; } return 0; } static int flashmap_get_programmers(struct qdl_zip *zip, struct json_value *layout, struct sahara_image *images, const char *incdir, bool uses_programmer_map) { if (uses_programmer_map) return flashmap_get_programmer_map(zip, layout, images, incdir); return flashmap_get_legacy_programmer(zip, layout, images, incdir); } static int flashmap_load_xml(struct list_head *ops, struct qdl_zip *zip, const char *filename, bool is_nand, const char *incdir) { struct qdl_file file; xmlNode *node; xmlNode *root; xmlDoc *doc; size_t len; void *xml; int type = QDL_FILE_UNKNOWN; int ret; ret = qdl_file_open(zip, filename, &file); if (ret < 0) { ux_err("unable to parse XML: %s\n", filename); return -1; } xml = qdl_file_load(&file, &len); if (!xml) { ux_err("failed to load \"%s\"\n", filename); ret = -1; goto out_close_file; } if (len > INT_MAX) { ux_err("\"%s\" too large for XML parser\n", filename); ret = -1; goto out_free_xml; } doc = xmlReadMemory(xml, (int)len, filename, NULL, 0); if (!doc) { ux_err("failed to parse %s\n", filename); ret = -1; goto out_free_xml; } root = xmlDocGetRootElement(doc); if (!root) { ux_err("unable to find XML root in \"%s\"\n", filename); ret = -1; goto out_free_doc; } if (!xmlStrcmp(root->name, (xmlChar *)"patches")) { type = QDL_FILE_PATCH; } else if (!xmlStrcmp(root->name, (xmlChar *)"data")) { type = QDL_FILE_PROGRAM; for (node = root->children; node ; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (!xmlStrcmp(node->name, (xmlChar *)"program") || !xmlStrcmp(node->name, (xmlChar *)"erase")) { type = QDL_FILE_PROGRAM; break; } } } switch (type) { case QDL_FILE_PROGRAM: ret = program_load_xml(ops, doc, zip, filename, is_nand, false, NULL, incdir); break; case QDL_FILE_PATCH: ret = patch_load_xml(ops, doc, filename); break; default: ux_err("unknown file type: %s\n", filename); ret = -1; break; } out_free_doc: xmlFreeDoc(doc); out_free_xml: free(xml); out_close_file: qdl_file_close(&file); return ret; } static int flashmap_enumerate_programmables(struct json_value *list, struct list_head *ops, struct qdl_zip *zip, const char *incdir) { struct json_value *programmable; struct json_value *files; struct firehose_op *op; char path[PATH_MAX]; const char *memory; const char *file; int file_count; int file_idx; bool is_nand; double slot; int count; int ret; int i; count = json_count_children(list); if (count < 0) { ux_err("flashmap: programmable list is not an array\n"); return -1; } if (count == 0) { ux_err("flashmap: programmable list is empty\n"); return -1; } for (i = 0; i < count; i++) { programmable = json_get_element_object(list, i); memory = json_get_string(programmable, "memory"); ret = json_get_number(programmable, "slot", &slot); files = json_get_child(programmable, "files"); if (!memory || ret < 0 || !files) { ux_err("failed to parse flashmap\n"); return -1; } is_nand = !strcmp(memory, "nand"); op = firehose_alloc_op(FIREHOSE_OP_CONFIGURE); op->storage_type = decode_storage_type(memory); list_append(ops, &op->node); file_count = json_count_children(files); if (file_count < 0) { ux_err("flashmap: files list is not an array\n"); return -1; } for (file_idx = 0; file_idx < file_count; file_idx++) { file = json_get_element_string(files, file_idx); if (!file) { ux_err("flashmap: parse error when decoding files\n"); return -1; } if (flashmap_resolve_path(path, sizeof(path), file, incdir)) return -1; ret = flashmap_load_xml(ops, zip, path, is_nand, incdir); if (ret) return ret; } } return 0; } int flashmap_load(struct list_head *ops, const char *filename, char *specifier, struct sahara_image *images, const char *incdir) { struct list_head flashmap_ops = LIST_INIT(flashmap_ops); enum qdl_storage_type current_type = QDL_STORAGE_UNKNOWN; struct json_value *programmable; struct json_value *product; struct json_value *layout; struct firehose_op *op; struct firehose_op *next; struct qdl_file flashmap; struct json_value *json; struct json_value *obj; struct qdl_zip *zip; unsigned int type_filter = 0; unsigned int matched_ops = 0; char *filter = specifier; char *save = NULL; char *tmp; enum qdl_storage_type type; bool uses_programmer_map = false; const char *version; const char *name; size_t json_size; void *json_blob; int count; int ret = -1; if (filter) { for (tmp = strtok_r(filter, ",", &save); tmp; tmp = strtok_r(NULL, ",", &save)) { type = decode_storage_type(tmp); if (type == QDL_STORAGE_UNKNOWN) { ux_err("unknown storage type \"%s\"\n", tmp); return -1; } type_filter |= 1U << type; } } if (!type_filter) type_filter = ~0U; ret = qdl_zip_open(filename, &zip); if (ret < 0) { ux_err("unable to create zip reference\n"); return -1; } ret = qdl_file_open(zip, zip ? "flashmap.json" : filename, &flashmap); if (ret < 0) { ux_err("failed to open flashmap\n"); goto out_put_zip; } json_blob = qdl_file_load(&flashmap, &json_size); qdl_file_close(&flashmap); if (!json_blob) goto out_put_zip; json = json_parse_buf(json_blob, json_size); if (!json) { ux_err("failed to parse flashmap json\n"); goto out_free_blob; } version = json_get_string(json, "version"); if (!version) { ux_err("unsupported flashmap version\n"); ret = -1; goto out_free_json; } if (!strcmp(version, "1.2.0-qdl")) uses_programmer_map = true; else if (strcmp(version, "1.1.0")) { ux_err("unsupported flashmap version\n"); ret = -1; goto out_free_json; } ux_debug("found flashmap of version: %s\n", version); obj = json_get_child(json, "products"); count = json_count_children(obj); if (count != 1) { ux_err("flashmap: only single product map supported, found %d\n", count); ret = -1; goto out_free_json; } product = json_get_element_object(obj, 0); name = json_get_string(product, "name"); ux_debug("product: %s\n", name); obj = json_get_child(product, "layouts"); count = json_count_children(obj); if (count != 1) { ux_err("flashmap: only one layout supported, found %d\n", count); ret = -1; goto out_free_json; } layout = json_get_element_object(obj, 0); ret = flashmap_get_programmers(zip, layout, images, zip ? NULL : incdir, uses_programmer_map); if (ret) goto out_free_json; programmable = json_get_child(layout, "programmable"); if (!programmable) { ux_err("flashmap: parse error when decoding programmables\n"); sahara_images_free(images, MAPPING_SZ); ret = -1; goto out_free_json; } ret = flashmap_enumerate_programmables(programmable, &flashmap_ops, zip, zip ? NULL : incdir); if (ret < 0) { firehose_free_ops(&flashmap_ops); sahara_images_free(images, MAPPING_SZ); goto out_free_json; } list_for_each_entry_safe(op, next, &flashmap_ops, node) { if (op->storage_type != QDL_STORAGE_UNKNOWN) current_type = op->storage_type; if ((1U << current_type) & type_filter) { list_del(&op->node); list_append(ops, &op->node); matched_ops++; } } firehose_free_ops(&flashmap_ops); if (!matched_ops) { ux_err("loaded flashmap does not contain any operations for selected storage type\n"); ret = -1; } out_free_json: json_free(json); out_free_blob: free(json_blob); out_put_zip: qdl_zip_put(zip); return ret; } linux-msm-qdl-f540e59/flashmap.h000066400000000000000000000007201521156022300165240ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #ifndef __FLASHMAP_H__ #define __FLASHMAP_H__ #include #include "list.h" struct sahara_image; int flashmap_load(struct list_head *ops, const char *filename, char *specifier, struct sahara_image *images, const char *incdir); int zipper_write(const char *filename, struct list_head *ops, struct sahara_image *images); #endif linux-msm-qdl-f540e59/gpt.c000066400000000000000000000164101521156022300155210ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #include "firehose.h" #include #include #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include "qdl.h" #include "gpt.h" struct gpt_guid { uint32_t data1; uint16_t data2; uint16_t data3; uint8_t data4[8]; } __attribute__((packed)); static const struct gpt_guid gpt_zero_guid = {0}; struct gpt_header { uint8_t signature[8]; uint32_t revision; uint32_t header_size; uint32_t header_crc32; uint32_t reserved; uint64_t current_lba; uint64_t backup_lba; uint64_t first_usable_lba; uint64_t last_usable_lba; struct gpt_guid disk_guid; uint64_t part_entry_lba; uint32_t num_part_entries; uint32_t part_entry_size; uint32_t part_array_crc32; uint8_t reserved2[420]; } __attribute__((packed)); struct gpt_entry { struct gpt_guid type_guid; struct gpt_guid unique_guid; uint64_t first_lba; uint64_t last_lba; uint64_t attrs; uint16_t name_utf16le[36]; } __attribute__((packed)); struct gpt_partition { const char *name; unsigned int partition; unsigned int start_sector; unsigned int num_sectors; struct gpt_partition *next; }; static struct gpt_partition *gpt_partitions; static struct gpt_partition *gpt_partitions_last; static void utf16le_to_utf8(uint16_t *in, size_t in_len, uint8_t *out, size_t out_len) { uint32_t codepoint; uint16_t high; uint16_t low; uint16_t w; size_t i; size_t j = 0; for (i = 0; i < in_len; i++) { w = in[i]; if (w >= 0xd800 && w <= 0xdbff) { high = w - 0xd800; if (i < in_len) { w = in[++i]; if (w >= 0xdc00 && w <= 0xdfff) { low = w - 0xdc00; codepoint = (((uint32_t)high << 10) | low) + 0x10000; } else { /* Surrogate without low surrogate */ codepoint = 0xfffd; } } else { /* Lone high surrogate at end of string */ codepoint = 0xfffd; } } else if (w >= 0xdc00 && w <= 0xdfff) { /* Low surrogate without high */ codepoint = 0xfffd; } else { codepoint = w; } if (codepoint == 0) break; if (codepoint <= 0x7f) { if (j + 1 >= out_len) break; out[j++] = (uint8_t)codepoint; } else if (codepoint <= 0x7ff) { if (j + 2 >= out_len) break; out[j++] = 0xc0 | ((codepoint >> 6) & 0x1f); out[j++] = 0x80 | (codepoint & 0x3f); } else if (codepoint <= 0xffff) { if (j + 3 >= out_len) break; out[j++] = 0xe0 | ((codepoint >> 12) & 0x0f); out[j++] = 0x80 | ((codepoint >> 6) & 0x3f); out[j++] = 0x80 | (codepoint & 0x3f); } else if (codepoint <= 0x10ffff) { if (j + 4 >= out_len) break; out[j++] = 0xf0 | ((codepoint >> 18) & 0x07); out[j++] = 0x80 | ((codepoint >> 12) & 0x3f); out[j++] = 0x80 | ((codepoint >> 6) & 0x3f); out[j++] = 0x80 | (codepoint & 0x3f); } } out[j] = '\0'; } static int gpt_load_table_from_partition(struct qdl_device *qdl, unsigned int phys_partition, bool *eof) { struct gpt_partition *partition; struct gpt_entry *entry; struct gpt_header gpt; uint8_t buf[4096]; struct firehose_op op; unsigned int offset; uint64_t lba; char lba_buf[21]; uint16_t name_utf16le[36]; char name[36 * 4]; int ret; unsigned int i; memset(&op, 0, sizeof(op)); op.type = FIREHOSE_OP_READ; op.sector_size = qdl->sector_size; op.start_sector = "1"; op.num_sectors = 1; op.partition = phys_partition; memset(&buf, 0, sizeof(buf)); ret = firehose_read_buf(qdl, &op, &gpt, sizeof(gpt)); if (ret) { /* Assume that we're beyond the last partition */ *eof = true; return -1; } if (memcmp(gpt.signature, "EFI PART", 8)) { ux_err("partition %d has not GPT header\n", phys_partition); return 0; } if (gpt.part_entry_size > qdl->sector_size || gpt.num_part_entries > 1024) { ux_debug("partition %d has invalid GPT header\n", phys_partition); return -1; } ux_debug("Loading GPT table from physical partition %d\n", phys_partition); for (i = 0; i < gpt.num_part_entries; i++) { offset = (i * gpt.part_entry_size) % qdl->sector_size; if (offset == 0) { lba = gpt.part_entry_lba + i * gpt.part_entry_size / qdl->sector_size; snprintf(lba_buf, sizeof(lba_buf), "%" PRIu64, lba); op.start_sector = lba_buf; memset(buf, 0, sizeof(buf)); ret = firehose_read_buf(qdl, &op, buf, sizeof(buf)); if (ret) { ux_err("failed to read GPT partition entries from %d:%u\n", phys_partition, lba); return -1; } } entry = (struct gpt_entry *)(buf + offset); if (!memcmp(&entry->type_guid, &gpt_zero_guid, sizeof(struct gpt_guid))) continue; memcpy(name_utf16le, entry->name_utf16le, sizeof(name_utf16le)); utf16le_to_utf8(name_utf16le, 36, (uint8_t *)name, sizeof(name)); partition = calloc(1, sizeof(*partition)); partition->name = strdup(name); partition->partition = phys_partition; partition->start_sector = entry->first_lba; /* if first_lba == last_lba there is 1 sector worth of data (IE: add 1 below) */ partition->num_sectors = entry->last_lba - entry->first_lba + 1; ux_debug(" %3d: %s start sector %u, num sectors %u\n", i, partition->name, partition->start_sector, partition->num_sectors); if (gpt_partitions) { gpt_partitions_last->next = partition; gpt_partitions_last = partition; } else { gpt_partitions = partition; gpt_partitions_last = partition; } } return 0; } static int gpt_load_tables(struct qdl_device *qdl) { unsigned int i; bool eof = false; int ret = 0; if (gpt_partitions) return 0; for (i = 0; ; i++) { ret = gpt_load_table_from_partition(qdl, i, &eof); if (ret) break; } return eof ? 0 : ret; } int gpt_find_by_name(struct qdl_device *qdl, const char *name, int *phys_partition, unsigned int *start_sector, unsigned int *num_sectors) { struct gpt_partition *gpt_part; bool found = false; int ret; if (qdl->dev_type == QDL_DEVICE_SIM) return 0; ret = gpt_load_tables(qdl); if (ret < 0) return -1; for (gpt_part = gpt_partitions; gpt_part; gpt_part = gpt_part->next) { if (*phys_partition >= 0 && gpt_part->partition != (unsigned int)(*phys_partition)) continue; if (strcmp(gpt_part->name, name)) continue; if (found) { ux_err("duplicate candidates for partition \"%s\" found\n", name); return -1; } *phys_partition = gpt_part->partition; *start_sector = gpt_part->start_sector; *num_sectors = gpt_part->num_sectors; found = true; } if (!found) { if (*phys_partition >= 0) ux_err("no partition \"%s\" found on physical partition %d\n", name, *phys_partition); else ux_err("no partition \"%s\" found\n", name); return -1; } return 0; } int gpt_resolve_deferrals(struct qdl_device *qdl, struct list_head *ops) { unsigned int start_sector; struct firehose_op *op; char buf[20]; int ret; list_for_each_entry(op, ops, node) { if (op->type != FIREHOSE_OP_PROGRAM && op->type != FIREHOSE_OP_ERASE && op->type != FIREHOSE_OP_READ && op->type != FIREHOSE_OP_GET_SHA256_DIGEST) continue; if (!op->gpt_partition) continue; ret = gpt_find_by_name(qdl, op->gpt_partition, &op->partition, &start_sector, &op->num_sectors); if (ret < 0) return -1; sprintf(buf, "%u", start_sector); op->start_sector = strdup(buf); } return 0; } linux-msm-qdl-f540e59/gpt.h000066400000000000000000000005521521156022300155260ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __GPT_H__ #define __GPT_H__ #include "list.h" struct qdl_device; struct list_head; int gpt_find_by_name(struct qdl_device *qdl, const char *name, int *partition, unsigned int *start_sector, unsigned int *num_sectors); int gpt_resolve_deferrals(struct qdl_device *qdl, struct list_head *ops); #endif linux-msm-qdl-f540e59/io.c000066400000000000000000000066641521156022300153500ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved. */ #include #include #include #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(); if (type == QDL_DEVICE_QUD) return qud_init(); if (type == QDL_DEVICE_AUTO) return auto_init(); return NULL; } void qdl_deinit(struct qdl_device *qdl) { if (qdl) { free(qdl->pending_buf); 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); } /** * qdl_read() - Read a message from the device * @qdl: device handle * @buf: buffer to write the data into * @len: maximum length of data to be read * @timeout: timeout for the read, in milliseconds * * Drains the pushback buffer (qdl_push_back()) before touching the underlying * transport, so a previous read that crossed a Firehose message boundary can * deliver the trailing bytes here. * * Returns: number of bytes read, might be zero for a ZLP * negative errno on failure (notably -ETIMEDOUT) */ int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout) { size_t available; size_t copy; if (qdl->pending_buf) { available = qdl->pending_len - qdl->pending_off; copy = available < len ? available : len; memcpy(buf, qdl->pending_buf + qdl->pending_off, copy); qdl->pending_off += copy; if (qdl->pending_off >= qdl->pending_len) { free(qdl->pending_buf); qdl->pending_buf = NULL; qdl->pending_len = 0; qdl->pending_off = 0; } return (int)copy; } return qdl->read(qdl, buf, len, timeout); } /** * qdl_push_back() - Stash unread bytes for a future qdl_read() * @qdl: device handle * @buf: bytes to remember * @len: number of bytes * * Concatenates @buf onto whatever is already pending (after any already-served * prefix). Used by firehose_read() when a single transport read returned more * than one Firehose message - or an XML envelope followed immediately by its * rawmode binary payload - and we need the trailing bytes to surface on the * next qdl_read() before any new transport I/O happens. * * Returns: 0 on success, negative errno on allocation failure. */ int qdl_push_back(struct qdl_device *qdl, const void *buf, size_t len) { char *grown; size_t total; if (!len) return 0; if (qdl->pending_off > 0) { size_t leftover = qdl->pending_len - qdl->pending_off; memmove(qdl->pending_buf, qdl->pending_buf + qdl->pending_off, leftover); qdl->pending_len = leftover; qdl->pending_off = 0; } total = qdl->pending_len + len; grown = realloc(qdl->pending_buf, total); if (!grown) return -ENOMEM; memcpy(grown + qdl->pending_len, buf, len); qdl->pending_buf = grown; qdl->pending_len = total; return 0; } /** * qdl_write() - Write a message from the device * @qdl: device handle * @buf: buffer with data to be written * @len: length of data to be written * @timeout: timeout for write, in milliseconds * * Returns: number of bytes read * negative errno on failure (notably -ETIMEDOUT) */ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len, unsigned int timeout) { return qdl->write(qdl, buf, len, timeout); } linux-msm-qdl-f540e59/json.c000066400000000000000000000426151521156022300157060ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. * Copyright (c) 2018-2019, Linaro Ltd. * 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 #include #include #include #include #include #include #include #include "json.h" static const char *input_buf; static int input_pos; static int input_len; static bool input_can_unput; static int nesting_depth; char json_error[JSON_ERROR_SIZE]; enum { JSON_INPUT_EOF = -1 }; static int json_parse_array(struct json_value *array); static int json_parse_object(struct json_value *object); static int json_parse_property(struct json_value *value); static int input(void); static void unput(void); static void json_set_error(const char *fmt, ...) { va_list ap; if (json_error[0]) return; va_start(ap, fmt); vsnprintf(json_error, sizeof(json_error), fmt, ap); va_end(ap); } static int json_hex_value(int ch) { if (ch >= '0' && ch <= '9') return ch - '0'; if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; return -1; } static int json_buf_append(char **buf, size_t *len, size_t *cap, unsigned char byte) { char *new_buf; if (*len + 1 >= *cap) { size_t new_cap = *cap ? (*cap * 2) : 32; new_buf = realloc(*buf, new_cap); if (!new_buf) return -1; *buf = new_buf; *cap = new_cap; } (*buf)[*len] = (char)byte; (*len)++; return 0; } static int json_buf_append_utf8(char **buf, size_t *len, size_t *cap, unsigned int cp) { if (cp <= 0x7f) return json_buf_append(buf, len, cap, (unsigned char)cp); if (cp <= 0x7ff) { if (json_buf_append(buf, len, cap, (unsigned char)(0xc0 | (cp >> 6)))) return -1; return json_buf_append(buf, len, cap, (unsigned char)(0x80 | (cp & 0x3f))); } if (cp <= 0xffff) { if (json_buf_append(buf, len, cap, (unsigned char)(0xe0 | (cp >> 12)))) return -1; if (json_buf_append(buf, len, cap, (unsigned char)(0x80 | ((cp >> 6) & 0x3f)))) return -1; return json_buf_append(buf, len, cap, (unsigned char)(0x80 | (cp & 0x3f))); } if (cp <= 0x10ffff) { if (json_buf_append(buf, len, cap, (unsigned char)(0xf0 | (cp >> 18)))) return -1; if (json_buf_append(buf, len, cap, (unsigned char)(0x80 | ((cp >> 12) & 0x3f)))) return -1; if (json_buf_append(buf, len, cap, (unsigned char)(0x80 | ((cp >> 6) & 0x3f)))) return -1; return json_buf_append(buf, len, cap, (unsigned char)(0x80 | (cp & 0x3f))); } return -1; } static int json_parse_hex4(unsigned int *out) { unsigned int code = 0; int i; for (i = 0; i < 4; i++) { int ch = input(); int digit; if (ch == JSON_INPUT_EOF) return -1; digit = json_hex_value(ch); if (digit < 0) return -1; code = (code << 4) | (unsigned int)digit; } *out = code; return 0; } static int json_parse_escape(char **buf, size_t *len, size_t *cap, int escape_pos) { unsigned int code; unsigned int low; int ch; ch = input(); if (ch == JSON_INPUT_EOF) { json_set_error("unterminated escape at offset %d", escape_pos); return -1; } switch (ch) { case '"': case '\\': case '/': if (json_buf_append(buf, len, cap, (unsigned char)ch)) { json_set_error("out of memory while parsing escape at offset %d", escape_pos); return -1; } return 0; case 'b': if (json_buf_append(buf, len, cap, '\b')) { json_set_error("out of memory while parsing escape at offset %d", escape_pos); return -1; } return 0; case 'f': if (json_buf_append(buf, len, cap, '\f')) { json_set_error("out of memory while parsing escape at offset %d", escape_pos); return -1; } return 0; case 'n': if (json_buf_append(buf, len, cap, '\n')) { json_set_error("out of memory while parsing escape at offset %d", escape_pos); return -1; } return 0; case 'r': if (json_buf_append(buf, len, cap, '\r')) { json_set_error("out of memory while parsing escape at offset %d", escape_pos); return -1; } return 0; case 't': if (json_buf_append(buf, len, cap, '\t')) { json_set_error("out of memory while parsing escape at offset %d", escape_pos); return -1; } return 0; case 'u': if (json_parse_hex4(&code)) { json_set_error("invalid unicode escape at offset %d", escape_pos); return -1; } if (code >= 0xd800 && code <= 0xdbff) { ch = input(); if (ch != '\\') { json_set_error("expected low surrogate escape at offset %d", input_pos - 1); return -1; } ch = input(); if (ch != 'u') { json_set_error("expected low surrogate escape at offset %d", input_pos - 1); return -1; } if (json_parse_hex4(&low)) { json_set_error("invalid low surrogate escape at offset %d", input_pos); return -1; } if (low < 0xdc00 || low > 0xdfff) { json_set_error("invalid low surrogate value at offset %d", input_pos); return -1; } code = 0x10000 + (((code - 0xd800) << 10) | (low - 0xdc00)); } else if (code >= 0xdc00 && code <= 0xdfff) { json_set_error("unexpected low surrogate at offset %d", escape_pos); return -1; } if (json_buf_append_utf8(buf, len, cap, code)) { json_set_error("out of memory while decoding unicode escape"); return -1; } return 0; default: json_set_error("invalid escape '\\%c' at offset %d", ch, escape_pos); return -1; } } static int json_enter_nesting(int offset) { nesting_depth++; if (nesting_depth > JSON_MAX_DEPTH) { nesting_depth--; json_set_error("maximum nesting depth %d exceeded at offset %d", JSON_MAX_DEPTH, offset); return -1; } return 0; } static void json_leave_nesting(void) { if (nesting_depth > 0) nesting_depth--; } static int input(void) { if (input_pos >= input_len) { input_can_unput = false; return JSON_INPUT_EOF; } input_can_unput = true; return (unsigned char)input_buf[input_pos++]; } static void unput(void) { if (input_can_unput && input_pos > 0) { input_pos--; input_can_unput = false; } } static void json_skip_whitespace(void) { int ch; while ((ch = input()) != JSON_INPUT_EOF && isspace((unsigned char)ch)) ; unput(); } static int json_parse_string(struct json_value *value) { size_t len = 0; size_t cap = 0; char *str; int string_start; int ch; ch = input(); if (ch != '"') { unput(); return 0; } string_start = input_pos - 1; str = NULL; while ((ch = input()) != JSON_INPUT_EOF) { if (ch == '"') break; if (ch == '\\') { if (json_parse_escape(&str, &len, &cap, input_pos - 1)) { free(str); return -1; } continue; } if (ch < 0x20) { json_set_error("unescaped control character in string at offset %d", input_pos - 1); free(str); return -1; } if (json_buf_append(&str, &len, &cap, (unsigned char)ch)) { json_set_error("out of memory while parsing string"); free(str); return -1; } } if (ch == JSON_INPUT_EOF) { json_set_error("unterminated string starting at offset %d", string_start); free(str); return -1; } if (json_buf_append(&str, &len, &cap, '\0')) { json_set_error("out of memory while finalizing string"); free(str); return -1; } value->type = JSON_TYPE_STRING; value->u.string = str; return 1; } static int json_parse_number(struct json_value *value) { char *token; char *endptr; double parsed; size_t len; int start; int end; int ch; ch = input(); if (ch != '-' && (ch == JSON_INPUT_EOF || !isdigit(ch))) { unput(); return 0; } start = input_pos - 1; if (ch == '-') { ch = input(); if (ch == JSON_INPUT_EOF || !isdigit(ch)) { json_set_error("invalid number: '-' not followed by digit at offset %d", start); return -1; } } if (ch == '0') { ch = input(); if (ch != JSON_INPUT_EOF && isdigit(ch)) { json_set_error("invalid number: leading zero at offset %d", start); return -1; } } else if (isdigit(ch)) { do { ch = input(); } while (ch != JSON_INPUT_EOF && isdigit(ch)); } else { json_set_error("invalid number at offset %d", start); return -1; } if (ch == '.') { ch = input(); if (ch == JSON_INPUT_EOF || !isdigit(ch)) { json_set_error("invalid number: fractional part missing digits at offset %d", start); return -1; } do { ch = input(); } while (ch != JSON_INPUT_EOF && isdigit(ch)); } if (ch == 'e' || ch == 'E') { ch = input(); if (ch == '+' || ch == '-') ch = input(); if (ch == JSON_INPUT_EOF || !isdigit(ch)) { json_set_error("invalid number: exponent missing digits at offset %d", start); return -1; } do { ch = input(); } while (ch != JSON_INPUT_EOF && isdigit(ch)); } if (ch != JSON_INPUT_EOF) { end = input_pos - 1; unput(); } else { end = input_pos; } len = (size_t)(end - start); token = malloc(len + 1); if (!token) { json_set_error("out of memory while parsing number"); return -1; } memcpy(token, input_buf + start, len); token[len] = '\0'; errno = 0; parsed = strtod(token, &endptr); if (endptr != token + len || errno == ERANGE || !isfinite(parsed)) { json_set_error("invalid or out-of-range number at offset %d", start); free(token); return -1; } free(token); value->type = JSON_TYPE_NUMBER; value->u.number = parsed; return 1; } static int json_parse_keyword(struct json_value *value) { const char *match; int i; int ch; ch = input(); switch (ch) { case 't': match = "true"; value->type = JSON_TYPE_TRUE; break; case 'f': match = "false"; value->type = JSON_TYPE_FALSE; break; case 'n': match = "null"; value->type = JSON_TYPE_NULL; break; default: unput(); return 0; } for (i = 1; match[i]; i++) { ch = input(); if (ch == JSON_INPUT_EOF || ch != match[i]) { json_set_error("invalid keyword at offset %d", input_pos - 1); return -1; } } return 1; } static int json_parse_value(struct json_value *value) { int ret; json_skip_whitespace(); ret = json_parse_object(value); if (ret) goto out; ret = json_parse_array(value); if (ret) goto out; ret = json_parse_string(value); if (ret) goto out; ret = json_parse_number(value); if (ret) goto out; ret = json_parse_keyword(value); if (ret) goto out; json_set_error("unable to match value at offset %d", input_pos); return -1; out: json_skip_whitespace(); return ret; } static int json_parse_array(struct json_value *array) { struct json_value *value; struct json_value *last = NULL; int ret; int ch; ch = input(); if (ch != '[') { unput(); return 0; } if (json_enter_nesting(input_pos - 1)) return -1; array->type = JSON_TYPE_ARRAY; json_skip_whitespace(); ch = input(); if (ch == ']') { json_leave_nesting(); return 1; } unput(); do { value = calloc(1, sizeof(*value)); if (!value) { json_set_error("out of memory while parsing array at offset %d", input_pos); json_leave_nesting(); return -1; } ret = json_parse_value(value); if (ret <= 0) { json_set_error("invalid array element at offset %d", input_pos); free(value); json_leave_nesting(); return -1; } if (!array->u.value) array->u.value = value; if (last) last->next = value; last = value; ch = input(); if (ch == ']') { json_leave_nesting(); return 1; } } while (ch == ','); json_set_error("expected ',' or ']' at offset %d", input_pos - 1); json_leave_nesting(); return -1; } static int json_parse_object(struct json_value *object) { struct json_value *value; struct json_value *last = NULL; int ret; int ch; ch = input(); if (ch != '{') { unput(); return 0; } if (json_enter_nesting(input_pos - 1)) return -1; object->type = JSON_TYPE_OBJECT; json_skip_whitespace(); ch = input(); if (ch == '}') { json_leave_nesting(); return 1; } unput(); do { value = calloc(1, sizeof(*value)); if (!value) { json_set_error("out of memory while parsing object at offset %d", input_pos); json_leave_nesting(); return -1; } ret = json_parse_property(value); if (ret <= 0) { json_set_error("invalid object property at offset %d", input_pos); free(value); json_leave_nesting(); return -1; } if (!object->u.value) object->u.value = value; if (last) last->next = value; last = value; ch = input(); if (ch == '}') { json_leave_nesting(); return 1; } } while (ch == ','); json_set_error("expected ',' or '}' at offset %d", input_pos - 1); json_leave_nesting(); return -1; } static int json_parse_property(struct json_value *value) { struct json_value key; int ret; int ch; json_skip_whitespace(); ret = json_parse_string(&key); if (ret <= 0) { json_set_error("expected string key at offset %d", input_pos); return -1; } value->key = key.u.string; json_skip_whitespace(); ch = input(); if (ch != ':') { json_set_error("expected ':' after key at offset %d", input_pos - 1); return -1; } ret = json_parse_value(value); if (ret <= 0) { json_set_error("invalid property value at offset %d", input_pos); return -1; } return 1; } static struct json_value *json_parse_internal(const char *json, size_t len) { struct json_value *root; int ret; input_buf = json; input_pos = 0; input_len = len; input_can_unput = false; nesting_depth = 0; json_error[0] = '\0'; root = calloc(1, sizeof(*root)); if (!root) { json_set_error("out of memory while allocating parse root"); return NULL; } ret = json_parse_value(root); if (ret != 1) { json_set_error("parse error near offset %d", input_pos); json_free(root); return NULL; } json_skip_whitespace(); if (input() != JSON_INPUT_EOF) { json_set_error("unexpected trailing token at offset %d", input_pos - 1); json_free(root); return NULL; } return root; } struct json_value *json_parse_buf(const char *json, size_t len) { return json_parse_internal(json, len); } struct json_value *json_get_child(struct json_value *object, const char *key) { struct json_value *it; if (!object || object->type != JSON_TYPE_OBJECT) return NULL; for (it = object->u.value; it; it = it->next) { if (!strcmp(it->key, key)) return it; } return NULL; } struct json_value *json_get_element_object(struct json_value *object, unsigned int idx) { struct json_value *it; unsigned int i; if (!object || object->type != JSON_TYPE_ARRAY) return NULL; for (it = object->u.value, i = 0; it; it = it->next, i++) { if (i == idx) return it; } return NULL; } const char *json_get_element_string(struct json_value *object, unsigned int idx) { struct json_value *element; if (!object) return NULL; element = json_get_element_object(object, idx); if (!element || element->type != JSON_TYPE_STRING) return NULL; return element->u.string; } int json_count_children(struct json_value *array) { struct json_value *it; int count = 0; if (!array || array->type != JSON_TYPE_ARRAY) return -1; for (it = array->u.value; it; it = it->next) count++; return count; } int json_get_number(struct json_value *object, const char *key, double *number) { struct json_value *it; if (!object || object->type != JSON_TYPE_OBJECT) return -1; for (it = object->u.value; it; it = it->next) { if (!strcmp(it->key, key)) { if (it->type != JSON_TYPE_NUMBER) return -1; *number = it->u.number; return 0; } } return -1; } const char *json_get_string(struct json_value *object, const char *key) { struct json_value *it; if (!object || object->type != JSON_TYPE_OBJECT) return NULL; for (it = object->u.value; it; it = it->next) { if (!strcmp(it->key, key)) { if (it->type != JSON_TYPE_STRING) return NULL; return it->u.string; } } return NULL; } void json_free(struct json_value *value) { struct json_value *stack; struct json_value *next; struct json_value *it; if (!value) return; /* Free iteratively to avoid recursion depth issues on deeply nested JSON. */ stack = value; stack->next = NULL; while (stack) { value = stack; stack = stack->next; if (value->type == JSON_TYPE_OBJECT || value->type == JSON_TYPE_ARRAY) { it = value->u.value; while (it) { next = it->next; it->next = stack; stack = it; it = next; } } free((char *)value->key); if (value->type == JSON_TYPE_STRING) free((char *)value->u.string); free(value); } } linux-msm-qdl-f540e59/json.h000066400000000000000000000056361521156022300157150ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. * Copyright (c) 2019, Linaro Ltd. * 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. */ #ifndef __JSON_H__ #define __JSON_H__ #include #define JSON_ERROR_SIZE 256 #define JSON_MAX_DEPTH 256 enum { JSON_TYPE_UNKNOWN, JSON_TYPE_TRUE, JSON_TYPE_FALSE, JSON_TYPE_NULL, JSON_TYPE_NUMBER, JSON_TYPE_STRING, JSON_TYPE_ARRAY, JSON_TYPE_OBJECT, }; struct json_value { const char *key; int type; union { double number; const char *string; struct json_value *value; } u; struct json_value *next; }; /* * Parser error message for the most recent json_parse_buf() call. * Empty string means no parse error was recorded. * This buffer is global and not thread-safe. */ extern char json_error[JSON_ERROR_SIZE]; struct json_value *json_parse_buf(const char *json, size_t len); int json_count_children(struct json_value *array); /* Duplicate object keys are resolved by first match in input order. */ struct json_value *json_get_child(struct json_value *object, const char *key); struct json_value *json_get_element_object(struct json_value *object, unsigned int idx); const char *json_get_element_string(struct json_value *object, unsigned int idx); int json_get_number(struct json_value *object, const char *key, double *number); const char *json_get_string(struct json_value *object, const char *key); void json_free(struct json_value *value); #endif linux-msm-qdl-f540e59/ks.c000066400000000000000000000060051521156022300153430ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause #include #include #include #include #include #include #include #include #include #include #include #include #include "qdl.h" #include "oscompat.h" #ifdef _WIN32 const char *__progname = "ks"; #endif static struct qdl_device qdl; bool qdl_debug; int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout __unused) { return read(qdl->fd, buf, len); } int qdl_write(struct qdl_device *qdl, const void *buf, size_t len, unsigned int timeout __unused) { return write(qdl->fd, buf, len); } static void print_usage(FILE *out) { extern const char *__progname; fprintf(out, "%s -p -s ...\n", __progname); fprintf(out, " -h --help Print this usage info\n" " -p --port Sahara device node to use\n" " -s --sahara Sahara protocol file mapping\n" "\n" "One -p instance is required. One or more -s instances are required.\n" "\n" "Example:\n" "ks -p /dev/mhi0_QAIC_SAHARA -s 1:/opt/qti-aic/firmware/fw1.bin -s 2:/opt/qti-aic/firmware/fw2.bin\n"); } int main(int argc, char **argv) { struct sahara_image mappings[MAPPING_SZ] = {}; const char *filename; bool found_mapping = false; char *dev_node = NULL; long file_id; char *colon; int opt; int ret; static struct option options[] = { {"debug", no_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {"port", required_argument, 0, 'p'}, {"sahara", required_argument, 0, 's'}, {0, 0, 0, 0} }; while ((opt = getopt_long(argc, argv, "dvp:s:h", options, NULL)) != -1) { switch (opt) { case 'd': qdl_debug = true; break; case 'v': print_version(); return 0; case 'p': dev_node = optarg; printf("Using port - %s\n", dev_node); break; case 's': found_mapping = true; file_id = strtol(optarg, NULL, 10); if (file_id < 0) { print_usage(stderr); return 1; } if (file_id >= MAPPING_SZ) { fprintf(stderr, "ID:%ld exceeds the max value of %d\n", file_id, MAPPING_SZ - 1); return 1; } colon = strchr(optarg, ':'); if (!colon) { print_usage(stderr); return 1; } filename = &optarg[colon - optarg + 1]; ret = load_sahara_image(NULL, filename, &mappings[file_id]); if (ret < 0) return 1; printf("Created mapping ID:%ld File:%s\n", file_id, filename); break; case 'h': print_usage(stdout); return 0; default: print_usage(stderr); return 1; } } // -p and -s is required if (!dev_node || !found_mapping) { print_usage(stderr); return 1; } if (qdl_debug) print_version(); qdl.fd = open(dev_node, O_RDWR); if (qdl.fd < 0) { fprintf(stderr, "Unable to open %s\n", dev_node); return 1; } ret = sahara_run(&qdl, mappings, NULL, NULL); if (ret < 0) return 1; return 0; } linux-msm-qdl-f540e59/list.h000066400000000000000000000041501521156022300157050ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) 2016, Linaro Ltd. */ #ifndef __LIST_H__ #define __LIST_H__ #include #include struct list_head { struct list_head *prev; struct list_head *next; }; #define LIST_INIT(list) { &(list), &(list) } static inline void list_init(struct list_head *list) { list->prev = list; list->next = list; } static inline bool list_empty(struct list_head *list) { return list->next == list; } static inline void list_prepend(struct list_head *list, struct list_head *item) { struct list_head *first = list->next; item->next = first; item->prev = list; list->next = item; first->prev = item; } static inline void list_append(struct list_head *list, struct list_head *item) { struct list_head *prev = list->prev; item->next = list; item->prev = prev; prev->next = item; list->prev = item; } static inline void list_del(struct list_head *item) { item->prev->next = item->next; item->next->prev = item->prev; } #define list_for_each(item, list) \ for (item = (list)->next; item != list; item = item->next) #define list_for_each_safe(item, tmp, list) \ for (item = (list)->next, tmp = item->next; item != list; item = tmp, tmp = item->next) #define list_entry(item, type, member) \ container_of(item, type, member) #define list_entry_first(list, type, member) \ container_of((list)->next, type, member) #define list_entry_next(item, member) \ container_of((item)->member.next, typeof(*(item)), member) #define list_for_each_entry(item, list, member) \ for (item = list_entry_first(list, typeof(*(item)), member); \ &item->member != list; \ item = list_entry_next(item, member)) #define list_for_each_entry_continue(item, list, member) \ for (item = list_entry_next(item, member); \ &item->member != list; \ item = list_entry_next(item, member)) #define list_for_each_entry_safe(item, next, list, member) \ for (item = list_entry_first(list, typeof(*(item)), member), \ next = list_entry_next(item, member); \ &item->member != list; \ item = next, \ next = list_entry_next(item, member)) \ #endif linux-msm-qdl-f540e59/meson.build000066400000000000000000000150301521156022300167220ustar00rootroot00000000000000project('qdl', 'c', default_options : ['warning_level=2', 'optimization=2'], license: 'BSD-3-Clause', meson_version: '>=1.1.0' ) # Determine version git_check_ver = run_command('git', 'describe', '--dirty', '--always', '--tags', check: false).stdout().strip() git_v = git_check_ver != '' ? git_check_ver : 'unknown-version' env_v = get_option('VERSION') ver = env_v != '' ? env_v : git_v # Dependencies libusb_dep = dependency('libusb-1.0') libxml_dep = dependency('libxml-2.0') libzip_dep = dependency('libzip') help2man = find_program('help2man', required: false) cmocka_dep = dependency('cmocka', required: get_option('tests')) # Common include dirs inc = include_directories('.') # Windows-specific link flags ws2_dep = [] setupapi_dep = [] if host_machine.system() == 'windows' ws2_dep = meson.get_compiler('c').find_library('ws2_32', required : false) # SetupAPI / cfgmgr32 are needed by the QUD backend to enumerate Qualcomm # COM ports exposed by the official QDLoader 9008 driver. setupapi_dep = [ meson.get_compiler('c').find_library('setupapi', required : false), meson.get_compiler('c').find_library('cfgmgr32', required : false), ] endif common_dep = [libusb_dep, libxml_dep, libzip_dep, ws2_dep, setupapi_dep] # Compile-only view of common_dep (includes + cflags, no link args). # Used on executables so their own sources see the headers, while link # args propagate exactly once through the static library below, avoiding # duplicate "-l..." entries on the link line. common_compile_dep = [] foreach d : [libusb_dep, libxml_dep, libzip_dep] common_compile_dep += d.partial_dependency(compile_args: true, includes: true) endforeach # --- version header generation --- conf = configuration_data() conf.set('version', ver) version_h = configure_file( input: 'version.h.in', output: 'version.h', configuration: conf ) common_sources = files( 'sahara.c', 'util.c', 'ux.c', 'oscompat.c', 'file.c' ) shared_lib = static_library('qdl_common', common_sources, dependencies: common_dep) # --- qdl executable --- qdl_sources = files( 'auto.c', 'qud.c', 'firehose.c', 'io.c', 'qdl.c', 'patch.c', 'program.c', 'read.c', 'sha2.c', 'sim.c', 'ufs.c', 'usb.c', 'vip.c', 'sparse.c', 'gpt.c', 'flashmap.c', 'json.c', 'contents.c', 'pathbuf.c', 'zipper.c', ) qdl_exe = executable('qdl', sources : qdl_sources + [version_h], link_with: shared_lib, dependencies : common_compile_dep, include_directories : inc, install : true ) # --- qdl-ramdump executable --- ramdump_sources = files( 'ramdump.c', 'auto.c', 'qud.c', 'io.c', 'sha2.c', 'sim.c', 'usb.c' ) qdl_ramdump_exe = executable('qdl-ramdump', sources : ramdump_sources + [version_h], link_with: shared_lib, dependencies : common_compile_dep, include_directories : inc, install : true ) # --- qdl-ks executable --- ks_sources = files('ks.c') qdl_ks_exe = executable('qdl-ks', sources : ks_sources + [version_h], link_with: shared_lib, dependencies : common_compile_dep, include_directories : inc, install : true ) # -- tests --- test( 'VIP table generation', find_program('bash'), args: [meson.current_source_dir() / 'tests/test_vip_generation.sh', '--builddir', meson.current_build_dir()], depends: [qdl_exe], suite: 'integration', ) test( 'Flashmap dry-run', find_program('bash'), args: [meson.current_source_dir() / 'tests/test_flashmap.sh', '--builddir', meson.current_build_dir()], depends: [qdl_exe], suite: 'integration', protocol: 'tap', ) if cmocka_dep.found() test_program_load_xml = executable('test_program_load_xml', sources : [ 'tests/test_program_load_xml.c', 'tests/common.c', 'pathbuf.c', 'program.c', 'util.c', version_h, ], dependencies : common_dep + [cmocka_dep], include_directories : inc, c_args : ['-Daccess=mock_access'], ) test( 'program_load_xml path resolution', test_program_load_xml, suite: 'unit', protocol: 'tap', env: ['CMOCKA_MESSAGE_OUTPUT=TAP'], ) test_pathbuf = executable('test_pathbuf', sources : [ 'tests/test_pathbuf.c', 'pathbuf.c', ], dependencies : common_dep + [cmocka_dep], include_directories : inc, ) test( 'pathbuf dirname behavior', test_pathbuf, suite: 'unit', protocol: 'tap', env: ['CMOCKA_MESSAGE_OUTPUT=TAP'], ) test_contents_selectors = executable('test_contents_selectors', sources : [ 'tests/test_contents_selectors.c', 'pathbuf.c', ], dependencies : common_dep + [cmocka_dep], include_directories : inc, ) test( 'contents selector resolution', test_contents_selectors, suite: 'unit', protocol: 'tap', env: ['CMOCKA_MESSAGE_OUTPUT=TAP'], ) test_contents_xml = executable('test_contents_xml', sources : [ 'tests/test_contents_xml.c', 'tests/common.c', 'pathbuf.c', ], dependencies : common_dep + [cmocka_dep], include_directories : inc, ) test( 'contents XML parsing', test_contents_xml, suite: 'unit', protocol: 'tap', env: ['CMOCKA_MESSAGE_OUTPUT=TAP'], ) else warning('cmocka not found; skipping unit tests') endif # --- checkpatch targets --- check_script = join_paths(meson.project_source_root(), 'scripts', 'checkpatch_wrapper.sh') foreach checkpatchparam : ['download-checkpatch', 'check', 'check-cached', 'check-range'] run_target(checkpatchparam, command: ['bash', '-lc', check_script + ' ' + checkpatchparam] ) endforeach # --- markdown lint target --- markdown_script = join_paths(meson.project_source_root(), 'scripts', 'markdownlint_wrapper.sh') run_target('markdown-lint', command: ['bash', '-lc', markdown_script + ' check'] ) # --- man generation --- if help2man.found() # Test if we can run the built executable (might fail in cross-compilation) can_run_exe = not meson.is_cross_build() if can_run_exe manpage_targets = [] foreach prog : [ ['qdl', qdl_exe, 'Qualcomm Download'], ['qdl-ramdump', qdl_ramdump_exe, 'Qualcomm Download Ramdump'], ['qdl-ks', qdl_ks_exe, 'Qualcomm KS'] ] manpage_targets += custom_target(prog[0] + '.1', input: prog[1], output: prog[0] + '.1', command: [help2man, '-N', '-n', prog[2], prog[1].full_path()], capture: true, install: true, install_dir: get_option('mandir') / 'man1') endforeach # Replicate the "manpages" phony target. alias_target requires at # least one dependency, so it is only declared when we have actually # produced manpage targets above. alias_target('manpages', manpage_targets) endif endif linux-msm-qdl-f540e59/meson.options000066400000000000000000000002601521156022300173150ustar00rootroot00000000000000option('VERSION', type: 'string', value: '', description: 'Project version') option('tests', type: 'feature', value: 'auto', description: 'Build unit tests (requires cmocka)') linux-msm-qdl-f540e59/oscompat.c000066400000000000000000000025201521156022300165510ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause #ifdef _WIN32 #include #include #include #include #include "oscompat.h" extern const char *__progname; void timeradd(const struct timeval *a, const struct timeval *b, struct timeval *result) { result->tv_sec = a->tv_sec + b->tv_sec; result->tv_usec = a->tv_usec + b->tv_usec; if (result->tv_usec >= 1000000) { result->tv_sec += 1; result->tv_usec -= 1000000; } } void err(int eval, const char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "%s: ", __progname); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": "); } fprintf(stderr, "%s\n", strerror(errno)); va_end(ap); exit(eval); } void errx(int eval, const char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "%s: ", __progname); if (fmt) vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); exit(eval); } void warn(const char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "%s: ", __progname); if (fmt) { vfprintf(stderr, fmt, ap); fprintf(stderr, ": "); } fprintf(stderr, "%s\n", strerror(errno)); va_end(ap); } void warnx(const char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "%s: ", __progname); if (fmt) vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); } #endif // _WIN32 linux-msm-qdl-f540e59/oscompat.h000066400000000000000000000017711521156022300165650ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __OSCOMPAT_H__ #define __OSCOMPAT_H__ #include #include #ifndef _WIN32 #include #define O_BINARY 0 #else // _WIN32 #include void timeradd(const struct timeval *a, const struct timeval *b, struct timeval *result); void err(int eval, const char *fmt, ...); void errx(int eval, const char *fmt, ...); void warn(const char *fmt, ...); void warnx(const char *fmt, ...); #endif /** * path_is_absolute() - check if a path is absolute * @path: path string to check * * On POSIX systems, a path starting with '/' is absolute. * On Windows, absolute paths are either drive-letter paths (e.g. "C:\...") * or UNC paths (e.g. "\\server\share"). * * Returns: true if @path is absolute, false otherwise */ static inline bool path_is_absolute(const char *path) { #ifndef _WIN32 return path[0] == '/'; #else return (isalpha(path[0]) && path[1] == ':') || (path[0] == '\\' && path[1] == '\\'); #endif } #endif linux-msm-qdl-f540e59/patch.c000066400000000000000000000040201521156022300160200ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2016-2017, Linaro Ltd. * All rights reserved. */ #include #include #include #include #include #include #include #include "patch.h" #include "firehose.h" #include "qdl.h" static bool patches_loaded; int patch_load_xml(struct list_head *ops, xmlDoc *doc, const char *patch_file) { struct firehose_op *patch; xmlNode *node; xmlNode *root; int errors; root = xmlDocGetRootElement(doc); for (node = root->children; node ; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (xmlStrcmp(node->name, (xmlChar *)"patch")) { ux_err("unrecognized tag \"%s\" in patch-type file, ignoring\n", node->name); continue; } errors = 0; patch = firehose_alloc_op(FIREHOSE_OP_PATCH); patch->sector_size = attr_as_unsigned(node, "SECTOR_SIZE_IN_BYTES", &errors); patch->byte_offset = attr_as_unsigned(node, "byte_offset", &errors); patch->filename = attr_as_string(node, "filename", &errors); patch->partition = attr_as_unsigned(node, "physical_partition_number", &errors); patch->size_in_bytes = attr_as_unsigned(node, "size_in_bytes", &errors); patch->start_sector = attr_as_string(node, "start_sector", &errors); patch->value = attr_as_string(node, "value", &errors); patch->what = attr_as_string(node, "what", &errors); if (errors) { ux_err("errors while parsing patch-type file \"%s\"\n", patch_file); free((void *)patch->filename); free((void *)patch->start_sector); free((void *)patch->value); free((void *)patch->what); free(patch); continue; } list_append(ops, &patch->node); } patches_loaded = true; return 0; } int patch_load(struct list_head *ops, const char *patch_file) { xmlDoc *doc; int ret; doc = xmlReadFile(patch_file, NULL, 0); if (!doc) { ux_err("failed to parse patch-type file \"%s\"\n", patch_file); return -EINVAL; } ret = patch_load_xml(ops, doc, patch_file); xmlFreeDoc(doc); return ret; } linux-msm-qdl-f540e59/patch.h000066400000000000000000000005021521156022300160260ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __PATCH_H__ #define __PATCH_H__ #include #include "list.h" struct qdl_device; struct firehose_op; int patch_load(struct list_head *ops, const char *patch_file); int patch_load_xml(struct list_head *ops, xmlDoc *doc, const char *patch_file); #endif linux-msm-qdl-f540e59/pathbuf.c000066400000000000000000000065311521156022300163630ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #include #include #include #include "oscompat.h" #include "pathbuf.h" static bool qdl_path_is_sep(char ch) { #ifdef _WIN32 return ch == '/' || ch == '\\'; #else return ch == '/'; #endif } static char *qdl_pathbuf_last_sep(char *path) { char *last = NULL; for (; *path; path++) { if (qdl_path_is_sep(*path)) last = path; } return last; } #ifdef _WIN32 static size_t qdl_pathbuf_unc_root_len(const char *path) { size_t i; size_t start; if (!qdl_path_is_sep(path[0]) || !qdl_path_is_sep(path[1])) return 0; i = 2; while (qdl_path_is_sep(path[i])) i++; if (!path[i]) return 2; start = i; while (path[i] && !qdl_path_is_sep(path[i])) i++; if (i == start) return 2; while (qdl_path_is_sep(path[i])) i++; if (!path[i]) return 2; start = i; while (path[i] && !qdl_path_is_sep(path[i])) i++; if (i == start) return 2; return i; } #endif void qdl_pathbuf_reset(struct pathbuf *path) { if (!path) return; path->buf[0] = '\0'; path->len = 0; } void qdl_pathbuf_dup(struct pathbuf *dst, const struct pathbuf *orig) { memcpy(dst, orig, sizeof(struct pathbuf)); } int qdl_pathbuf_push(struct pathbuf *path, const char *component) { size_t component_len; size_t path_len; size_t skip = 0; size_t need_sep = 0; if (!path || !component) return -EINVAL; if (component[0] == '\0') return 0; path_len = path->len; if (path_is_absolute(component)) path_len = 0; if (component[0] == '.' && qdl_path_is_sep(component[1])) skip += 2; if (path_len > 0) need_sep = !qdl_path_is_sep(path->buf[path_len - 1]); if (path_len > 0 && !need_sep) { while (qdl_path_is_sep(component[skip])) skip++; } component_len = strlen(component + skip); if (component_len == 0) return 0; if (path_len + need_sep + component_len + 1 > sizeof(path->buf)) return -ENAMETOOLONG; if (need_sep) path->buf[path_len++] = '/'; #ifndef _WIN32 memcpy(path->buf + path_len, component + skip, component_len + 1); #else for (size_t i = 0; i <= component_len; i++) path->buf[path_len + i] = qdl_path_is_sep(component[skip + i]) ? '/' : component[skip + i]; #endif path->len = path_len + component_len; return 0; } const char *qdl_pathbuf_str(const struct pathbuf *path) { return path ? path->buf : NULL; } void qdl_pathbuf_dirname(struct pathbuf *path) { size_t root_len = 0; char *sep; if (!path || !path->buf[0]) return; if (path->len == 0) path->len = strlen(path->buf); #ifdef _WIN32 if (isalpha((unsigned char)path->buf[0]) && path->buf[1] == ':' && qdl_path_is_sep(path->buf[2])) { root_len = 3; } else { root_len = qdl_pathbuf_unc_root_len(path->buf); if (root_len == 0 && qdl_path_is_sep(path->buf[0])) root_len = 1; } #else if (path->buf[0] == '/') root_len = 1; #endif while (path->len > root_len && qdl_path_is_sep(path->buf[path->len - 1])) { path->buf[--path->len] = '\0'; } sep = qdl_pathbuf_last_sep(path->buf); if (!sep) return; if ((size_t)(sep - path->buf) < root_len) { path->buf[root_len] = '\0'; path->len = root_len; return; } *sep = '\0'; path->len = (size_t)(sep - path->buf); } bool qdl_path_exists(const struct pathbuf *path) { struct stat st; if (!path || !path->buf[0]) return false; return stat(path->buf, &st) == 0; } linux-msm-qdl-f540e59/pathbuf.h000066400000000000000000000011701521156022300163620ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #ifndef __PATHBUF_H__ #define __PATHBUF_H__ #include #include #include struct pathbuf { char buf[PATH_MAX]; size_t len; }; void qdl_pathbuf_reset(struct pathbuf *path); void qdl_pathbuf_dup(struct pathbuf *dst, const struct pathbuf *orig); int qdl_pathbuf_push(struct pathbuf *path, const char *component); const char *qdl_pathbuf_str(const struct pathbuf *path); void qdl_pathbuf_dirname(struct pathbuf *path); bool qdl_path_exists(const struct pathbuf *path); #endif linux-msm-qdl-f540e59/program.c000066400000000000000000000327621521156022300164060ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2016-2017, Linaro Ltd. * All rights reserved. */ #include "contents.h" #include "pathbuf.h" #include #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include #include "program.h" #include "file.h" #include "qdl.h" #include "firehose.h" #include "sparse.h" #include "gpt.h" static int load_erase_tag(struct list_head *ops, xmlNode *node, bool is_nand) { struct firehose_op *program; int errors = 0; program = firehose_alloc_op(FIREHOSE_OP_ERASE); if (!program) return -1; program->is_nand = is_nand; program->sector_size = attr_as_unsigned(node, "SECTOR_SIZE_IN_BYTES", &errors); program->num_sectors = attr_as_unsigned(node, "num_partition_sectors", &errors); program->partition = attr_as_unsigned(node, "physical_partition_number", &errors); program->start_sector = attr_as_string(node, "start_sector", &errors); if (is_nand) { program->pages_per_block = attr_as_unsigned(node, "PAGES_PER_BLOCK", &errors); } if (errors) { ux_err("errors while parsing erase tag\n"); free((void *)program->start_sector); free(program); return -EINVAL; } if (!program->num_sectors) { ux_err("erase tag with num_sectors=0 not allowed\n"); free((void *)program->start_sector); free(program); return -EINVAL; } list_append(ops, &program->node); return 0; } static int program_load_sparse(struct list_head *ops, struct firehose_op *program, struct qdl_file *file) { struct firehose_op *program_sparse = NULL; char tmp[PATH_MAX]; sparse_header_t sparse_header; unsigned int start_sector; uint32_t sparse_fill_value; uint64_t chunk_size; off_t sparse_offset; int chunk_type; if (sparse_header_parse(file, &sparse_header)) { /* * If the XML tag "program" contains the attribute 'sparse="true"' * for a partition node but lacks a sparse header, * it will be validated against the defined partition size. * If the sizes match, it is likely that the 'sparse="true"' attribute * was set by mistake, fix the sparse flag and add the * program entry to the list. */ if ((off_t)program->sector_size * program->num_sectors == qdl_file_seek(file, 0, SEEK_END)) { program->sparse = false; list_append(ops, &program->node); return 0; } ux_err("[PROGRAM] Unable to parse sparse header at %s...failed\n", program->filename); return -1; } for (uint32_t i = 0; i < sparse_header.total_chunks; ++i) { chunk_type = sparse_chunk_header_parse(file, &sparse_header, &chunk_size, &sparse_fill_value, &sparse_offset); if (chunk_type < 0) { ux_err("[PROGRAM] Unable to parse sparse chunk %i at %s...failed\n", i, program->filename); return -1; } if (chunk_size == 0) continue; if (chunk_size % program->sector_size != 0) { ux_err("[SPARSE] File chunk #%u size %" PRIu64 " is not a sector-multiple\n", i, chunk_size); return -1; } if (chunk_size / program->sector_size >= UINT_MAX) { /* * Perhaps the programmer can handle larger "num_sectors"? * Let's cap it for now, it's big enough for now... */ ux_err("[SPARSE] File chunk #%u size %" PRIu64 " is too large\n", i, chunk_size); return -1; } if (chunk_type == CHUNK_TYPE_RAW || chunk_type == CHUNK_TYPE_FILL) { program_sparse = firehose_alloc_op(FIREHOSE_OP_PROGRAM); program_sparse->pages_per_block = program->pages_per_block; program_sparse->sector_size = program->sector_size; program_sparse->file_offset = program->file_offset; program_sparse->filename = strdup(program->filename); program_sparse->label = strdup(program->label); program_sparse->partition = program->partition; program_sparse->sparse = program->sparse; program_sparse->start_sector = strdup(program->start_sector); program_sparse->last_sector = program->last_sector; program_sparse->is_nand = program->is_nand; program_sparse->sparse_chunk_type = chunk_type; program_sparse->num_sectors = chunk_size / program->sector_size; if (chunk_type == CHUNK_TYPE_RAW) program_sparse->sparse_offset = sparse_offset; else program_sparse->sparse_fill_value = sparse_fill_value; list_append(ops, &program_sparse->node); } start_sector = (unsigned int)strtoul(program->start_sector, NULL, 0); start_sector += chunk_size / program->sector_size; sprintf(tmp, "%u", start_sector); free((void *)program->start_sector); program->start_sector = strdup(tmp); } return 0; } static int program_resolve_path(struct firehose_op *program, const char *program_file, struct contents_filter *contents_filter, const char *incdir) { char *program_file_copy; char candidate[PATH_MAX]; const char *filename = program->filename; const char *program_dir; struct pathbuf pathbuf = {}; char *resolved; size_t len; int ret; /* Don't attempt to resolve filenames in zip files */ if (program->zip) return 0; /* Attempt to look up the file in the contents database */ ret = contents_resolve_path(contents_filter, filename, &pathbuf); if (ret == 1) { resolved = strdup(qdl_pathbuf_str(&pathbuf)); if (!resolved) return -1; free((void *)program->filename); program->filename = resolved; return 0; } /* Look for the file in include directory */ if (incdir) { snprintf(candidate, sizeof(candidate), "%s/%s", incdir, filename); if (!access(candidate, F_OK)) { resolved = strdup(candidate); if (!resolved) return -ENOMEM; free((void *)program->filename); program->filename = resolved; return 0; } } /* Look for the file adjacent to the program XML */ len = strlen(program_file) + 1; program_file_copy = alloca(len); memcpy(program_file_copy, program_file, len); program_dir = dirname(program_file_copy); if (strcmp(program_dir, ".")) { ret = snprintf(candidate, sizeof(candidate), "%s/%s", program_dir, filename); if (ret < 0 || (size_t)ret >= sizeof(candidate)) return -ENAMETOOLONG; if (!access(candidate, F_OK)) { resolved = strdup(candidate); if (!resolved) return -ENOMEM; free((void *)program->filename); program->filename = resolved; } } return 0; } static int load_program_tag(struct list_head *ops, xmlNode *node, bool is_nand, bool allow_missing, struct qdl_zip *zip, const char *program_file, struct contents_filter *contents_filter, const char *incdir) { struct firehose_op *program; struct qdl_file file = {}; int errors = 0; int ret; program = firehose_alloc_op(FIREHOSE_OP_PROGRAM); if (!program) return -1; program->is_nand = is_nand; program->sector_size = attr_as_unsigned(node, "SECTOR_SIZE_IN_BYTES", &errors); program->zip = qdl_zip_get(zip); program->filename = attr_as_string(node, "filename", &errors); program->label = attr_as_string(node, "label", &errors); program->num_sectors = attr_as_unsigned(node, "num_partition_sectors", &errors); program->partition = attr_as_unsigned(node, "physical_partition_number", &errors); program->sparse = attr_as_bool(node, "sparse", &errors); program->start_sector = attr_as_string(node, "start_sector", &errors); if (is_nand) { program->pages_per_block = attr_as_unsigned(node, "PAGES_PER_BLOCK", &errors); if (xmlGetProp(node, (xmlChar *)"last_sector")) { program->last_sector = attr_as_unsigned(node, "last_sector", &errors); } } else { program->file_offset = attr_as_unsigned(node, "file_sector_offset", &errors); } if (errors) { ux_err("errors while parsing program tag\n"); goto err_free_op; } if (program->filename) { ret = program_resolve_path(program, program_file, contents_filter, incdir); if (ret < 0) goto err_free_op; ret = qdl_file_open(zip, program->filename, &file); if (ret < 0) { ux_info("unable to open %s", program->filename); if (!allow_missing) { ux_info("...failing\n"); goto err_free_op; } ux_info("...ignoring\n"); free((void *)program->filename); program->filename = NULL; } } if (program->filename && program->sparse) { ret = program_load_sparse(ops, program, &file); if (ret < 0) goto err_free_op; /* * Chunks were added to the program list, drop the filename of * the parent, to prevent this from being written to the device */ free((void *)program->filename); program->filename = NULL; } list_append(ops, &program->node); qdl_file_close(&file); return 0; err_free_op: qdl_file_close(&file); qdl_zip_put(program->zip); free((void *)program->filename); free((void *)program->label); free((void *)program->start_sector); free(program); return -1; } int program_load_xml(struct list_head *ops, xmlDoc *doc, struct qdl_zip *zip, const char *program_file, bool is_nand, bool allow_missing, struct contents_filter *contents_filter, const char *incdir) { xmlNode *node; xmlNode *root; int errors = 0; root = xmlDocGetRootElement(doc); for (node = root->children; node ; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (!xmlStrcmp(node->name, (xmlChar *)"erase")) errors = load_erase_tag(ops, node, is_nand); else if (!xmlStrcmp(node->name, (xmlChar *)"program")) errors = load_program_tag(ops, node, is_nand, allow_missing, zip, program_file, contents_filter, incdir); else { ux_err("unrecognized tag \"%s\" in program-type file \"%s\"\n", node->name, program_file); errors = -EINVAL; } if (errors) break; } return errors; } int program_load(struct list_head *ops, const char *program_file, bool is_nand, bool allow_missing, struct contents_filter *contents_filter, const char *incdir) { xmlDoc *doc; int errors; doc = xmlReadFile(program_file, NULL, 0); if (!doc) { ux_err("failed to parse program-type file \"%s\"\n", program_file); return -EINVAL; } errors = program_load_xml(ops, doc, NULL, program_file, is_nand, allow_missing, contents_filter, incdir); xmlFreeDoc(doc); return errors; } int erase_execute(struct qdl_device *qdl, struct firehose_op *op, int (*apply)(struct qdl_device *qdl, struct firehose_op *op)) { int ret; assert(op->type == FIREHOSE_OP_ERASE); ret = apply(qdl, op); if (ret) return ret; return 0; } static struct firehose_op *program_find_partition(struct list_head *ops, const char *partition) { struct firehose_op *program; const char *label; list_for_each_entry(program, ops, node) { if (program->type != FIREHOSE_OP_PROGRAM) continue; label = program->label; if (!label) continue; if (!strcmp(label, partition)) return program; } return NULL; } /** * program_find_bootable_partition() - find one bootable partition * * Returns partition number, or negative errno on failure. * * Scan program tags for a partition with the label "sbl1", "xbl" or "xbl_a" * and return the partition number for this. If more than one line matches * we're informing the caller so that they can warn the user about the * uncertainty of this logic. */ int program_find_bootable_partition(struct list_head *ops, bool *multiple_found) { struct firehose_op *program; int part = -ENOENT; *multiple_found = false; program = program_find_partition(ops, "xbl"); if (program) part = program->partition; program = program_find_partition(ops, "xbl_a"); if (program) { if (part != -ENOENT) *multiple_found = true; else part = program->partition; } program = program_find_partition(ops, "sbl1"); if (program) { if (part != -ENOENT) *multiple_found = true; else part = program->partition; } return part; } /** * program_is_sec_partition_flashed() - find if secdata partition is flashed * * Returns true if filename for secdata is set in program*.xml, * or false otherwise. */ int program_is_sec_partition_flashed(struct list_head *ops) { struct firehose_op *program; program = program_find_partition(ops, "secdata"); if (!program) return false; if (program->filename) return true; return false; } int program_cmd_add(struct list_head *ops, const char *address, const char *filename) { unsigned int start_sector; unsigned int num_sectors; struct firehose_op *program; char *gpt_partition; int partition; char buf[20]; int ret; ret = parse_storage_address(address, &partition, &start_sector, &num_sectors, &gpt_partition); if (ret < 0) return ret; program = firehose_alloc_op(FIREHOSE_OP_PROGRAM); if (!program) { ux_err("failed to allocate program command\n"); return -1; } program->sector_size = 0; program->file_offset = 0; program->filename = filename ? strdup(filename) : NULL; program->label = filename ? strdup(filename) : NULL; program->num_sectors = num_sectors; program->partition = partition; program->sparse = false; sprintf(buf, "%u", start_sector); program->start_sector = strdup(buf); program->last_sector = 0; program->is_nand = false; program->gpt_partition = gpt_partition; list_append(ops, &program->node); return 0; } int erase_cmd_add(struct list_head *ops, const char *address) { unsigned int start_sector; unsigned int num_sectors; struct firehose_op *program; char *gpt_partition; int partition; char buf[20]; int ret; ret = parse_storage_address(address, &partition, &start_sector, &num_sectors, &gpt_partition); if (ret < 0) return ret; program = firehose_alloc_op(FIREHOSE_OP_ERASE); if (!program) { ux_err("failed to allocate erase command\n"); return -1; } program->num_sectors = num_sectors; program->partition = partition; sprintf(buf, "%u", start_sector); program->start_sector = strdup(buf); program->gpt_partition = gpt_partition; list_append(ops, &program->node); return 0; } linux-msm-qdl-f540e59/program.h000066400000000000000000000020751521156022300164050ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __PROGRAM_H__ #define __PROGRAM_H__ #include #include #include #include #include "list.h" struct qdl_device; struct firehose_op; struct qdl_zip; struct contents_filter; int program_load(struct list_head *ops, const char *program_file, bool is_nand, bool allow_missing, struct contents_filter *contents_filter, const char *incdir); int program_load_xml(struct list_head *ops, xmlDoc *doc, struct qdl_zip *zip, const char *program_file, bool is_nand, bool allow_missing, struct contents_filter *contents_filter, const char *incdir); int erase_execute(struct qdl_device *qdl, struct firehose_op *op, int (*apply)(struct qdl_device *qdl, struct firehose_op *op)); int program_find_bootable_partition(struct list_head *ops, bool *multiple_found); int program_is_sec_partition_flashed(struct list_head *ops); int program_cmd_add(struct list_head *ops, const char *address, const char *filename); int erase_cmd_add(struct list_head *ops, const char *address); #endif linux-msm-qdl-f540e59/qdl.c000066400000000000000000000731351521156022300155160ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2016-2017, Linaro Ltd. * Copyright (c) 2018, The Linux Foundation. All rights reserved. * All rights reserved. */ #include #include #include #include #include #include #include #include #include #include #include #include "qdl.h" #include "contents.h" #include "file.h" #include "firehose.h" #include "flashmap.h" #include "patch.h" #include "pathbuf.h" #include "program.h" #include "ufs.h" #include "oscompat.h" #include "vip.h" #ifdef _WIN32 const char *__progname = "qdl"; #endif #define MAX_USBFS_BULK_SIZE (16 * 1024) enum { QDL_FILE_UNKNOWN, QDL_FILE_PATCH, QDL_FILE_PROGRAM, QDL_FILE_READ, QDL_FILE_UFS, QDL_FILE_CONTENTS, QDL_CMD_READ, QDL_CMD_WRITE, QDL_CMD_ERASE, QDL_CMD_FLASH, QDL_CMD_SHA256, }; bool qdl_debug; static int detect_type(const char *verb) { xmlNode *root; xmlDoc *doc; xmlNode *node; int type = QDL_FILE_UNKNOWN; if (!strcmp(verb, "read")) return QDL_CMD_READ; if (!strcmp(verb, "write")) return QDL_CMD_WRITE; if (!strcmp(verb, "erase")) return QDL_CMD_ERASE; if (!strcmp(verb, "flash")) return QDL_CMD_FLASH; if (!strcmp(verb, "sha256")) return QDL_CMD_SHA256; if (access(verb, F_OK)) { ux_err("%s is not a verb and not a XML file\n", verb); return -EINVAL; } doc = xmlReadFile(verb, NULL, 0); if (!doc) { ux_err("failed to parse XML file \"%s\"\n", verb); return -EINVAL; } root = xmlDocGetRootElement(doc); if (!xmlStrcmp(root->name, (xmlChar *)"patches")) { type = QDL_FILE_PATCH; } else if (!xmlStrcmp(root->name, (xmlChar *)"data")) { for (node = root->children; node ; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (!xmlStrcmp(node->name, (xmlChar *)"program")) { type = QDL_FILE_PROGRAM; break; } if (!xmlStrcmp(node->name, (xmlChar *)"read")) { type = QDL_FILE_READ; break; } if (!xmlStrcmp(node->name, (xmlChar *)"ufs")) { type = QDL_FILE_UFS; break; } } } else if (!xmlStrcmp(root->name, (xmlChar *)"contents")) { type = QDL_FILE_CONTENTS; } xmlFreeDoc(doc); return type; } /* * Parse a --backend= value into an enum. "auto" maps to the meta-backend * QDL_DEVICE_AUTO, which inside its open path runs a unified wait loop * over libusb and (on Windows) the QUD SetupAPI enumeration, binding * whichever first reaches an EDL device. Explicit "usb"/"qud" pin to a * single concrete transport and skip the meta layer entirely. * * QDL_DEVICE_SIM is intentionally not selectable via --backend; --dry-run / * --create-digests pick it implicitly. */ static int decode_backend(const char *name, enum QDL_DEVICE_TYPE *out) { if (!name || !strcmp(name, "auto")) { *out = QDL_DEVICE_AUTO; return 0; } if (!strcmp(name, "usb")) { *out = QDL_DEVICE_USB; return 0; } if (!strcmp(name, "qud")) { *out = QDL_DEVICE_QUD; return 0; } return -1; } #define CPIO_MAGIC "070701" struct cpio_newc_header { char c_magic[6]; /* "070701" */ char c_ino[8]; char c_mode[8]; char c_uid[8]; char c_gid[8]; char c_nlink[8]; char c_mtime[8]; char c_filesize[8]; char c_devmajor[8]; char c_devminor[8]; char c_rdevmajor[8]; char c_rdevminor[8]; char c_namesize[8]; char c_check[8]; }; static uint32_t parse_ascii_hex32(const char *s) { uint32_t x = 0; for (int i = 0; i < 8; i++) { if (!isxdigit(s[i])) err(1, "non-hex-digit found in archive header"); if (s[i] <= '9') x = (x << 4) | (s[i] - '0'); else x = (x << 4) | (10 + (s[i] | 32) - 'a'); } return x; } /** * decode_programmer_archive() - Attempt to decode a programmer CPIO archive * @blob: Loaded image to be decoded as archive * @images: List of Sahara images to populate * * The blob might be a CPIO archive containing Sahara images, in files with * names in the format ":". Load each such Sahara image into the * relevant spot in the @images array. * * The blob is always consumed (freed) on both success and error paths. * On error, any partially-populated @images entries are also freed. * * Returns: 0 if no archive was found, 1 if archive was decoded, -1 on error */ static int decode_programmer_archive(struct sahara_image *blob, struct sahara_image *images) { struct cpio_newc_header *hdr; size_t filesize; size_t namesize; char name[128]; char *save; char *tok; void *ptr = blob->ptr; void *end = blob->ptr + blob->len; long id; if (blob->len < sizeof(*hdr) || memcmp(ptr, CPIO_MAGIC, 6)) return 0; for (;;) { if (ptr + sizeof(*hdr) > end) { ux_err("programmer archive is truncated\n"); goto err; } hdr = ptr; if (memcmp(hdr->c_magic, "070701", 6)) { ux_err("expected cpio header in programmer archive\n"); goto err; } filesize = parse_ascii_hex32(hdr->c_filesize); namesize = parse_ascii_hex32(hdr->c_namesize); ptr += sizeof(*hdr); if (ptr + namesize > end || ptr + filesize + namesize > end) { ux_err("programmer archive is truncated\n"); goto err; } if (namesize > sizeof(name)) { ux_err("unexpected filename length in programmer archive\n"); goto err; } memcpy(name, ptr, namesize); if (!memcmp(name, "TRAILER!!!", 11)) break; tok = strtok_r(name, ":", &save); if (!tok) { ux_err("missing image id in programmer archive entry\n"); goto err; } id = strtoul(tok, NULL, 0); if (id == 0 || id >= MAPPING_SZ) { ux_err("invalid image id \"%s\" in programmer archive\n", tok); goto err; } ptr += namesize; ptr = ALIGN_UP(ptr, 4); tok = strtok_r(NULL, ":", &save); if (tok) images[id].name = strdup(tok); images[id].len = filesize; images[id].ptr = malloc(filesize); memcpy(images[id].ptr, ptr, filesize); ptr += filesize; ptr = ALIGN_UP(ptr, 4); } free(blob->ptr); blob->ptr = NULL; blob->len = 0; return 1; err: sahara_images_free(images, MAPPING_SZ); free(blob->ptr); blob->ptr = NULL; blob->len = 0; return -1; } /** * decode_sahara_config() - Attempt to decode a Sahara config XML document * @blob: Loaded image to be decoded as Sahara config * @images: List of Sahara images, with @images[0] populated * * The single blob provided in @images[0] might be a XML blob containing * a sahara_config document with definitions of the various Sahara images that * will be loaded. Attempt to parse this and if possible load each referenced * Sahara image into the @images array. * * The original blob (in @images[0]) is freed once it has been consumed. * * Returns: 0 if no archive was found, 1 if archive was decoded, -1 on error */ int decode_sahara_config(struct sahara_image *blob, struct sahara_image *images, struct contents_filter *contents_filter) { char image_path_full[PATH_MAX]; struct pathbuf image_full_path = {}; const char *image_path; unsigned int image_id; size_t image_path_len; xmlNode *images_node; xmlNode *image_node; char *blob_name_buf; size_t base_path_len; char *base_path; xmlNode *root; xmlDoc *doc; int errors = 0; int ret; if (blob->len < 5 || memcmp(blob->ptr, "ptr, blob->len, blob->name, NULL, 0); if (!doc) { ux_err("failed to parse sahara_config in \"%s\"\n", blob->name); return -1; } blob_name_buf = strdup(blob->name); base_path = dirname(blob_name_buf); base_path_len = strlen(base_path); root = xmlDocGetRootElement(doc); if (xmlStrcmp(root->name, (xmlChar *)"sahara_config")) { ux_err("specified sahara_config \"%s\" is not a Sahara config\n", blob->name); goto err_free_doc; } for (images_node = root->children; images_node; images_node = images_node->next) { if (images_node->type == XML_ELEMENT_NODE && !xmlStrcmp(images_node->name, (xmlChar *)"images")) break; } if (!images_node) { ux_err("no images definitions found in sahara_config \"%s\"\n", blob->name); goto err_free_doc; } for (image_node = images_node->children; image_node; image_node = image_node->next) { if (image_node->type != XML_ELEMENT_NODE || xmlStrcmp(image_node->name, (xmlChar *)"image")) continue; image_id = attr_as_unsigned(image_node, "image_id", &errors); image_path = attr_as_string(image_node, "image_path", &errors); if (image_id == 0 || image_id >= MAPPING_SZ || errors) { ux_err("invalid sahara_config image in \"%s\"\n", blob->name); free((void *)image_path); goto err_free_doc; } image_path_len = strlen(image_path); if (contents_resolve_path(contents_filter, image_path, &image_full_path) == 1) { memcpy(image_path_full, image_full_path.buf, image_full_path.len + 1); } else if (path_is_absolute(image_path)) { if (image_path_len + 1 > PATH_MAX) { free((void *)image_path); goto err_free_doc; } memcpy(image_path_full, image_path, image_path_len + 1); } else { if (base_path_len + 1 + image_path_len + 1 > PATH_MAX) { free((void *)image_path); goto err_free_doc; } memcpy(image_path_full, base_path, base_path_len); image_path_full[base_path_len] = '/'; memcpy(image_path_full + base_path_len + 1, image_path, image_path_len); image_path_full[base_path_len + 1 + image_path_len] = '\0'; } free((void *)image_path); ret = load_sahara_image(NULL, image_path_full, &images[image_id]); if (ret < 0) goto err_free_doc; } xmlFreeDoc(doc); free(blob_name_buf); free(blob->ptr); blob->ptr = NULL; blob->len = 0; return 1; err_free_doc: sahara_images_free(images, MAPPING_SZ); free(blob->ptr); blob->ptr = NULL; blob->len = 0; xmlFreeDoc(doc); free(blob_name_buf); return -1; } /** * decode_programmer() - decodes the programmer specifier * @s: programmer specifier, from the user * @images: array of images to populate * * This parses the programmer specifier @s, which can either be a single * filename, or a comma-separated series of : entries. * * In the first case an attempt will be made to decode the Sahara archive and * each programmer part will be loaded into their requested @images entry. If * the file isn't an archive @images[SAHARA_ID_EHOSTDL_IMG] is assigned. In the * second case, each comma-separated entry will be split on ':' and the given * will be assigned to the @image entry indicated by the given . * * Memory is not allocated for the various strings, instead @s will be modified * by the tokenizer and pointers to the individual parts will be stored in the * @images array. * * Returns: 0 on success, -1 otherwise. */ static int decode_programmer(char *s, struct sahara_image *images) { struct sahara_image archive; char *filename; char *save1; char *pair; char *tail; long id; int ret; strtoul(s, &tail, 0); if (tail != s && tail[0] == ':') { for (pair = strtok_r(s, ",", &save1); pair; pair = strtok_r(NULL, ",", &save1)) { id = strtoul(pair, &tail, 0); if (tail == pair) { ux_err("invalid programmer specifier\n"); return -1; } if (id == 0 || id >= MAPPING_SZ) { ux_err("invalid image id \"%s\"\n", pair); return -1; } filename = &tail[1]; ret = load_sahara_image(NULL, filename, &images[id]); if (ret < 0) return -1; } } else { ret = load_sahara_image(NULL, s, &archive); if (ret < 0) return -1; ret = decode_programmer_archive(&archive, images); if (ret < 0 || ret == 1) return ret; ret = decode_sahara_config(&archive, images, NULL); if (ret < 0 || ret == 1) return ret; images[SAHARA_ID_EHOSTDL_IMG] = archive; } return 0; } static void print_usage(FILE *out) { extern const char *__progname; fprintf(out, "Usage: %s [options] ( | | )...\n", __progname); fprintf(out, " %s [options] ((read | write)
)...\n", __progname); fprintf(out, " %s [options] (erase
)...\n", __progname); fprintf(out, " %s [options] (sha256
)...\n", __progname); fprintf(out, " %s list\n", __progname); fprintf(out, " %s ramdump [--debug] [-o ] [,...]\n", __progname); fprintf(out, " %s flash ([::specifier] | [::])\n", __progname); fprintf(out, " %s create-zip [::]\n", __progname); fprintf(out, " -d, --debug\t\t\tPrint detailed debug info\n"); fprintf(out, " -v, --version\t\t\tPrint the current version and exit\n"); fprintf(out, " -n, --dry-run\t\t\tDry run execution, no device reading or flashing\n"); fprintf(out, " -f, --allow-missing\t\tAllow skipping of missing files during flashing\n"); fprintf(out, " -s, --storage=T\t\tSet target storage type T: \n"); fprintf(out, " -l, --finalize-provisioning\tProvision the target storage\n"); fprintf(out, " -i, --include=T\t\tSet an optional folder T to search for files\n"); fprintf(out, " -S, --serial=T\t\t\tSelect target by serial number T (e.g. <0AA94EFD>)\n"); fprintf(out, " -u, --out-chunk-size=T\t\tOverride chunk size for transaction with T\n"); fprintf(out, " -t, --create-digests=T\t\tGenerate table of digests in the T folder\n"); fprintf(out, " -T, --slot=T\t\t\tSet slot number T for multiple storage devices\n"); fprintf(out, " -D, --vip-table-path=T\t\tUse digest tables in the T folder for VIP\n"); fprintf(out, " -R, --skip-reset\t\tDo not send the reset command after flashing completes\n"); fprintf(out, " --backend=B\t\tSelect device backend B: (default: auto)\n"); fprintf(out, " --skipblock=M\t\tUse readback mechanism M to skip entries already on flash;\n"); fprintf(out, " \t\tM: (default: none)\n"); fprintf(out, " -h, --help\t\t\tPrint this usage info\n"); fprintf(out, " \t\txml file containing or directives\n"); fprintf(out, " \t\txml file containing directives\n"); fprintf(out, " \t\txml file containing directives\n"); fprintf(out, "
\t\tdisk address specifier, can be one of

,

,

, , or\n"); fprintf(out, " \t\t

, to specify a physical partition number P, a starting sector\n"); fprintf(out, " \t\tnumber S, the number of sectors to follow L, or partition by \"name\"\n"); fprintf(out, " \t\tpath where ramdump should stored\n"); fprintf(out, " \toptional glob-pattern to select which segments to ramdump\n"); fprintf(out, " \tflashmap JSON file, or ZIP archive with flashmap.json\n"); fprintf(out, " \tcontents XML file\n"); fprintf(out, " \tcomma-separated list of specifiers, such as storage type and flavors\n"); fprintf(out, "\n"); fprintf(out, "Example: %s prog_firehose_ddr.elf rawprogram*.xml patch*.xml\n", __progname); fprintf(out, " %s flash contents.xml::ufs,spinor/safe_rtos\n", __progname); } static int qdl_list(FILE *out) { struct qdl_device_desc *usb_devices; struct qud_device_desc *qud_devices; unsigned int usb_count = 0; unsigned int qud_count = 0; unsigned int i; usb_devices = usb_list(&usb_count); qud_devices = qud_list(&qud_count); if (usb_count == 0 && qud_count == 0) { fprintf(out, "No devices found\n"); } else { for (i = 0; i < usb_count; i++) fprintf(out, "%04x:%04x\t%s\n", usb_devices[i].vid, usb_devices[i].pid, usb_devices[i].serial); for (i = 0; i < qud_count; i++) fprintf(out, "05c6:%04x\t%s\t%s\n", qud_devices[i].pid, qud_devices[i].serial, qud_devices[i].path); } free(usb_devices); free(qud_devices); return 0; } /* Long-only option ids, distinct from any short option character. */ enum { OPT_BACKEND = 0x100, OPT_SKIPBLOCK, }; static int qdl_ramdump(int argc, char **argv) { struct qdl_device *qdl; char *ramdump_path = "."; char *filter = NULL; char *serial = NULL; enum QDL_DEVICE_TYPE qdl_dev_type = QDL_DEVICE_AUTO; int ret = 0; int opt; static struct option options[] = { {"debug", no_argument, 0, 'd'}, {"version", no_argument, 0, 'v'}, {"output", required_argument, 0, 'o'}, {"serial", required_argument, 0, 'S'}, {"backend", required_argument, 0, OPT_BACKEND}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while ((opt = getopt_long(argc, argv, "dvo:S:h", options, NULL)) != -1) { switch (opt) { case 'd': qdl_debug = true; break; case 'v': print_version(); return 0; case 'o': ramdump_path = optarg; break; case 'S': serial = optarg; break; case OPT_BACKEND: if (decode_backend(optarg, &qdl_dev_type) < 0) errx(1, "unknown backend \"%s\" (expected auto|usb|qud)", optarg); break; case 'h': print_usage(stdout); return 0; default: print_usage(stderr); return 1; } } if (optind < argc) filter = argv[optind++]; if (optind != argc) { print_usage(stderr); return 1; } ux_init(); qdl = qdl_init(qdl_dev_type); if (!qdl) { ux_err("backend not available\n"); return 1; } if (qdl_debug) print_version(); ret = qdl_open(qdl, serial); if (ret) { ret = 1; goto out_cleanup; } ret = sahara_run(qdl, NULL, ramdump_path, filter); if (ret < 0) { ret = 1; goto out_cleanup; } out_cleanup: qdl_close(qdl); qdl_deinit(qdl); return ret; } static int qdl_ensure_configured(struct list_head *ops, enum qdl_storage_type storage_type) { struct firehose_op *op; if (list_empty(ops)) return 0; op = list_entry_first(ops, struct firehose_op, node); if (op->type == FIREHOSE_OP_CONFIGURE) return 0; op = firehose_alloc_op(FIREHOSE_OP_CONFIGURE); if (!op) return -1; op->storage_type = storage_type; list_prepend(ops, &op->node); return 0; } static char *qdl_split_specifier(const char *param, char **specifier) { char *filename; char *tmp; if (!param || !param[0]) return NULL; filename = strdup(param); if (!filename) { ux_err("internal error: unable to allocate memory for argument\n"); return NULL; } *specifier = NULL; tmp = strstr(filename, "::"); if (tmp) { if (strstr(tmp + 2, "::")) { free(filename); return NULL; } *tmp = '\0'; if (!filename[0] || !tmp[2]) { free(filename); return NULL; } *specifier = tmp + 2; } return filename; } static int qdl_cmd_flash(struct list_head *firehose_ops, const char *arg, const char *incdir, struct sahara_image *images) { struct qdl_file flashmap; struct qdl_zip *zip = NULL; const char *dot; char *specifier; char *filename; char *tmp; char *base; int file_type = QDL_FILE_UNKNOWN; int ret; filename = qdl_split_specifier(arg, &specifier); if (!filename) { ux_err("failed to parse flash argument \"%s\" (expected or ::)\n", arg); return -1; } tmp = strdup(filename); if (!tmp) return -1; base = basename(tmp); dot = strrchr(base, '.'); if (dot && !strcmp(dot, ".xml")) { file_type = QDL_FILE_CONTENTS; } else if (dot && !strcmp(dot, ".json")) { file_type = QDL_CMD_FLASH; } else { ret = qdl_zip_open(filename, &zip); if (!ret) { ret = qdl_file_open(zip, "flashmap.json", &flashmap); if (!ret) { qdl_file_close(&flashmap); file_type = QDL_CMD_FLASH; } qdl_zip_put(zip); } } free(tmp); switch (file_type) { case QDL_FILE_CONTENTS: ret = contents_load(firehose_ops, filename, specifier, images, incdir); break; case QDL_CMD_FLASH: ret = flashmap_load(firehose_ops, filename, specifier, images, incdir); break; default: ux_err("flash input must be contents.xml, flashmap.json, or a zip containing flashmap.json\n"); ret = -1; break; } free(filename); return ret; } static int qdl_create_zip(int argc, char **argv) { struct sahara_image images[MAPPING_SZ] = {}; struct list_head ops = LIST_INIT(ops); const char *zipfile = argv[1]; char *specifier; char *filename; int ret; if (argc != 3) { print_usage(stderr); return 1; } ux_init(); filename = qdl_split_specifier(argv[2], &specifier); if (!filename) { ux_err("failed to parse flash argument"); return 1; } ret = contents_load(&ops, filename, specifier, images, NULL); if (ret < 0) goto out_free_filename; ret = zipper_write(zipfile, &ops, images); sahara_images_free(images, MAPPING_SZ); firehose_free_ops(&ops); out_free_filename: free(filename); return ret ? 1 : 0; } static int qdl_determine_bootable(struct list_head *ops) { struct firehose_op *op; bool multiple; int bootable; bootable = program_find_bootable_partition(ops, &multiple); if (bootable < 0) { ux_debug("no boot partition found\n"); return 0; } if (multiple) ux_info("Multiple candidates for primary bootloader found, using partition %d\n", bootable); op = firehose_alloc_op(FIREHOSE_OP_SET_BOOTABLE); if (!op) return -1; op->partition = bootable; list_append(ops, &op->node); return 0; } /* * Walk the firehose op list and emit one hex line per * FIREHOSE_OP_GET_SHA256_DIGEST entry. firehose_run() fills op->digest; * formatting and printing live here so firehose.c stays out of the * user-facing output policy. * * If the request shipped but the device returned no digest * (digest_valid stayed false), surface that to the user instead of * silently skipping the region. */ static void print_sha256_results(struct list_head *ops) { struct firehose_op *op; list_for_each_entry(op, ops, node) { char hex[SHA256_DIGEST_STRING_LENGTH]; size_t i; if (op->type != FIREHOSE_OP_GET_SHA256_DIGEST) continue; if (!op->digest_valid) { ux_err("no sha256 digest returned for %s+0x%x\n", op->start_sector, op->num_sectors); continue; } for (i = 0; i < SHA256_DIGEST_LENGTH; i++) snprintf(hex + i * 2, 3, "%02x", op->digest[i]); hex[SHA256_DIGEST_STRING_LENGTH - 1] = '\0'; printf("%s\n", hex); fflush(stdout); } } static int qdl_flash(int argc, char **argv) { enum qdl_storage_type storage_type = QDL_STORAGE_UFS; struct sahara_image sahara_images[MAPPING_SZ] = {}; struct list_head firehose_ops = LIST_INIT(firehose_ops); char *incdir = NULL; char *serial = NULL; const char *vip_generate_dir = NULL; const char *vip_table_path = NULL; int type; int ret; int opt; bool qdl_finalize_provisioning = false; bool allow_fusing = false; bool allow_missing = false; bool skip_reset = false; long out_chunk_size = 0; unsigned int slot = UINT_MAX; struct qdl_device *qdl = NULL; enum QDL_DEVICE_TYPE qdl_dev_type = QDL_DEVICE_AUTO; enum qdl_skipblock_mode skipblock_mode = QDL_SKIPBLOCK_NONE; static struct option options[] = { {"debug", no_argument, 0, 'd'}, {"version", no_argument, 0, 'v'}, {"include", required_argument, 0, 'i'}, {"finalize-provisioning", no_argument, 0, 'l'}, {"out-chunk-size", required_argument, 0, 'u' }, {"serial", required_argument, 0, 'S'}, {"vip-table-path", required_argument, 0, 'D'}, {"storage", required_argument, 0, 's'}, {"allow-missing", no_argument, 0, 'f'}, {"allow-fusing", no_argument, 0, 'c'}, {"dry-run", no_argument, 0, 'n'}, {"create-digests", required_argument, 0, 't'}, {"slot", required_argument, 0, 'T'}, {"skip-reset", no_argument, 0, 'R'}, {"backend", required_argument, 0, OPT_BACKEND}, {"skipblock", required_argument, 0, OPT_SKIPBLOCK}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while ((opt = getopt_long(argc, argv, "dvi:lu:S:D:s:fcnt:T:Rh", options, NULL)) != -1) { switch (opt) { case 'd': qdl_debug = true; break; case 'n': qdl_dev_type = QDL_DEVICE_SIM; break; case 't': vip_generate_dir = optarg; /* we also enforce dry-run mode */ qdl_dev_type = QDL_DEVICE_SIM; break; case 'v': print_version(); return 0; case 'f': allow_missing = true; break; case 'i': incdir = optarg; break; case 'l': qdl_finalize_provisioning = true; break; case 'c': allow_fusing = true; break; case 'u': out_chunk_size = strtol(optarg, NULL, 10); break; case 's': storage_type = decode_storage_type(optarg); if (storage_type == QDL_STORAGE_UNKNOWN) errx(1, "unknown storage type \"%s\"", optarg); break; case 'S': serial = optarg; break; case 'D': vip_table_path = optarg; break; case 'T': slot = (unsigned int)strtoul(optarg, NULL, 10); break; case 'R': skip_reset = true; break; case OPT_BACKEND: /* * --dry-run / --create-digests already pinned the backend to * QDL_DEVICE_SIM; honour that and ignore --backend in that case. */ if (qdl_dev_type != QDL_DEVICE_SIM && decode_backend(optarg, &qdl_dev_type) < 0) errx(1, "unknown backend \"%s\" (expected auto|usb|qud)", optarg); break; case OPT_SKIPBLOCK: if (!strcmp(optarg, "none")) skipblock_mode = QDL_SKIPBLOCK_NONE; else if (!strcmp(optarg, "sha256")) skipblock_mode = QDL_SKIPBLOCK_SHA256; else errx(1, "unknown --skipblock mode \"%s\", valid options are none and sha256", optarg); break; case 'h': print_usage(stdout); return 0; default: print_usage(stderr); return 1; } } /* at least 2 non optional args required */ if ((optind + 2) > argc) { print_usage(stderr); return 1; } qdl = qdl_init(qdl_dev_type); if (!qdl) { ret = -1; goto out_cleanup; } qdl->slot = slot; qdl->skipblock_mode = skipblock_mode; if (vip_table_path) { if (vip_generate_dir) errx(1, "VIP mode and VIP table generation can't be enabled together\n"); ret = vip_transfer_init(qdl, vip_table_path); if (ret) errx(1, "VIP initialization failed\n"); } if (out_chunk_size) qdl_set_out_chunk_size(qdl, out_chunk_size); if (vip_generate_dir) { ret = vip_gen_init(qdl, vip_generate_dir); if (ret) goto out_cleanup; } ux_init(); if (qdl_debug) print_version(); /* * The programmer needs to either be selected explicitly or through the * "flash" subcommand. Handling of "flash" happens in the loop below. */ if (strcmp(argv[optind], "flash")) { ret = decode_programmer(argv[optind++], sahara_images); if (ret < 0) goto out_cleanup; } do { type = detect_type(argv[optind]); if (type < 0 || type == QDL_FILE_UNKNOWN) errx(1, "failed to detect file type of %s\n", argv[optind]); switch (type) { case QDL_FILE_PATCH: ret = patch_load(&firehose_ops, argv[optind]); if (ret < 0) errx(1, "patch_load %s failed", argv[optind]); break; case QDL_FILE_PROGRAM: ret = program_load(&firehose_ops, argv[optind], storage_type == QDL_STORAGE_NAND, allow_missing, NULL, incdir); if (ret < 0) errx(1, "program_load %s failed", argv[optind]); if (!allow_fusing && program_is_sec_partition_flashed(&firehose_ops)) errx(1, "secdata partition to be programmed, which can lead to irreversible" " changes. Allow explicitly with --allow-fusing parameter"); break; case QDL_FILE_READ: ret = read_op_load(&firehose_ops, argv[optind], incdir); if (ret < 0) errx(1, "read_op_load %s failed", argv[optind]); break; case QDL_FILE_UFS: if (storage_type != QDL_STORAGE_UFS) errx(1, "attempting to load provisioning config when storage isn't \"ufs\""); ret = ufs_load(argv[optind], qdl_finalize_provisioning); if (ret < 0) errx(1, "ufs_load %s failed", argv[optind]); break; case QDL_CMD_READ: if (optind + 2 >= argc) errx(1, "read command missing arguments"); ret = read_cmd_add(&firehose_ops, argv[optind + 1], argv[optind + 2]); if (ret < 0) errx(1, "failed to add read command"); optind += 2; break; case QDL_CMD_WRITE: if (optind + 2 >= argc) errx(1, "write command missing arguments"); ret = program_cmd_add(&firehose_ops, argv[optind + 1], argv[optind + 2]); if (ret < 0) errx(1, "failed to add write command"); optind += 2; break; case QDL_CMD_ERASE: if (optind + 1 >= argc) errx(1, "erase command missing address"); ret = erase_cmd_add(&firehose_ops, argv[optind + 1]); if (ret < 0) errx(1, "failed to add erase command"); optind += 1; break; case QDL_CMD_SHA256: if (optind + 1 >= argc) errx(1, "sha256 command missing address"); ret = sha256_cmd_add(&firehose_ops, argv[optind + 1]); if (ret < 0) errx(1, "failed to add sha256 command"); optind += 1; break; case QDL_CMD_FLASH: if (optind + 1 >= argc) errx(1, "flash command missing operands"); ret = qdl_cmd_flash(&firehose_ops, argv[optind + 1], incdir, sahara_images); if (ret < 0) goto out_cleanup; optind += 1; break; default: errx(1, "%s type not yet supported", argv[optind]); break; } } while (++optind < argc); ret = qdl_ensure_configured(&firehose_ops, storage_type); if (ret < 0) goto out_cleanup; ret = qdl_determine_bootable(&firehose_ops); if (ret) goto out_cleanup; /* * Reset is the last operation in any flashing run, modelled as a regular * firehose op so callers can compose it like any other. Skip the append * to leave the programmer alive across qdl invocations. */ if (!skip_reset) { struct firehose_op *reset_op = firehose_alloc_op(FIREHOSE_OP_RESET); if (!reset_op) { ret = -1; goto out_cleanup; } list_append(&firehose_ops, &reset_op->node); } ret = qdl_open(qdl, serial); if (ret) goto out_cleanup; ret = sahara_run(qdl, sahara_images, NULL, NULL); if (ret < 0) goto out_cleanup; if (ufs_need_provisioning()) ret = firehose_provision(qdl, skip_reset); else ret = firehose_run(qdl, &firehose_ops); if (ret < 0) goto out_cleanup; print_sha256_results(&firehose_ops); out_cleanup: if (qdl) { if (vip_generate_dir) vip_gen_finalize(qdl); qdl_close(qdl); } sahara_images_free(sahara_images, MAPPING_SZ); firehose_free_ops(&firehose_ops); if (qdl) { if (qdl->vip_data.state != VIP_DISABLED) vip_transfer_deinit(qdl); qdl_deinit(qdl); } return !!ret; } int main(int argc, char **argv) { int i; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "list")) return qdl_list(stdout); if (!strcmp(argv[i], "ramdump")) return qdl_ramdump(argc - i, argv + i); if (!strcmp(argv[i], "create-zip")) return qdl_create_zip(argc - i, argv + i); if (argv[i][0] != '-') break; } return qdl_flash(argc, argv); } linux-msm-qdl-f540e59/qdl.h000066400000000000000000000145251521156022300155210ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __QDL_H__ #define __QDL_H__ #ifdef _WIN32 #include #else #include #endif #include #include "patch.h" #include "program.h" #include "read.h" #include #include "vip.h" #define container_of(ptr, typecast, member) ({ \ void *_ptr = (void *)(ptr); \ ((typeof(typecast) *)(_ptr - offsetof(typecast, member))); }) #define MIN(x, y) ({ \ __typeof__(x) _x = (x); \ __typeof__(y) _y = (y); \ _x < _y ? _x : _y; \ }) #define ROUND_UP(x, a) ({ \ __typeof__(x) _x = (x); \ __typeof__(a) _a = (a); \ (_x + _a - 1) & ~(_a - 1); \ }) #define __unused __attribute__((__unused__)) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ALIGN_UP(p, size) ({ \ __typeof__(size) _mask = (size) - 1; \ (__typeof__(p))(((uintptr_t)(p) + _mask) & ~_mask); \ }) #define MAPPING_SZ 128 #define SAHARA_ID_EHOSTDL_IMG 13 enum QDL_DEVICE_TYPE { QDL_DEVICE_USB, QDL_DEVICE_SIM, QDL_DEVICE_QUD, /* * Meta-backend: defers transport selection to the wait loop inside * auto_open(), which polls libusb and (on Windows) the QUD SetupAPI * enumeration each tick and binds whichever first reaches an EDL * device. Resolves the UX hazard of an upfront probe timeout where * the user plugs in the cable just after the grace window expires. */ QDL_DEVICE_AUTO, }; enum qdl_storage_type { QDL_STORAGE_UNKNOWN, QDL_STORAGE_EMMC, QDL_STORAGE_NAND, QDL_STORAGE_UFS, QDL_STORAGE_NVME, QDL_STORAGE_SPINOR, }; enum qdl_skipblock_mode { QDL_SKIPBLOCK_NONE, QDL_SKIPBLOCK_SHA256, }; struct qdl_device { enum QDL_DEVICE_TYPE dev_type; int fd; size_t max_payload_size; size_t sector_size; enum qdl_storage_type current_storage_type; enum qdl_skipblock_mode skipblock_mode; unsigned int slot; 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, unsigned int timeout); void (*close)(struct qdl_device *qdl); void (*set_out_chunk_size)(struct qdl_device *qdl, long size); void (*set_vip_transfer)(struct qdl_device *qdl, const char *signed_table, const char *chained_table); struct vip_transfer_data vip_data; /* * Pushback buffer for stream-oriented transports (Windows COM via the * QDLoader driver, virtio-console, ...). When a single read crosses a * Firehose message boundary - typically because the binary payload of * a rawmode response trails the XML envelope in the same read - the * leftover bytes are stashed here and qdl_read() returns them before * pulling more data from the transport. */ char *pending_buf; size_t pending_len; size_t pending_off; }; struct sahara_image { char *name; void *ptr; size_t len; }; struct qdl_zip; struct libusb_device_handle; 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_push_back(struct qdl_device *qdl, const void *buf, size_t len); int qdl_write(struct qdl_device *qdl, const void *buf, size_t len, unsigned int timeout); void qdl_set_out_chunk_size(struct qdl_device *qdl, long size); int qdl_vip_transfer_enable(struct qdl_device *qdl, const char *vip_table_path); struct qdl_device *usb_init(void); struct qdl_device *sim_init(void); struct qdl_device *qud_init(void); struct qdl_device *auto_init(void); /* * try_usb_open() - single libusb scan-and-open pass; shared between * the --backend usb wait loop in usb.c and the unified auto_open() loop. * Returns 0 on success (and emits the "Flashing/Collecting device" UX * line), -ENODEV when no EDL device is visible, -EBUSY when one is * visible but cannot be opened (typically: the Qualcomm QDLoader 9008 * driver has claimed it), -EIO on a libusb failure. @visible_out, if * non-NULL, receives the count of EDL devices seen on this pass. * * qud_probe_present() returns the number of Qualcomm COM ports the QUD * backend enumerated via SetupAPI; 0 on non-Windows hosts. */ int try_usb_open(struct qdl_device *qdl, const char *serial, int *visible_out); int qud_probe_present(void); struct qdl_device_desc { int vid; int pid; char serial[16]; }; struct qdl_device_desc *usb_list(unsigned int *devices_found); /* * QUD-side counterpart to qdl_device_desc. Serial here is the iSerial as * Windows stored it in the device-instance ID (no fixed length guarantee * across OEMs), and path is the kernel-driver-exposed handle the QUD * backend will open (e.g. "\\\\.\\COM5"). */ struct qud_device_desc { unsigned int pid; char serial[64]; char path[64]; }; struct qud_device_desc *qud_list(unsigned int *devices_found); int firehose_run(struct qdl_device *qdl, struct list_head *ops); int firehose_provision(struct qdl_device *qdl, bool skip_reset); int firehose_read_buf(struct qdl_device *qdl, struct firehose_op *read_op, void *out_buf, size_t out_size); int sahara_run(struct qdl_device *qdl, const struct sahara_image *images, const char *ramdump_path, const char *ramdump_filter); int load_sahara_image(struct qdl_zip *zip, const char *filename, struct sahara_image *image); void sahara_images_free(struct sahara_image *images, size_t count); void print_hex_dump(const char *prefix, const void *buf, size_t len); unsigned int attr_as_unsigned(xmlNode *node, const char *attr, int *errors); const char *attr_as_string(xmlNode *node, const char *attr, int *errors); bool attr_as_bool(xmlNode *node, const char *attr, int *errors); void ux_init(void); void ux_err(const char *fmt, ...); void ux_info(const char *fmt, ...); void ux_log(const char *fmt, ...); void ux_debug(const char *fmt, ...); void ux_progress(const char *fmt, unsigned int value, unsigned int size, ...); void print_version(void); int parse_storage_address(const char *address, int *physical_partition, unsigned int *start_sector, unsigned int *num_sectors, char **gpt_partition); enum qdl_storage_type decode_storage_type(const char *storage); const char *encode_storage_type(enum qdl_storage_type storage); int decode_sahara_config(struct sahara_image *blob, struct sahara_image *images, struct contents_filter *contents_filter); extern bool qdl_debug; #endif linux-msm-qdl-f540e59/qud.c000066400000000000000000000344611521156022300155260ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. * * QUD ("Qualcomm USB Driver") backend: talks to a Qualcomm EDL device * through a kernel-mode driver that exposes the device as a character * file, rather than via libusb. Today only the Windows path is implemented: * the official QDLoader 9008 driver presents the device as a serial port * (\\.\COMx). The Sahara/Firehose framing is identical to the libusb * backend; this file only deals with how user space reaches the underlying * bulk pipes. */ #include #include #include #include #include #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN /* * CancelIoEx() requires Vista+; MinGW headers gate it on _WIN32_WINNT >= 0x0600. * Windows 7 is the de-facto floor for this project so this is safe. */ #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0600 #endif #include /* * must precede any header that declares the GUIDs we reference, * so that GUID_DEVCLASS_PORTS gets a real definition in this TU instead of an * extern reference (MinGW does not ship a uuid.lib that would resolve it). */ #include #include #include #include #include #endif #include "qdl.h" #include "oscompat.h" #ifdef _WIN32 #define QCOM_HWID_PREFIX "USB\\VID_05C6&PID_" /* * The bus-reported device description is a PnP property (DEVPKEY), not * an SPDRP_* registry property; older MinGW headers don't define the * key, so we declare it locally. GUID + PID per Microsoft docs. */ static const DEVPROPKEY qud_devpkey_bus_reported_device_desc = { { 0x540b947e, 0x8b40, 0x45bc, { 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2 } }, 4 }; struct qdl_device_qud { struct qdl_device base; void *handle; /* HANDLE; void* avoids leaking to consumers */ char path[PATH_MAX]; }; /* * Pull the iSerial out of the SetupAPI device-instance ID. The instance ID for * a USB device looks like "USB\\VID_05C6&PID_9008\\"; the trailing * component after the last backslash is the iSerial as Windows stored it. */ static void win_extract_serial(const char *instance_id, char *out, size_t out_size) { const char *last; out[0] = '\0'; last = strrchr(instance_id, '\\'); if (!last) return; last++; snprintf(out, out_size, "%s", last); } /* * Read PortName ("COM5", "COM12", ...) from the device's "Device Parameters" * registry subkey. */ static bool win_read_port_name(HDEVINFO devinfo, SP_DEVINFO_DATA *info, char *out, size_t out_size) { DWORD type = 0; DWORD size = 0; HKEY key; LSTATUS s; key = SetupDiOpenDevRegKey(devinfo, info, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE); if (key == INVALID_HANDLE_VALUE) return false; size = (DWORD)out_size; s = RegQueryValueExA(key, "PortName", NULL, &type, (LPBYTE)out, &size); RegCloseKey(key); if (s != ERROR_SUCCESS || type != REG_SZ) return false; if (size > 0 && out[size - 1] != '\0' && size < out_size) out[size] = '\0'; return true; } /* * The EDL firmware leaves iSerial == 0 and stuffs the real serial into * iProduct as "..._SN:_...". Windows surfaces iProduct verbatim * through SPDRP_BUSREPORTEDDEVICEDESC, so we can recover the same serial * the libusb backend extracts in usb_list() without opening the device. * Returns true and writes the serial on success; false if the property * is missing, empty, or does not contain the _SN: token. */ static bool win_read_iproduct_sn(HDEVINFO devinfo, SP_DEVINFO_DATA *info, char *out, size_t out_size) { WCHAR wdesc[256]; char desc[512]; DEVPROPTYPE prop_type = 0; DWORD required = 0; const char *p; size_t len; int n; if (!SetupDiGetDevicePropertyW(devinfo, info, &qud_devpkey_bus_reported_device_desc, &prop_type, (PBYTE)wdesc, sizeof(wdesc), &required, 0)) return false; if (prop_type != DEVPROP_TYPE_STRING) return false; wdesc[ARRAY_SIZE(wdesc) - 1] = L'\0'; n = WideCharToMultiByte(CP_UTF8, 0, wdesc, -1, desc, sizeof(desc), NULL, NULL); if (n <= 0) return false; p = strstr(desc, "_SN:"); if (!p) return false; p += 4; len = strcspn(p, " _"); if (len == 0 || len + 1 > out_size) return false; memcpy(out, p, len); out[len] = '\0'; return true; } /* * Parse the 4-digit hex PID immediately following the QCOM_HWID_PREFIX * in a HARDWAREID string. Returns 0 if no valid PID is found. */ static unsigned int win_parse_pid(const char *hwid) { const char *p = hwid + sizeof(QCOM_HWID_PREFIX) - 1; unsigned int pid = 0; int i; for (i = 0; i < 4; i++) { char c = p[i]; unsigned int digit; if (c >= '0' && c <= '9') digit = (unsigned int)(c - '0'); else if (c >= 'a' && c <= 'f') digit = (unsigned int)(c - 'a') + 10; else if (c >= 'A' && c <= 'F') digit = (unsigned int)(c - 'A') + 10; else return 0; pid = (pid << 4) | digit; } return pid; } static int win_enumerate_qcom(struct qud_device_desc *out, size_t out_max) { HDEVINFO devinfo; SP_DEVINFO_DATA info = { .cbSize = sizeof(info) }; char hwid[256]; char instance_id[256]; char port_name[32]; size_t count = 0; DWORD i; devinfo = SetupDiGetClassDevsA(&GUID_DEVCLASS_PORTS, NULL, NULL, DIGCF_PRESENT); if (devinfo == INVALID_HANDLE_VALUE) return -1; for (i = 0; SetupDiEnumDeviceInfo(devinfo, i, &info); i++) { DWORD type = 0; DWORD size = 0; if (!SetupDiGetDeviceRegistryPropertyA(devinfo, &info, SPDRP_HARDWAREID, &type, (BYTE *)hwid, sizeof(hwid), &size)) continue; /* HARDWAREID is a REG_MULTI_SZ; first entry is what we want. */ if (strncmp(hwid, QCOM_HWID_PREFIX, sizeof(QCOM_HWID_PREFIX) - 1) != 0) continue; if (!SetupDiGetDeviceInstanceIdA(devinfo, &info, instance_id, sizeof(instance_id), NULL)) continue; if (!win_read_port_name(devinfo, &info, port_name, sizeof(port_name))) continue; if (count < out_max) { out[count].pid = win_parse_pid(hwid); if (!win_read_iproduct_sn(devinfo, &info, out[count].serial, sizeof(out[count].serial))) win_extract_serial(instance_id, out[count].serial, sizeof(out[count].serial)); snprintf(out[count].path, sizeof(out[count].path), "\\\\.\\%s", port_name); } count++; } SetupDiDestroyDeviceInfoList(devinfo); return (int)count; } static int win_configure_handle(HANDLE h) { COMMTIMEOUTS to = { 0 }; DCB dcb = { .DCBlength = sizeof(dcb) }; /* * The QDLoader 9008 driver presents the device as a serial port, but * line-discipline parameters (baud, parity, stop bits) are irrelevant * over USB-CDC. Some driver builds nevertheless reject I/O until DCB * has been initialised, so we set sane defaults and never touch them * again. */ if (!GetCommState(h, &dcb)) return -1; dcb.BaudRate = 115200; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fBinary = TRUE; dcb.fNull = FALSE; dcb.fAbortOnError = FALSE; /* * Force-disable hardware/software flow control: some QDLoader driver * builds gate bulk-IN delivery on these handshake lines, which would * stall reads even though USB-CDC has no real RS-232 signals. */ dcb.fOutxCtsFlow = FALSE; dcb.fOutxDsrFlow = FALSE; dcb.fDsrSensitivity = FALSE; dcb.fOutX = FALSE; dcb.fInX = FALSE; dcb.fDtrControl = DTR_CONTROL_ENABLE; dcb.fRtsControl = RTS_CONTROL_ENABLE; if (!SetCommState(h, &dcb)) return -1; /* * Mirror libusb bulk-read semantics: ReadFile returns as soon as any * data is available, or after ReadTotalTimeoutConstant ms with zero * bytes if the link is idle (MSDN COMMTIMEOUTS, "MAXDWORD interval + * MAXDWORD multiplier" case). qud_read() rewrites the constant * per call; this just seeds a sane default for writes and any read * issued before that. */ to.ReadIntervalTimeout = MAXDWORD; to.ReadTotalTimeoutMultiplier = MAXDWORD; to.ReadTotalTimeoutConstant = 1000; to.WriteTotalTimeoutMultiplier = 0; to.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(h, &to)) return -1; SetupComm(h, 64 * 1024, 64 * 1024); /* * Only purge the TX side. The QDLoader driver claims the USB * interface during PnP enumeration, well before CreateFile() runs, * so the device's Sahara HELLO is typically already sitting in the * driver's RX FIFO by the time we get here. Purging RX would * discard it, and the device only sends HELLO once. */ PurgeComm(h, PURGE_TXCLEAR | PURGE_TXABORT); return 0; } static int qud_open(struct qdl_device *qdl, const char *serial) { struct qdl_device_qud *qd = container_of(qdl, struct qdl_device_qud, base); struct qud_device_desc matches[16]; const char *path = NULL; HANDLE h; int found; int i; /* Allow an explicit \\.\COMx (or COMx) path via --serial. */ if (serial && (serial[0] == '\\' || (strlen(serial) >= 3 && (serial[0] == 'C' || serial[0] == 'c') && (serial[1] == 'O' || serial[1] == 'o') && (serial[2] == 'M' || serial[2] == 'm')))) { path = serial; } else { found = win_enumerate_qcom(matches, ARRAY_SIZE(matches)); if (found < 0) { ux_err("qud: SetupDiGetClassDevs failed (error %lu)\n", (unsigned long)GetLastError()); return -1; } if (found == 0) { ux_err("qud: no Qualcomm COM ports found\n"); return -1; } for (i = 0; i < found && i < (int)ARRAY_SIZE(matches); i++) { if (!serial || serial[0] == '\0' || _stricmp(matches[i].serial, serial) == 0) { path = matches[i].path; break; } } if (!path) { ux_err("qud: no Qualcomm device matching serial \"%s\"\n", serial); return -1; } } h = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (h == INVALID_HANDLE_VALUE) { ux_err("qud: CreateFile(%s) failed (error %lu)\n", path, (unsigned long)GetLastError()); return -1; } if (win_configure_handle(h) < 0) { ux_err("qud: failed to configure %s (error %lu)\n", path, (unsigned long)GetLastError()); CloseHandle(h); return -1; } qd->handle = h; snprintf(qd->path, sizeof(qd->path), "%s", path); ux_debug("qud: opened %s\n", path); return 0; } static int win_overlapped_io(HANDLE h, void *buf, size_t len, unsigned int timeout_ms, bool is_write) { OVERLAPPED ovl = { 0 }; HANDLE ev; DWORD wait; DWORD done = 0; BOOL ok; ev = CreateEvent(NULL, TRUE, FALSE, NULL); if (!ev) return -EIO; ovl.hEvent = ev; if (is_write) ok = WriteFile(h, buf, (DWORD)len, NULL, &ovl); else ok = ReadFile(h, buf, (DWORD)len, NULL, &ovl); if (!ok && GetLastError() != ERROR_IO_PENDING) { CloseHandle(ev); return -EIO; } wait = WaitForSingleObject(ev, timeout_ms); if (wait == WAIT_TIMEOUT) { CancelIoEx(h, &ovl); /* Drain the cancellation so the OVERLAPPED isn't reused live. */ GetOverlappedResult(h, &ovl, &done, TRUE); CloseHandle(ev); return -ETIMEDOUT; } if (wait != WAIT_OBJECT_0) { CloseHandle(ev); return -EIO; } if (!GetOverlappedResult(h, &ovl, &done, FALSE)) { CloseHandle(ev); return -EIO; } CloseHandle(ev); return (int)done; } static int qud_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout) { struct qdl_device_qud *qd = container_of(qdl, struct qdl_device_qud, base); HANDLE h = (HANDLE)qd->handle; COMMTIMEOUTS to = { .ReadIntervalTimeout = MAXDWORD, .ReadTotalTimeoutMultiplier = MAXDWORD, .ReadTotalTimeoutConstant = timeout ? timeout : 1, .WriteTotalTimeoutMultiplier = 0, .WriteTotalTimeoutConstant = 0, }; int ret; /* * The COM driver itself enforces the per-call timeout via * COMMTIMEOUTS; the WaitForSingleObject() inside win_overlapped_io() * is a backstop with a small grace window in case the driver * completes a hair after the constant. */ if (!SetCommTimeouts(h, &to)) return -EIO; ret = win_overlapped_io(h, buf, len, timeout + 250, false); if (ret == 0) return -ETIMEDOUT; return ret; } static int qud_write(struct qdl_device *qdl, const void *buf, size_t len, unsigned int timeout) { struct qdl_device_qud *qd = container_of(qdl, struct qdl_device_qud, base); return win_overlapped_io((HANDLE)qd->handle, (void *)buf, len, timeout, true); } static void qud_close(struct qdl_device *qdl) { struct qdl_device_qud *qd = container_of(qdl, struct qdl_device_qud, base); HANDLE h = (HANDLE)qd->handle; if (h && h != INVALID_HANDLE_VALUE) { CloseHandle(h); qd->handle = NULL; } } static void qud_set_out_chunk_size(struct qdl_device *qdl __unused, long size __unused) { /* The kernel-mode driver handles bulk-transfer chunking. */ } struct qdl_device *qud_init(void) { struct qdl_device_qud *qd = calloc(1, sizeof(*qd)); if (!qd) return NULL; qd->handle = NULL; qd->base.dev_type = QDL_DEVICE_QUD; qd->base.open = qud_open; qd->base.read = qud_read; qd->base.write = qud_write; qd->base.close = qud_close; qd->base.set_out_chunk_size = qud_set_out_chunk_size; qd->base.max_payload_size = 1048576; return &qd->base; } /* * qud_probe_present() - count Qualcomm COM ports exposed by the QDLoader * 9008 driver. Used by the auto-backend selector on Windows to decide * whether the QUD backend can take over when libusb cannot open the * device. */ int qud_probe_present(void) { struct qud_device_desc matches[16]; int found; found = win_enumerate_qcom(matches, ARRAY_SIZE(matches)); return found > 0 ? found : 0; } /* * Enumerate Qualcomm COM ports the QUD backend can drive, returning a * heap-allocated array of qud_device_desc and writing the count to * *devices_found. Caller frees with free(). Returns NULL on error or * when no devices are present (count is still written). */ struct qud_device_desc *qud_list(unsigned int *devices_found) { struct qud_device_desc *result; int found; *devices_found = 0; result = calloc(16, sizeof(*result)); if (!result) return NULL; found = win_enumerate_qcom(result, 16); if (found <= 0) { free(result); return NULL; } if (found > 16) { ux_err("qud: %d devices present, only the first 16 will be listed\n", found); found = 16; } *devices_found = (unsigned int)found; return result; } #else /* unsupported platform */ struct qdl_device *qud_init(void) { ux_err("qud backend is not supported on this platform\n"); return NULL; } int qud_probe_present(void) { return 0; } struct qud_device_desc *qud_list(unsigned int *devices_found) { *devices_found = 0; return NULL; } #endif linux-msm-qdl-f540e59/ramdump.c000066400000000000000000000037041521156022300163760ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause #include #include #include #include #include "qdl.h" #ifdef _WIN32 const char *__progname = "ramdump"; #endif bool qdl_debug; static void print_usage(FILE *out) { extern const char *__progname; fprintf(out, "%s [--debug] [-o ] [segment-filter]\n\n" " -d, --debug\t\tEnable debug output\n" " -v, --version\t\tPrint version\n" " -o, --output=PATH\tDirectory to write ramdump segments into (default: .)\n" " -S, --serial=T\t\tSelect target by serial number T (e.g. <0AA94EFD>)\n" " -h, --help\t\tPrint this usage info\n" " [segment-filter]\tOptional comma-separated list of segment names to dump\n", __progname); } int main(int argc, char **argv) { struct qdl_device *qdl; qdl = qdl_init(QDL_DEVICE_AUTO); if (!qdl) return 1; char *ramdump_path = "."; char *filter = NULL; char *serial = NULL; int ret = 0; int opt; static struct option options[] = { {"debug", no_argument, 0, 'd'}, {"version", no_argument, 0, 'v'}, {"output", required_argument, 0, 'o'}, {"serial", required_argument, 0, 'S'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while ((opt = getopt_long(argc, argv, "dvo:S:h", options, NULL)) != -1) { switch (opt) { case 'd': qdl_debug = true; break; case 'v': print_version(); ret = 0; goto out_cleanup; case 'o': ramdump_path = optarg; break; case 'S': serial = optarg; break; case 'h': print_usage(stdout); return 0; default: print_usage(stderr); return 1; } } if (optind < argc) filter = argv[optind++]; if (optind != argc) { print_usage(stderr); return 1; } ux_init(); if (qdl_debug) print_version(); ret = qdl_open(qdl, serial); if (ret) { ret = 1; goto out_cleanup; } ret = sahara_run(qdl, NULL, ramdump_path, filter); if (ret < 0) { ret = 1; goto out_cleanup; } out_cleanup: qdl_close(qdl); qdl_deinit(qdl); return ret; } linux-msm-qdl-f540e59/read.c000066400000000000000000000066411521156022300156470ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2016-2017, Linaro Ltd. * All rights reserved. */ #include #include #include #include #include #include #include #include "list.h" #include "read.h" #include "qdl.h" #include "oscompat.h" #include "firehose.h" #include "gpt.h" int read_op_load(struct list_head *ops, const char *read_op_file, const char *incdir) { struct firehose_op *read_op; xmlNode *node; xmlNode *root; xmlDoc *doc; int errors; char tmp[PATH_MAX]; doc = xmlReadFile(read_op_file, NULL, 0); if (!doc) { ux_err("failed to parse read-type file \"%s\"\n", read_op_file); return -EINVAL; } root = xmlDocGetRootElement(doc); for (node = root->children; node ; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (xmlStrcmp(node->name, (xmlChar *)"read")) { ux_err("unrecognized tag \"%s\" in read-type file \"%s\", ignoring\n", node->name, read_op_file); continue; } errors = 0; read_op = firehose_alloc_op(FIREHOSE_OP_READ); read_op->sector_size = attr_as_unsigned(node, "SECTOR_SIZE_IN_BYTES", &errors); read_op->filename = attr_as_string(node, "filename", &errors); read_op->partition = attr_as_unsigned(node, "physical_partition_number", &errors); read_op->num_sectors = attr_as_unsigned(node, "num_partition_sectors", &errors); read_op->start_sector = attr_as_string(node, "start_sector", &errors); if (errors) { ux_err("errors while parsing read-type file \"%s\"\n", read_op_file); free(read_op); continue; } if (incdir) { snprintf(tmp, PATH_MAX, "%s/%s", incdir, read_op->filename); if (access(tmp, F_OK) != -1) read_op->filename = strdup(tmp); } list_append(ops, &read_op->node); } xmlFreeDoc(doc); return 0; } int read_cmd_add(struct list_head *ops, const char *address, const char *filename) { struct firehose_op *read_op; unsigned int start_sector; unsigned int num_sectors; char *gpt_partition; int partition; char buf[20]; int ret; ret = parse_storage_address(address, &partition, &start_sector, &num_sectors, &gpt_partition); if (ret < 0) return ret; if (num_sectors == 0 && !gpt_partition) { ux_err("read command without length specifier not supported\n"); return -1; } read_op = firehose_alloc_op(FIREHOSE_OP_READ); if (!read_op) return -1; read_op->sector_size = 0; read_op->filename = strdup(filename); read_op->partition = partition; read_op->num_sectors = num_sectors; sprintf(buf, "%u", start_sector); read_op->start_sector = strdup(buf); read_op->gpt_partition = gpt_partition; list_append(ops, &read_op->node); return 0; } int sha256_cmd_add(struct list_head *ops, const char *address) { struct firehose_op *op; unsigned int start_sector; unsigned int num_sectors; char *gpt_partition; int partition; char buf[20]; int ret; ret = parse_storage_address(address, &partition, &start_sector, &num_sectors, &gpt_partition); if (ret < 0) return ret; if (num_sectors == 0 && !gpt_partition) { ux_err("sha256 command without length specifier not supported\n"); return -1; } op = firehose_alloc_op(FIREHOSE_OP_GET_SHA256_DIGEST); if (!op) return -1; op->sector_size = 0; op->partition = partition; op->num_sectors = num_sectors; sprintf(buf, "%u", start_sector); op->start_sector = strdup(buf); op->gpt_partition = gpt_partition; list_append(ops, &op->node); return 0; } linux-msm-qdl-f540e59/read.h000066400000000000000000000006271521156022300156520ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __READ_H__ #define __READ_H__ #include #include "list.h" struct qdl_device; struct firehose_op; int read_op_load(struct list_head *ops, const char *read_op_file, const char *incdir); int read_cmd_add(struct list_head *ops, const char *address, const char *filename); int sha256_cmd_add(struct list_head *ops, const char *address); #endif linux-msm-qdl-f540e59/sahara.c000066400000000000000000000560141521156022300161720ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2016-2017, Linaro Ltd. * All rights reserved. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "qdl.h" #include "oscompat.h" /* Minimal ELF64 definitions — is not available on all platforms */ #define ELFMAG "\177ELF" #define SELFMAG 4 #define ET_CORE 4 #define PT_LOAD 1 #define EI_NIDENT 16 typedef uint16_t Elf64_Half; typedef uint32_t Elf64_Word; typedef uint64_t Elf64_Addr; typedef uint64_t Elf64_Off; typedef uint64_t Elf64_Xword; typedef struct { unsigned char e_ident[EI_NIDENT]; Elf64_Half e_type; Elf64_Half e_machine; Elf64_Word e_version; Elf64_Addr e_entry; Elf64_Off e_phoff; Elf64_Off e_shoff; Elf64_Word e_flags; Elf64_Half e_ehsize; Elf64_Half e_phentsize; Elf64_Half e_phnum; Elf64_Half e_shentsize; Elf64_Half e_shnum; Elf64_Half e_shstrndx; } Elf64_Ehdr; typedef struct { Elf64_Word p_type; Elf64_Word p_flags; Elf64_Off p_offset; Elf64_Addr p_vaddr; Elf64_Addr p_paddr; Elf64_Xword p_filesz; Elf64_Xword p_memsz; Elf64_Xword p_align; } Elf64_Phdr; #define SAHARA_HELLO_CMD 0x1 /* Min protocol version 1.0 */ #define SAHARA_HELLO_RESP_CMD 0x2 /* Min protocol version 1.0 */ #define SAHARA_READ_DATA_CMD 0x3 /* Min protocol version 1.0 */ #define SAHARA_END_OF_IMAGE_CMD 0x4 /* Min protocol version 1.0 */ #define SAHARA_DONE_CMD 0x5 /* Min protocol version 1.0 */ #define SAHARA_DONE_RESP_CMD 0x6 /* Min protocol version 1.0 */ #define SAHARA_RESET_CMD 0x7 /* Min protocol version 1.0 */ #define SAHARA_RESET_RESP_CMD 0x8 /* Min protocol version 1.0 */ #define SAHARA_MEM_DEBUG_CMD 0x9 /* Min protocol version 2.0 */ #define SAHARA_MEM_READ_CMD 0xa /* Min protocol version 2.0 */ #define SAHARA_CMD_READY_CMD 0xb /* Min protocol version 2.1 */ #define SAHARA_SWITCH_MODE_CMD 0xc /* Min protocol version 2.1 */ #define SAHARA_EXECUTE_CMD 0xd /* Min protocol version 2.1 */ #define SAHARA_EXECUTE_RESP_CMD 0xe /* Min protocol version 2.1 */ #define SAHARA_EXECUTE_DATA_CMD 0xf /* Min protocol version 2.1 */ #define SAHARA_MEM_DEBUG64_CMD 0x10 /* Min protocol version 2.5 */ #define SAHARA_MEM_READ64_CMD 0x11 /* Min protocol version 2.5 */ #define SAHARA_READ_DATA64_CMD 0x12 /* Min protocol version 2.8 */ #define SAHARA_RESET_STATE_CMD 0x13 /* Min protocol version 2.9 */ #define SAHARA_WRITE_DATA_CMD 0x14 /* Min protocol version 3.0 */ #define SAHARA_VERSION 2 #define SAHARA_SUCCESS 0 #define SAHARA_MODE_IMAGE_TX_PENDING 0x0 #define SAHARA_MODE_IMAGE_TX_COMPLETE 0x1 #define SAHARA_MODE_MEMORY_DEBUG 0x2 #define SAHARA_MODE_COMMAND 0x3 #define SAHARA_HELLO_LENGTH 0x30 #define SAHARA_READ_DATA_LENGTH 0x14 #define SAHARA_READ_DATA64_LENGTH 0x20 #define SAHARA_END_OF_IMAGE_LENGTH 0x10 #define SAHARA_MEM_READ64_LENGTH 0x18 #define SAHARA_MEM_DEBUG64_LENGTH 0x18 #define SAHARA_DONE_LENGTH 0x8 #define SAHARA_DONE_RESP_LENGTH 0xc #define SAHARA_RESET_LENGTH 0x8 #define DEBUG_BLOCK_SIZE (512u * 1024u) #define SAHARA_CMD_TIMEOUT_MS 1000 struct sahara_pkt { uint32_t cmd; uint32_t length; union { struct { uint32_t version; uint32_t compatible; uint32_t max_len; uint32_t mode; uint32_t reserved[6]; } hello_req; struct { uint32_t version; uint32_t compatible; uint32_t status; uint32_t mode; uint32_t reserved[6]; } hello_resp; struct { uint32_t image; uint32_t offset; uint32_t length; } read_req; struct { uint32_t image; uint32_t status; } eoi; struct { } done_req; struct { uint32_t status; } done_resp; struct { uint64_t addr; uint64_t length; } debug64_req; struct { uint64_t image; uint64_t offset; uint64_t length; } read64_req; }; }; struct sahara_debug_region64 { uint64_t type; uint64_t addr; uint64_t length; char region[20]; char filename[20]; }; static void sahara_send_reset(struct qdl_device *qdl) { struct sahara_pkt resp; resp.cmd = SAHARA_RESET_CMD; resp.length = SAHARA_RESET_LENGTH; qdl_write(qdl, &resp, resp.length, SAHARA_CMD_TIMEOUT_MS); } static int sahara_send_hello_resp(struct qdl_device *qdl, unsigned int mode) { struct sahara_pkt resp = {}; resp.cmd = SAHARA_HELLO_RESP_CMD; resp.length = SAHARA_HELLO_LENGTH; resp.hello_resp.version = SAHARA_VERSION; resp.hello_resp.compatible = 1; resp.hello_resp.status = SAHARA_SUCCESS; resp.hello_resp.mode = mode; qdl_write(qdl, &resp, resp.length, SAHARA_CMD_TIMEOUT_MS); return 0; } static int sahara_hello(struct qdl_device *qdl, struct sahara_pkt *pkt) { if (pkt->length != SAHARA_HELLO_LENGTH) { ux_err("unexpected HELLO packet length %u\n", pkt->length); sahara_send_reset(qdl); return -1; } ux_debug("HELLO version: 0x%x compatible: 0x%x max_len: %d mode: %d\n", pkt->hello_req.version, pkt->hello_req.compatible, pkt->hello_req.max_len, pkt->hello_req.mode); return sahara_send_hello_resp(qdl, pkt->hello_req.mode); } static int sahara_read(struct qdl_device *qdl, struct sahara_pkt *pkt, const struct sahara_image *images) { const struct sahara_image *image; unsigned int image_idx; size_t offset; size_t len; int ret; if (pkt->length != SAHARA_READ_DATA_LENGTH) { ux_err("unexpected READ_DATA packet length %u\n", pkt->length); sahara_send_reset(qdl); return -1; } ux_debug("READ image: %d offset: 0x%x length: 0x%x\n", pkt->read_req.image, pkt->read_req.offset, pkt->read_req.length); image_idx = pkt->read_req.image; if (image_idx >= MAPPING_SZ || !images[image_idx].ptr) { ux_err("device requested unknown image id %u, ensure that all Sahara images are provided\n", image_idx); sahara_send_reset(qdl); return -1; } offset = pkt->read_req.offset; len = pkt->read_req.length; image = &images[image_idx]; if (offset > image->len || offset + len > image->len) { ux_err("device requested invalid range of image %d\n", image_idx); return -1; } if (offset == 0) ux_info("Sahara: sending %s (%zu bytes)\n", image->name ? image->name : "(unknown)", image->len); ux_progress("%s", offset + len, image->len, image->name ? image->name : "image"); ret = qdl_write(qdl, image->ptr + offset, len, SAHARA_CMD_TIMEOUT_MS); if (ret < 0 || ((size_t)ret != len)) { ux_err("failed to write %zu bytes to sahara\n", len); return -1; } return 0; } static int sahara_read64(struct qdl_device *qdl, struct sahara_pkt *pkt, const struct sahara_image *images) { const struct sahara_image *image; unsigned int image_idx; size_t offset; size_t len; int ret; if (pkt->length != SAHARA_READ_DATA64_LENGTH) { ux_err("unexpected READ_DATA64 packet length %u\n", pkt->length); sahara_send_reset(qdl); return -1; } ux_debug("READ64 image: %" PRId64 " offset: 0x%" PRIx64 " length: 0x%" PRIx64 "\n", pkt->read64_req.image, pkt->read64_req.offset, pkt->read64_req.length); image_idx = pkt->read64_req.image; if (image_idx >= MAPPING_SZ || !images[image_idx].ptr) { ux_err("device requested unknown image id %u, ensure that all Sahara images are provided\n", image_idx); sahara_send_reset(qdl); return -1; } offset = pkt->read64_req.offset; len = pkt->read64_req.length; image = &images[image_idx]; if (offset > image->len || offset + len > image->len) { ux_err("device requested invalid range of image %d\n", image_idx); return -1; } if (offset == 0) ux_info("Sahara: sending %s (%zu bytes)\n", image->name ? image->name : "(unknown)", image->len); ux_progress("%s", offset + len, image->len, image->name ? image->name : "image"); ret = qdl_write(qdl, image->ptr + offset, len, SAHARA_CMD_TIMEOUT_MS); if (ret < 0 || ((size_t)ret != len)) { ux_err("failed to write %zu bytes to sahara\n", len); return -1; } return 0; } static int sahara_eoi(struct qdl_device *qdl, struct sahara_pkt *pkt) { struct sahara_pkt done; if (pkt->length != SAHARA_END_OF_IMAGE_LENGTH) { ux_err("unexpected END_OF_IMAGE packet length %u\n", pkt->length); sahara_send_reset(qdl); return -1; } ux_debug("END OF IMAGE image: %d status: %d\n", pkt->eoi.image, pkt->eoi.status); if (pkt->eoi.status != 0) { ux_err("received non-successful end-of-image result\n"); return -1; } done.cmd = SAHARA_DONE_CMD; done.length = SAHARA_DONE_LENGTH; qdl_write(qdl, &done, done.length, SAHARA_CMD_TIMEOUT_MS); return 0; } static int sahara_done(struct qdl_device *qdl, struct sahara_pkt *pkt) { if (pkt->length != SAHARA_DONE_RESP_LENGTH) { ux_err("unexpected DONE_RESP packet length %u\n", pkt->length); sahara_send_reset(qdl); return -1; } ux_debug("DONE status: %d\n", pkt->done_resp.status); // 0 == PENDING, 1 == COMPLETE. Device expects more images if // PENDING is set in status. return pkt->done_resp.status; } static ssize_t sahara_debug64_one(struct qdl_device *qdl, struct sahara_debug_region64 region, const char *ramdump_path) { struct sahara_pkt read_req; uint64_t remain; size_t offset, buf_offset; size_t chunk; size_t written; ssize_t n; void *buf; int fd; buf = malloc(DEBUG_BLOCK_SIZE); if (!buf) return -1; char path[PATH_MAX]; snprintf(path, sizeof(path), "%s/%s", ramdump_path, region.filename); fd = open(path, O_WRONLY | O_CREAT | O_BINARY, 0644); if (fd < 0) { warn("failed to open \"%s\"", region.filename); free(buf); return -1; } chunk = 0; while (chunk < region.length) { remain = MIN((uint64_t)(region.length - chunk), DEBUG_BLOCK_SIZE); read_req.cmd = SAHARA_MEM_READ64_CMD; read_req.length = SAHARA_MEM_READ64_LENGTH; read_req.debug64_req.addr = region.addr + chunk; read_req.debug64_req.length = remain; n = qdl_write(qdl, &read_req, read_req.length, SAHARA_CMD_TIMEOUT_MS); if (n < 0) break; offset = 0; while (offset < remain) { buf_offset = 0; n = qdl_read(qdl, buf, DEBUG_BLOCK_SIZE, 30000); if (n < 0) { warn("failed to read ramdump chunk"); goto out; } while (buf_offset < (size_t)n) { written = write(fd, buf + buf_offset, n - buf_offset); if (written <= 0) { warn("failed to write ramdump chunk to \"%s\"", region.filename); goto out; } buf_offset += written; } offset += buf_offset; } qdl_read(qdl, buf, DEBUG_BLOCK_SIZE, 10); chunk += DEBUG_BLOCK_SIZE; ux_progress("%s", chunk, region.length, region.filename); } out: close(fd); free(buf); return 0; } // simple pattern matching function supporting * and ? bool pattern_match(const char *pattern, const char *string) { if (*pattern == '\0' && *string == '\0') return true; if (*pattern == '*') return pattern_match(pattern + 1, string) || (*string != '\0' && pattern_match(pattern, string + 1)); if (*pattern == '?') return (*string != '\0') && pattern_match(pattern + 1, string + 1); if (*pattern == *string) return pattern_match(pattern + 1, string + 1); return false; } static bool sahara_debug64_filter(const char *filename, const char *filter) { char stem[PATH_MAX] = {}; bool anymatch = false; const char *dot; char *ptr; char *tmp; char *s; if (!filter) return false; /* Build a stem (filename without extension) for bare-name filter tokens */ dot = strrchr(filename, '.'); if (dot && dot != filename) { size_t len = dot - filename; if (len < sizeof(stem)) { memcpy(stem, filename, len); } } tmp = strdup(filter); for (s = strtok_r(tmp, ",", &ptr); s; s = strtok_r(NULL, ",", &ptr)) { if (pattern_match(s, filename) || (stem[0] && pattern_match(s, stem))) { anymatch = true; break; } } free(tmp); return !anymatch; } static int write_zeroes(FILE *out, size_t count) { static const char zeros[4096]; size_t chunk; while (count > 0) { chunk = count > sizeof(zeros) ? sizeof(zeros) : count; if (fwrite(zeros, 1, chunk, out) != chunk) return -1; count -= chunk; } return 0; } static ssize_t copy_file_to(FILE *out, const char *path) { char buf[65536]; ssize_t written = 0; FILE *in; size_t n; in = fopen(path, "rb"); if (!in) return -1; while ((n = fread(buf, 1, sizeof(buf), in)) > 0) { if (fwrite(buf, 1, n, out) != n) { fclose(in); return -1; } written += n; } fclose(in); return written; } /* * sahara_build_minidump_elf() - assemble a minidump ELF from downloaded regions * @ramdump_path: directory containing the downloaded region files * @filter: the segment filter that was applied during download (may be NULL) * @table: the Sahara debug region table received from the device * @n_regions: number of entries in @table * * When the kernel minidump backend is active, one of the downloaded regions is * named "KELF". It is a fully formed ELF core header whose PT_LOAD program * headers describe the physical address and output offset of every other kernel * region. A second region ("Kvmcorein") carries the vmcoreinfo data that fills * the tail of the PT_NOTE section and has no PT_LOAD entry of its own. * * This function detects those two special regions, then iterates the PT_LOAD * headers in p_offset order, matches each one to a downloaded region file by * physical address, and concatenates everything into a single minidump.elf * ready for crash or gdb. * * All segments are written sequentially because meminspect assigns p_offset * values in a strictly increasing, contiguous order. */ static void sahara_build_minidump_elf(const char *ramdump_path, const char *filter, const struct sahara_debug_region64 *table, size_t n_regions) { char path[PATH_MAX]; char out_path[PATH_MAX]; Elf64_Ehdr *ehdr; Elf64_Phdr *phdrs; void *kelf_buf = NULL; size_t kelf_size; ssize_t kelf_idx = -1; ssize_t vmcore_idx = -1; ssize_t written; FILE *out = NULL; FILE *in; size_t i, j; bool matched; /* Locate the KELF region; bail out silently if absent (not a minidump) */ for (i = 0; i < n_regions; i++) { if (!strncmp(table[i].region, "md_KELF", 7)) { kelf_idx = i; break; } } if (kelf_idx < 0) return; /* If KELF itself was excluded by the filter, nothing to do */ if (sahara_debug64_filter(table[kelf_idx].filename, filter)) return; /* Read the KELF file into memory so we can parse its program headers */ snprintf(path, sizeof(path), "%s/%s", ramdump_path, table[kelf_idx].filename); in = fopen(path, "rb"); if (!in) { ux_err("minidump: failed to open KELF file %s\n", path); return; } long kelf_size_l; fseek(in, 0, SEEK_END); kelf_size_l = ftell(in); if (kelf_size_l <= 0) { ux_err("minidump: failed to get KELF file size\n"); fclose(in); return; } fseek(in, 0, SEEK_SET); kelf_size = (size_t)kelf_size_l; kelf_buf = malloc(kelf_size); if (!kelf_buf) { fclose(in); return; } if (fread(kelf_buf, 1, kelf_size, in) != kelf_size) { ux_err("minidump: failed to read KELF file\n"); fclose(in); goto out_free; } fclose(in); ehdr = kelf_buf; if (kelf_size < sizeof(*ehdr) || memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || ehdr->e_type != ET_CORE || ehdr->e_phentsize != sizeof(Elf64_Phdr) || ehdr->e_phoff > kelf_size || (size_t)ehdr->e_phnum * sizeof(Elf64_Phdr) > kelf_size - ehdr->e_phoff) { ux_err("minidump: KELF does not look like a 64-bit ELF core file\n"); goto out_free; } phdrs = (Elf64_Phdr *)((char *)kelf_buf + ehdr->e_phoff); /* * Identify the vmcoreinfo region: among all K-prefixed regions, it is * the one whose physical address has no matching PT_LOAD p_paddr entry. * It fills the tail of the PT_NOTE section and must be placed immediately * after the KELF content in the output file. */ for (i = 0; i < n_regions; i++) { if ((ssize_t)i == kelf_idx || strncmp(table[i].region, "md_K", 4) != 0) continue; matched = false; for (j = 0; j < ehdr->e_phnum; j++) { if (phdrs[j].p_type == PT_LOAD && phdrs[j].p_paddr == table[i].addr) { matched = true; break; } } if (!matched) { vmcore_idx = i; break; } } snprintf(out_path, sizeof(out_path), "%s/minidump.elf", ramdump_path); out = fopen(out_path, "wb"); if (!out) { ux_err("minidump: failed to create %s\n", out_path); goto out_free; } /* 1. Write KELF: provides the ELF header, phdrs, and partial PT_NOTE */ if (fwrite(kelf_buf, 1, kelf_size, out) != kelf_size) { ux_err("minidump: failed to write KELF to output\n"); goto out_close; } /* 2. Write vmcoreinfo immediately after KELF to complete the PT_NOTE */ if (vmcore_idx >= 0) { snprintf(path, sizeof(path), "%s/%s", ramdump_path, table[vmcore_idx].filename); if (copy_file_to(out, path) < 0) ux_err("minidump: vmcoreinfo region missing, crash may not load correctly\n"); } /* * 3. Write PT_LOAD segments in ascending p_offset order. * meminspect assigns p_offset values contiguously in registration * order so iterating the phdr array gives the correct sequence. */ for (j = 0; j < ehdr->e_phnum; j++) { if (phdrs[j].p_type != PT_LOAD) continue; matched = false; for (i = 0; i < n_regions; i++) { if (table[i].addr != phdrs[j].p_paddr) continue; snprintf(path, sizeof(path), "%s/%s", ramdump_path, table[i].filename); written = copy_file_to(out, path); if (written < 0) { ux_err("minidump: region %s missing, skipping\n", table[i].filename); matched = true; break; } /* Zero-pad to p_filesz to keep offsets aligned */ if ((size_t)written < phdrs[j].p_filesz && write_zeroes(out, phdrs[j].p_filesz - (size_t)written)) { ux_err("minidump: failed to pad region %s\n", table[i].filename); goto out_close; } matched = true; break; } if (!matched) ux_err("minidump: no region found for PT_LOAD at paddr 0x%" PRIx64 "\n", phdrs[j].p_paddr); } ux_info("minidump: ELF written to %s\n", out_path); out_close: fclose(out); out_free: free(kelf_buf); } static void sahara_debug64(struct qdl_device *qdl, struct sahara_pkt *pkt, const char *ramdump_path, const char *filter) { struct sahara_debug_region64 *table; struct sahara_pkt read_req; ssize_t n; size_t i; if (pkt->length != SAHARA_MEM_DEBUG64_LENGTH) { ux_err("unexpected MEM_DEBUG64 packet length %u\n", pkt->length); sahara_send_reset(qdl); return; } ux_debug("DEBUG64 address: 0x%" PRIx64 " length: 0x%" PRIx64 "\n", pkt->debug64_req.addr, pkt->debug64_req.length); read_req.cmd = SAHARA_MEM_READ64_CMD; read_req.length = SAHARA_MEM_READ64_LENGTH; read_req.debug64_req.addr = pkt->debug64_req.addr; read_req.debug64_req.length = pkt->debug64_req.length; n = qdl_write(qdl, &read_req, read_req.length, SAHARA_CMD_TIMEOUT_MS); if (n < 0) return; if (read_req.debug64_req.length > 64 * 1024) { ux_err("DEBUG64 table length 0x%" PRIx64 " exceeds limit\n", read_req.debug64_req.length); return; } table = malloc(read_req.debug64_req.length); if (!table) return; n = qdl_read(qdl, table, pkt->debug64_req.length, SAHARA_CMD_TIMEOUT_MS); if (n < 0) { free(table); return; } for (i = 0; i < pkt->debug64_req.length / sizeof(table[0]); i++) { if (sahara_debug64_filter(table[i].filename, filter)) { ux_info("%s skipped per filter\n", table[i].filename); continue; } ux_debug("%-2d: type 0x%" PRIx64 " address: 0x%" PRIx64 " length: 0x%" PRIx64 " region: %s filename: %s\n", i, table[i].type, table[i].addr, table[i].length, table[i].region, table[i].filename); n = sahara_debug64_one(qdl, table[i], ramdump_path); if (n < 0) break; ux_info("%s dumped successfully\n", table[i].filename); } sahara_build_minidump_elf(ramdump_path, filter, table, pkt->debug64_req.length / sizeof(table[0])); free(table); sahara_send_reset(qdl); } static bool sahara_has_done_pending_quirk(const struct sahara_image *images) { unsigned int count = 0; int i; /* * E.g MSM8916 EDL reports done = pending, allow this when one a single * image is provided, and it's used as SAHARA_ID_EHOSTDL_IMG. */ for (i = 0; i < MAPPING_SZ; i++) { if (images[i].ptr) count++; } return count == 1 && images[SAHARA_ID_EHOSTDL_IMG].ptr; } static void sahara_debug_list_images(const struct sahara_image *images) { int i; ux_debug("Sahara images:\n"); for (i = 0; i < MAPPING_SZ; i++) { if (images[i].ptr) ux_debug(" %2d: %s\n", i, images[i].name ? : "(unknown)"); } } int sahara_run(struct qdl_device *qdl, const struct sahara_image *images, const char *ramdump_path, const char *ramdump_filter) { /* * Auto-detect that the device is already running the Firehose * programmer (e.g. left running by a previous --skip-reset * invocation): if the first read times out or returns a Firehose XML * banner instead of a Sahara HELLO, skip Sahara entirely. Disabled * in ramdump mode, where Sahara is the only valid protocol. */ const bool detect_firehose = !ramdump_path; bool first_read = true; struct sahara_pkt *pkt; char buf[4096]; char tmp[32]; bool done = false; int n; if (images) sahara_debug_list_images(images); /* * Don't need to do anything in simulation mode with Sahara, * we care only about Firehose protocol */ if (qdl->dev_type == QDL_DEVICE_SIM) return 0; while (!done) { n = qdl_read(qdl, buf, sizeof(buf), SAHARA_CMD_TIMEOUT_MS); if (n < 0) { if (first_read && detect_firehose && n == -ETIMEDOUT) { /* * The QUD driver will eat the HELLO request on * many modern targets, so send an unsolicited * HELLO response. * If the device is already in Firehose mode, * the programmer will fail to parse the "XML" * message and report an error, which will * trigger below detection of a dev_type == QDL_DEVICE_QUD || qdl->dev_type == QDL_DEVICE_AUTO) { sahara_send_hello_resp(qdl, 0); continue; } ux_info("no Sahara HELLO received; assuming Firehose programmer is already running\n"); return 0; } ux_err("failed to read sahara request from device\n"); break; } if (first_read && detect_firehose && n >= 5 && !memcmp(buf, "length) { ux_err("request length not matching received request\n"); return -EINVAL; } switch (pkt->cmd) { case SAHARA_HELLO_CMD: if (sahara_hello(qdl, pkt) < 0) return -1; break; case SAHARA_READ_DATA_CMD: if (sahara_read(qdl, pkt, images) < 0) return -1; break; case SAHARA_END_OF_IMAGE_CMD: if (sahara_eoi(qdl, pkt) < 0) return -1; break; case SAHARA_DONE_RESP_CMD: n = sahara_done(qdl, pkt); if (n < 0) return -1; done = n; /* E.g MSM8916 EDL reports done = 0 here */ if (sahara_has_done_pending_quirk(images)) done = true; break; case SAHARA_MEM_DEBUG64_CMD: sahara_debug64(qdl, pkt, ramdump_path, ramdump_filter); break; case SAHARA_READ_DATA64_CMD: if (sahara_read64(qdl, pkt, images) < 0) return -1; break; case SAHARA_RESET_RESP_CMD: if (pkt->length != SAHARA_RESET_LENGTH) { ux_err("unexpected RESET_RESP packet length %u\n", pkt->length); return -1; } if (ramdump_path) done = true; break; default: sprintf(tmp, "CMD%x", pkt->cmd); print_hex_dump(tmp, buf, n); break; } } return done ? 0 : -1; } linux-msm-qdl-f540e59/scripts/000077500000000000000000000000001521156022300162505ustar00rootroot00000000000000linux-msm-qdl-f540e59/scripts/checkpatch_wrapper.sh000077500000000000000000000050541521156022300224500ustar00rootroot00000000000000#!/bin/bash # SPDX-License-Identifier: BSD-3-Clause set -euo pipefail ROOT_URL="https://raw.githubusercontent.com/torvalds/linux/v6.15/scripts" CHECKPATCH_URL="${ROOT_URL}/checkpatch.pl" SPELLING_URL="${ROOT_URL}/spelling.txt" ROOT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )/../" CHECKPATCH_DIR="${ROOT_PATH}/.scripts" CHECKPATCH="${CHECKPATCH_DIR}/checkpatch.pl" SPELLING="${CHECKPATCH_DIR}/spelling.txt" # Just to repo root to work consistently from anywhere cd "$ROOT_PATH" # List only tracked source files (not ignored/untracked) CHECKPATCH_SOURCES=$(git ls-files | grep -E '\.(c|h|sh)$' | \ grep -vE '(^|/)sha2\.(c|h)$' | \ grep -vE 'version\.h$' | \ grep -vE 'list\.h$' ) ensure_checkpatch() { if [[ ! -x "$CHECKPATCH" ]]; then echo "Downloading checkpatch.pl and spelling.txt..." mkdir -p "$CHECKPATCH_DIR" curl -sSfL "$CHECKPATCH_URL" -o "$CHECKPATCH" curl -sSfL "$SPELLING_URL" -o "$SPELLING" chmod +x "$CHECKPATCH" fi } do_check_all() { ensure_checkpatch echo "Running checkpatch on tracked source files (excluding sha2.c, sha2.h)..." for file in ${CHECKPATCH_SOURCES}; do perl "${CHECKPATCH}" --no-tree -f "$file" || exit 1 done } do_check_cached() { ensure_checkpatch echo "Running checkpatch on staged changes..." git diff --cached -- . | perl "$CHECKPATCH" --no-tree - } do_check_range() { ensure_checkpatch local base="${CHECKPATCH_BASE:-origin/master}" local head="${CHECKPATCH_HEAD:-HEAD}" if ! git rev-parse --verify "${base}" >/dev/null 2>&1; then echo "Base ref '${base}' is not available." >&2 echo "Set CHECKPATCH_BASE to a fetched ref (e.g. origin/master)." >&2 exit 1 fi local commits commits=$(git rev-list --reverse "${base}..${head}") if [[ -z "${commits}" ]]; then echo "No commits in range ${base}..${head}; nothing to check." return 0 fi echo "Running checkpatch on commits in range ${base}..${head}..." local fail=0 for commit in ${commits}; do echo echo "--- Checking commit ${commit} ---" if ! git format-patch -1 --stdout "${commit}" | perl "${CHECKPATCH}" --no-tree -; then fail=1 fi done return $fail } usage() { echo "Usage: $0 {download-checkpatch|check|check-cached|check-range}" echo echo " check-range Run checkpatch per-commit on the range" echo " \$CHECKPATCH_BASE..\$CHECKPATCH_HEAD" echo " (defaults: origin/master..HEAD)" exit 2 } case "${1:-}" in download) ensure_checkpatch ;; check) do_check_all ;; check-cached) do_check_cached ;; check-range) do_check_range ;; *) usage ;; esac linux-msm-qdl-f540e59/scripts/markdownlint_wrapper.sh000077500000000000000000000012311521156022300230550ustar00rootroot00000000000000#!/bin/bash # SPDX-License-Identifier: BSD-3-Clause set -euo pipefail ROOT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )/../" # Run consistently from the repo root regardless of where the script is called cd "$ROOT_PATH" # Files to lint. Keep this in sync with .github/workflows/markdown-lint.yml MARKDOWN_FILES=("README.md") run_lint() { if ! command -v mdl >/dev/null 2>&1; then echo "mdl not found." >&2 echo "Install it with: sudo apt install markdownlint (or 'gem install mdl')" >&2 exit 1 fi mdl "${MARKDOWN_FILES[@]}" } usage() { echo "Usage: $0 {check}" exit 2 } case "${1:-}" in check) run_lint ;; *) usage ;; esac linux-msm-qdl-f540e59/sha2.c000066400000000000000000000304421521156022300155650ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* $OpenBSD: sha2.c,v 1.28 2019/07/23 12:35:22 dtucker Exp $ */ /* * FILE: sha2.c * AUTHOR: Aaron D. Gifford * * Copyright (c) 2000-2001, Aaron D. Gifford * All rights reserved. * * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $ */ #include #include #include "sha2.h" /* * UNROLLED TRANSFORM LOOP NOTE: * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform * loop version for the hash transform rounds (defined using macros * later in this file). Either define on the command line, for example: * * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c * * or define below: * * #define SHA2_UNROLL_TRANSFORM * */ #ifndef SHA2_SMALL #if defined(__amd64__) || defined(__i386__) #define SHA2_UNROLL_TRANSFORM #endif #endif /*** SHA-224/256/384/512 Machine Architecture Definitions *****************/ /* * BYTE_ORDER NOTE: * * Please make sure that your system defines BYTE_ORDER. If your * architecture is little-endian, make sure it also defines * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are * equivalent. * * If your system does not define the above, then you can do so by * hand like this: * * #define LITTLE_ENDIAN 1234 * #define BIG_ENDIAN 4321 * * And for little-endian machines, add: * * #define BYTE_ORDER LITTLE_ENDIAN * * Or for big-endian machines: * * #define BYTE_ORDER BIG_ENDIAN * * The FreeBSD machine this was written on defines BYTE_ORDER * appropriately by including (which in turn includes * where the appropriate definitions are actually * made). */ #if defined(__MINGW32__) || defined(__MINGW64__) #include #endif #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN) #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN #endif /*** SHA-224/256/384/512 Various Length Definitions ***********************/ /* NOTE: Most of these are in sha2.h */ #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) /*** ENDIAN SPECIFIC COPY MACROS **************************************/ #define BE_8_TO_32(dst, cp) do { \ (dst) = (uint32_t)(cp)[3] | ((uint32_t)(cp)[2] << 8) | \ ((uint32_t)(cp)[1] << 16) | ((uint32_t)(cp)[0] << 24); \ } while(0) #define BE_8_TO_64(dst, cp) do { \ (dst) = (uint64_t)(cp)[7] | ((uint64_t)(cp)[6] << 8) | \ ((uint64_t)(cp)[5] << 16) | ((uint64_t)(cp)[4] << 24) | \ ((uint64_t)(cp)[3] << 32) | ((uint64_t)(cp)[2] << 40) | \ ((uint64_t)(cp)[1] << 48) | ((uint64_t)(cp)[0] << 56); \ } while (0) #define BE_64_TO_8(cp, src) do { \ (cp)[0] = (src) >> 56; \ (cp)[1] = (src) >> 48; \ (cp)[2] = (src) >> 40; \ (cp)[3] = (src) >> 32; \ (cp)[4] = (src) >> 24; \ (cp)[5] = (src) >> 16; \ (cp)[6] = (src) >> 8; \ (cp)[7] = (src); \ } while (0) #define BE_32_TO_8(cp, src) do { \ (cp)[0] = (src) >> 24; \ (cp)[1] = (src) >> 16; \ (cp)[2] = (src) >> 8; \ (cp)[3] = (src); \ } while (0) /* * Macro for incrementally adding the unsigned 64-bit integer n to the * unsigned 128-bit integer (represented using a two-element array of * 64-bit words): */ #define ADDINC128(w,n) do { \ (w)[0] += (uint64_t)(n); \ if ((w)[0] < (n)) { \ (w)[1]++; \ } \ } while (0) /*** THE SIX LOGICAL FUNCTIONS ****************************************/ /* * Bit shifting and rotation (used by the six SHA-XYZ logical functions: * * NOTE: The naming of R and S appears backwards here (R is a SHIFT and * S is a ROTATION) because the SHA-224/256/384/512 description document * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this * same "backwards" definition. */ /* Shift-right (used in SHA-224, SHA-256, SHA-384, and SHA-512): */ #define R(b,x) ((x) >> (b)) /* 32-bit Rotate-right (used in SHA-224 and SHA-256): */ #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b)))) /* Two of six logical functions used in SHA-224, SHA-256, SHA-384, and SHA-512: */ #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) /* Four of six logical functions used in SHA-224 and SHA-256: */ #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x))) #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x))) #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x))) #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ /* Hash constant words K for SHA-224 and SHA-256: */ static const uint32_t K256[64] = { 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL }; /* Initial hash value H for SHA-256: */ static const uint32_t sha256_initial_hash_value[8] = { 0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL, 0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL }; /*** SHA-256: *********************************************************/ void SHA256Init(SHA2_CTX *context) { memcpy(context->state.st32, sha256_initial_hash_value, sizeof(sha256_initial_hash_value)); memset(context->buffer, 0, sizeof(context->buffer)); context->bitcount[0] = 0; } #ifdef SHA2_UNROLL_TRANSFORM /* Unrolled SHA-256 round macros: */ #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do { \ BE_8_TO_32(W256[j], data); \ data += 4; \ T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \ (d) += T1; \ (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \ j++; \ } while(0) #define ROUND256(a,b,c,d,e,f,g,h) do { \ s0 = W256[(j+1)&0x0f]; \ s0 = sigma0_256(s0); \ s1 = W256[(j+14)&0x0f]; \ s1 = sigma1_256(s1); \ T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + \ (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \ (d) += T1; \ (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \ j++; \ } while(0) void SHA256Transform(uint32_t state[8], const uint8_t data[SHA256_BLOCK_LENGTH]) { uint32_t a, b, c, d, e, f, g, h, s0, s1; uint32_t T1, W256[16]; int j; /* Initialize registers with the prev. intermediate value */ a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; f = state[5]; g = state[6]; h = state[7]; j = 0; do { /* Rounds 0 to 15 (unrolled): */ ROUND256_0_TO_15(a,b,c,d,e,f,g,h); ROUND256_0_TO_15(h,a,b,c,d,e,f,g); ROUND256_0_TO_15(g,h,a,b,c,d,e,f); ROUND256_0_TO_15(f,g,h,a,b,c,d,e); ROUND256_0_TO_15(e,f,g,h,a,b,c,d); ROUND256_0_TO_15(d,e,f,g,h,a,b,c); ROUND256_0_TO_15(c,d,e,f,g,h,a,b); ROUND256_0_TO_15(b,c,d,e,f,g,h,a); } while (j < 16); /* Now for the remaining rounds up to 63: */ do { ROUND256(a,b,c,d,e,f,g,h); ROUND256(h,a,b,c,d,e,f,g); ROUND256(g,h,a,b,c,d,e,f); ROUND256(f,g,h,a,b,c,d,e); ROUND256(e,f,g,h,a,b,c,d); ROUND256(d,e,f,g,h,a,b,c); ROUND256(c,d,e,f,g,h,a,b); ROUND256(b,c,d,e,f,g,h,a); } while (j < 64); /* Compute the current intermediate hash value */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; state[5] += f; state[6] += g; state[7] += h; /* Clean up */ a = b = c = d = e = f = g = h = T1 = 0; } #else /* SHA2_UNROLL_TRANSFORM */ void SHA256Transform(uint32_t state[8], const uint8_t data[SHA256_BLOCK_LENGTH]) { uint32_t a, b, c, d, e, f, g, h, s0, s1; uint32_t T1, T2, W256[16]; int j; /* Initialize registers with the prev. intermediate value */ a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; f = state[5]; g = state[6]; h = state[7]; j = 0; do { BE_8_TO_32(W256[j], data); data += 4; /* Apply the SHA-256 compression function to update a..h */ T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j]; T2 = Sigma0_256(a) + Maj(a, b, c); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; j++; } while (j < 16); do { /* Part of the message block expansion: */ s0 = W256[(j+1)&0x0f]; s0 = sigma0_256(s0); s1 = W256[(j+14)&0x0f]; s1 = sigma1_256(s1); /* Apply the SHA-256 compression function to update a..h */ T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); T2 = Sigma0_256(a) + Maj(a, b, c); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; j++; } while (j < 64); /* Compute the current intermediate hash value */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; state[5] += f; state[6] += g; state[7] += h; /* Clean up */ a = b = c = d = e = f = g = h = T1 = T2 = 0; } #endif /* SHA2_UNROLL_TRANSFORM */ void SHA256Update(SHA2_CTX *context, const uint8_t *data, size_t len) { uint64_t freespace, usedspace; /* Calling with no data is valid (we do nothing) */ if (len == 0) return; usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH; if (usedspace > 0) { /* Calculate how much free space is available in the buffer */ freespace = SHA256_BLOCK_LENGTH - usedspace; if (len >= freespace) { /* Fill the buffer completely and process it */ memcpy(&context->buffer[usedspace], data, freespace); context->bitcount[0] += freespace << 3; len -= freespace; data += freespace; SHA256Transform(context->state.st32, context->buffer); } else { /* The buffer is not yet full */ memcpy(&context->buffer[usedspace], data, len); context->bitcount[0] += (uint64_t)len << 3; /* Clean up: */ usedspace = freespace = 0; return; } } while (len >= SHA256_BLOCK_LENGTH) { /* Process as many complete blocks as we can */ SHA256Transform(context->state.st32, data); context->bitcount[0] += SHA256_BLOCK_LENGTH << 3; len -= SHA256_BLOCK_LENGTH; data += SHA256_BLOCK_LENGTH; } if (len > 0) { /* There's left-overs, so save 'em */ memcpy(context->buffer, data, len); context->bitcount[0] += len << 3; } /* Clean up: */ usedspace = freespace = 0; } void SHA256Pad(SHA2_CTX *context) { unsigned int usedspace; usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH; if (usedspace > 0) { /* Begin padding with a 1 bit: */ context->buffer[usedspace++] = 0x80; if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) { /* Set-up for the last transform: */ memset(&context->buffer[usedspace], 0, SHA256_SHORT_BLOCK_LENGTH - usedspace); } else { if (usedspace < SHA256_BLOCK_LENGTH) { memset(&context->buffer[usedspace], 0, SHA256_BLOCK_LENGTH - usedspace); } /* Do second-to-last transform: */ SHA256Transform(context->state.st32, context->buffer); /* Prepare for last transform: */ memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH); } } else { /* Set-up for the last transform: */ memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH); /* Begin padding with a 1 bit: */ *context->buffer = 0x80; } /* Store the length of input data (in bits) in big endian format: */ BE_64_TO_8(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], context->bitcount[0]); /* Final transform: */ SHA256Transform(context->state.st32, context->buffer); /* Clean up: */ usedspace = 0; } void SHA256Final(uint8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *context) { SHA2_CTX *volatile const contextv = context; SHA256Pad(context); #if BYTE_ORDER == LITTLE_ENDIAN int i; /* Convert TO host byte order */ for (i = 0; i < 8; i++) BE_32_TO_8(digest + i * 4, context->state.st32[i]); #else memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH); #endif memset(contextv, 0, sizeof(*contextv)); }linux-msm-qdl-f540e59/sha2.h000066400000000000000000000021511521156022300155660ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* $OpenBSD: sha2.h,v 1.10 2016/09/03 17:00:29 tedu Exp $ */ /* * FILE: sha2.h * AUTHOR: Aaron D. Gifford * * Copyright (c) 2000-2001, Aaron D. Gifford * All rights reserved. * * $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $ */ #ifndef __SHA2_H__ #define __SHA2_H__ #include "stdint.h" /*** SHA-256/384/512 Various Length Definitions ***********************/ #define SHA256_BLOCK_LENGTH 64 #define SHA256_DIGEST_LENGTH 32 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) /*** SHA-224/256/384/512 Context Structure *******************************/ typedef struct _SHA2_CTX { union { uint32_t st32[8]; uint64_t st64[8]; } state; uint64_t bitcount[2]; uint8_t buffer[SHA256_BLOCK_LENGTH]; } SHA2_CTX; void SHA256Init(SHA2_CTX *); void SHA256Transform(uint32_t state[8], const uint8_t [SHA256_BLOCK_LENGTH]); void SHA256Update(SHA2_CTX *, const uint8_t *, size_t); void SHA256Pad(SHA2_CTX *); void SHA256Final(uint8_t [SHA256_DIGEST_LENGTH], SHA2_CTX *); char *SHA256End(SHA2_CTX *, char *); #endif /* __SHA2_H__ */linux-msm-qdl-f540e59/sim.c000066400000000000000000000502571521156022300155260ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved. */ #include #include #include #include #include #include #include "oscompat.h" #include "sha2.h" #include "sim.h" /* * Response XML templates sent by the simulated device. * * The configure response echoes back MaxPayloadSizeToTargetInBytes matching * the host's initial value (1 MiB), so firehose_try_configure() never * triggers a second configure exchange. */ #define SIM_ACK \ "" \ "" #define SIM_ACK_RAWMODE \ "" \ "" #define SIM_CONFIGURE_ACK \ "" \ "" /* sim_state tracks whether the sim is in XML command mode or raw binary mode */ enum sim_state { SIM_STATE_XML, /* normal state: dequeue queued XML responses */ SIM_STATE_RAW_IN, /* receiving raw binary data from host (program) */ SIM_STATE_RAW_OUT, /* sending raw binary data to host (read) */ }; struct sim_response { char *data; size_t len; struct sim_response *next; }; /* * VIP hash validation state. * * sim_vip_recv_table() extracts the data-chunk SHA256 hashes from each * received VIP table (signed MBN or raw chained binary) and stores them in * vip_hashes[]. sim_vip_check_chunk() computes SHA256 of each incoming chunk * and compares it against the next stored entry. * * Maximum capacity: * signed table: MAX_DIGESTS_PER_SIGNED_FILE - 1 = 53 chunk hashes * chained tables: MAX_CHAINED_FILES x (MAX_DIGESTS_PER_CHAINED_FILE - 1) * = 32 x 255 = 8160 chunk hashes * total: 8213 entries x SHA256_DIGEST_LENGTH bytes = ~256 KiB * (heap-allocated together with the enclosing struct qdl_device_sim) */ #define SIM_VIP_MAX_HASHES \ ((MAX_DIGESTS_PER_SIGNED_FILE - 1) + \ MAX_CHAINED_FILES * (MAX_DIGESTS_PER_CHAINED_FILE - 1)) struct qdl_device_sim { struct qdl_device base; struct vip_table_generator *vip_gen; bool create_digests; /* Response queue consumed by sim_read() */ struct sim_response *resp_head; struct sim_response *resp_tail; enum sim_state state; size_t raw_remaining; /* bytes of raw data left to transfer */ bool closed; /* set after power command to terminate reads fast */ /* VIP hash validation */ uint8_t vip_hashes[SIM_VIP_MAX_HASHES][SHA256_DIGEST_LENGTH]; size_t vip_hash_count; /* entries populated from received VIP tables */ size_t vip_hash_idx; /* index of the next hash to verify */ /* Chain hash linking each VIP table to the next */ uint8_t vip_chain_hash[SHA256_DIGEST_LENGTH]; bool vip_has_chain_hash; }; static void sim_enqueue(struct qdl_device_sim *qdl_sim, const char *xml) { struct sim_response *resp; resp = malloc(sizeof(*resp)); if (!resp) err(1, "sim: failed to allocate response entry"); resp->data = strdup(xml); if (!resp->data) err(1, "sim: failed to duplicate response string"); resp->len = strlen(xml); resp->next = NULL; if (qdl_sim->resp_tail) qdl_sim->resp_tail->next = resp; else qdl_sim->resp_head = resp; qdl_sim->resp_tail = resp; } static void sim_enqueue_log(struct qdl_device_sim *qdl_sim, const char *handler) { char buf[256]; snprintf(buf, sizeof(buf), "" "", handler); sim_enqueue(qdl_sim, buf); } static unsigned int sim_get_uint_attr(xmlNode *node, const char *name) { xmlChar *val; unsigned int ret; val = xmlGetProp(node, (xmlChar *)name); if (!val) return 0; ret = (unsigned int)strtoul((char *)val, NULL, 10); xmlFree(val); return ret; } /* * Qualcomm MBN header structures (little-endian, packed). * * The signed VIP table (DigestsToSign.bin.mbn) uses one of these layouts. * V5 and V6 share the same image_size/code_size offsets; V7 reorders the * fields. Only V5 and V6 are supported for VIP table parsing today. */ struct mbn_header_v5 { uint32_t image_id; uint32_t header_version; uint32_t qti_signature_size; uint32_t qti_cert_chain_size; uint32_t image_size; uint32_t code_size; uint32_t signature_ptr; uint32_t signature_size; uint32_t cert_chain_ptr; uint32_t cert_chain_size; } __attribute__((packed)); struct mbn_header_v6 { uint32_t image_id; uint32_t header_version; uint32_t qti_signature_size; uint32_t qti_cert_chain_size; uint32_t image_size; uint32_t code_size; uint32_t signature_ptr; uint32_t signature_size; uint32_t cert_chain_ptr; uint32_t cert_chain_size; uint32_t qti_metadata_size; uint32_t metadata_size; } __attribute__((packed)); #define MBN_HEADER_VERSION_5 5 #define MBN_HEADER_VERSION_6 6 /* * sim_vip_signed_payload() - locate the hash payload in a signed VIP MBN table * * DigestsToSign.bin.mbn is a Qualcomm MBN image with a packed header followed * by the hash data, a signature, and certificate chains. The header_version * field identifies the layout; V5 and V6 share the same image_size/code_size * offsets. The payload begins at (file_size - image_size) and spans code_size * bytes. * * Returns a pointer into @buf for the payload, or NULL if not recognised. * Sets @out_len to the payload byte count on success. */ static const uint8_t *sim_vip_signed_payload(const uint8_t *buf, size_t len, size_t *out_len) { struct mbn_header_v6 hdr; size_t offset; if (len < sizeof(struct mbn_header_v5)) return NULL; memcpy(&hdr, buf, sizeof(struct mbn_header_v5)); if (hdr.header_version != MBN_HEADER_VERSION_5 && hdr.header_version != MBN_HEADER_VERSION_6) return NULL; if (hdr.code_size == 0 || hdr.code_size % SHA256_DIGEST_LENGTH != 0 || hdr.code_size > MAX_DIGESTS_PER_SIGNED_FILE * SHA256_DIGEST_LENGTH || hdr.image_size < hdr.code_size || hdr.image_size > len) return NULL; offset = len - hdr.image_size; if (offset < sizeof(struct mbn_header_v5) || offset + hdr.code_size > len) return NULL; *out_len = hdr.code_size; return buf + offset; } /* * sim_vip_recv_table() - parse a received VIP table and store chunk hashes * * @is_signed: true -> DigestsToSign.bin.mbn (MBN-wrapped, signed) * false -> ChainedTableOfDigests.bin (raw binary) * * The last entry of a full signed table and of a non-final chained table is a * chain hash (SHA256 of the following table) rather than a data-chunk hash. * It is excluded from the stored list. * * A chained table is "final" when its raw byte count is not a multiple of * SHA256_DIGEST_LENGTH: vip_gen_finalize() appends a single zero-padding byte * to the final chained table so that it cannot be a USB-512-byte multiple. */ static void sim_vip_recv_table(struct qdl_device_sim *qdl_sim, const void *buf, size_t len, bool is_signed) { const uint8_t *hashes; size_t count, data_count; bool has_chain_hash; if (is_signed) { size_t payload_len; hashes = sim_vip_signed_payload(buf, len, &payload_len); if (!hashes) { ux_err("sim: VIP signed table: unrecognised format " "(%zu bytes)\n", len); return; } count = payload_len / SHA256_DIGEST_LENGTH; /* Full table uses the last slot for the chain hash */ has_chain_hash = (count == MAX_DIGESTS_PER_SIGNED_FILE); /* Save chain hash so the first chained table can be verified */ if (has_chain_hash) { memcpy(qdl_sim->vip_chain_hash, hashes + (count - 1) * SHA256_DIGEST_LENGTH, SHA256_DIGEST_LENGTH); qdl_sim->vip_has_chain_hash = true; } } else { /* Verify this table against the chain hash from the previous table */ if (qdl_sim->vip_has_chain_hash) { uint8_t hash[SHA256_DIGEST_LENGTH]; char got_hex[SHA256_DIGEST_STRING_LENGTH]; char exp_hex[SHA256_DIGEST_STRING_LENGTH]; SHA2_CTX ctx; size_t i; SHA256Init(&ctx); SHA256Update(&ctx, buf, len); SHA256Final(hash, &ctx); if (memcmp(hash, qdl_sim->vip_chain_hash, SHA256_DIGEST_LENGTH) != 0) { for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { sprintf(got_hex + i * 2, "%02x", hash[i]); sprintf(exp_hex + i * 2, "%02x", qdl_sim->vip_chain_hash[i]); } got_hex[SHA256_DIGEST_STRING_LENGTH - 1] = '\0'; exp_hex[SHA256_DIGEST_STRING_LENGTH - 1] = '\0'; ux_err("sim: VIP chained table chain hash mismatch\n" " computed: %s\n" " expected: %s\n", got_hex, exp_hex); } else { ux_debug("sim: VIP chained table chain hash OK\n"); } qdl_sim->vip_has_chain_hash = false; } hashes = buf; if (len % SHA256_DIGEST_LENGTH != 0) { /* * Trailing zero-padding byte -> final chained table. * All floor(len / 32) entries are chunk hashes. */ count = len / SHA256_DIGEST_LENGTH; has_chain_hash = false; } else { /* * Size is a multiple of 32 -> non-final chained table. * The last entry is a chain hash to the next table. */ count = len / SHA256_DIGEST_LENGTH; has_chain_hash = (count == MAX_DIGESTS_PER_CHAINED_FILE); } /* Save chain hash so the next chained table can be verified */ if (has_chain_hash) { memcpy(qdl_sim->vip_chain_hash, hashes + (count - 1) * SHA256_DIGEST_LENGTH, SHA256_DIGEST_LENGTH); qdl_sim->vip_has_chain_hash = true; } } data_count = has_chain_hash ? count - 1 : count; if (qdl_sim->vip_hash_count + data_count > SIM_VIP_MAX_HASHES) { ux_err("sim: VIP table overflow (%zu + %zu > %u), " "truncating\n", qdl_sim->vip_hash_count, data_count, SIM_VIP_MAX_HASHES); data_count = SIM_VIP_MAX_HASHES - qdl_sim->vip_hash_count; } memcpy(qdl_sim->vip_hashes[qdl_sim->vip_hash_count], hashes, data_count * SHA256_DIGEST_LENGTH); qdl_sim->vip_hash_count += data_count; ux_debug("sim: loaded %zu VIP chunk hashes from %s table " "(total: %zu)\n", data_count, is_signed ? "signed" : "chained", qdl_sim->vip_hash_count); } /* * sim_vip_check_chunk() - SHA256 @buf/@len and verify against the next entry * * Called once per VIP chunk (one XML command write or one raw-data write). * A mismatch is reported via ux_err() but does not abort, so that all * mismatches across a full flash run are visible at once. */ static void sim_vip_check_chunk(struct qdl_device_sim *qdl_sim, const void *buf, size_t len) { uint8_t hash[SHA256_DIGEST_LENGTH]; char got_hex[SHA256_DIGEST_STRING_LENGTH]; char exp_hex[SHA256_DIGEST_STRING_LENGTH]; const uint8_t *expected; SHA2_CTX ctx; size_t i; if (qdl_sim->create_digests || !qdl_sim->vip_hash_count) return; SHA256Init(&ctx); SHA256Update(&ctx, buf, len); SHA256Final(hash, &ctx); if (qdl_sim->vip_hash_idx >= qdl_sim->vip_hash_count) { ux_err("sim: VIP chunk %zu has no entry in the digest table " "(table has %zu entries)\n", qdl_sim->vip_hash_idx, qdl_sim->vip_hash_count); qdl_sim->vip_hash_idx++; return; } expected = qdl_sim->vip_hashes[qdl_sim->vip_hash_idx]; if (memcmp(hash, expected, SHA256_DIGEST_LENGTH) == 0) { ux_debug("sim: VIP chunk %zu hash OK\n", qdl_sim->vip_hash_idx); qdl_sim->vip_hash_idx++; return; } for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { sprintf(got_hex + i * 2, "%02x", hash[i]); sprintf(exp_hex + i * 2, "%02x", expected[i]); } got_hex[SHA256_DIGEST_STRING_LENGTH - 1] = '\0'; exp_hex[SHA256_DIGEST_STRING_LENGTH - 1] = '\0'; ux_err("sim: VIP hash mismatch for chunk %zu\n" " computed: %s\n" " expected: %s\n", qdl_sim->vip_hash_idx, got_hex, exp_hex); qdl_sim->vip_hash_idx++; } static int sim_open(struct qdl_device *qdl __unused, const char *serial __unused) { 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 __unused) {} /* * sim_read() - serve the next queued XML response or raw-out data * * Returns the number of bytes written to @buf, or a negative errno. * Returns -ETIMEDOUT when the response queue is empty (mirroring the * behaviour of the USB driver when no data arrives within the timeout). * Returns -EIO after a power/reset command to let the caller drain quickly. */ static int sim_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout __unused) { struct qdl_device_sim *qdl_sim = container_of(qdl, struct qdl_device_sim, base); struct sim_response *resp; size_t copy_len; /* * Always drain the response queue first. This matters for read * commands: sim_write() sets SIM_STATE_RAW_OUT and queues the * rawmode=true ACK at the same time, so the pending XML must be * served before we start sending raw sector data. */ resp = qdl_sim->resp_head; if (resp) { copy_len = resp->len; memcpy(buf, resp->data, copy_len); qdl_sim->resp_head = resp->next; if (!qdl_sim->resp_head) qdl_sim->resp_tail = NULL; free(resp->data); free(resp); return copy_len; } /* * Queue empty and in raw-out mode: the host has received the * rawmode=true ACK and is now reading sector data. Serve zeros * and enqueue the final rawmode=false ACK when all sectors are done. */ if (qdl_sim->state == SIM_STATE_RAW_OUT) { copy_len = MIN(len, qdl_sim->raw_remaining); memset(buf, 0, copy_len); qdl_sim->raw_remaining -= copy_len; if (qdl_sim->raw_remaining == 0) { sim_enqueue(qdl_sim, SIM_ACK); qdl_sim->state = SIM_STATE_XML; } return copy_len; } /* * Queue is empty. Return -EIO after power/reset so that callers * which drain trailing log messages after the final ACK return * immediately instead of spinning until the timeout expires. */ if (qdl_sim->closed) return -EIO; return -ETIMEDOUT; } /* * sim_write() - accept a host write and queue the matching device response(s) * * In SIM_STATE_RAW_IN the simulator counts down the expected raw payload and * queues the final rawmode=false ACK once the last byte arrives. * * In SIM_STATE_XML the XML command is parsed and one or more response messages * are pushed onto the queue for sim_read() to serve back. * * In dry-run mode without digest generation, binary writes that fail XML * parsing are treated as VIP digest tables: sim_vip_recv_table() extracts the * chunk hashes and an ACK is enqueued so that the host's post-send * firehose_read() succeeds. Every subsequent XML command write and every * raw-data write is then verified against the stored hashes by * sim_vip_check_chunk(). */ static int sim_write(struct qdl_device *qdl, const void *buf, size_t len, unsigned int timeout __unused) { struct qdl_device_sim *qdl_sim = container_of(qdl, struct qdl_device_sim, base); unsigned int num_sectors, sector_size; xmlNode *root, *node, *child; xmlDoc *doc; /* * VIP table writes (both the signed MBN and chained raw binaries) may * arrive while SIM_STATE_RAW_IN is active, interleaved between data * chunks of the same program operation. Intercept them here using the * sending_table flag set exclusively by vip_transfer_send_raw(), before * the raw-data accounting branch below. */ if (!qdl_sim->create_digests && qdl_sim->base.vip_data.sending_table) { bool is_signed = (qdl_sim->base.vip_data.state == VIP_INIT); sim_vip_recv_table(qdl_sim, buf, len, is_signed); sim_enqueue(qdl_sim, SIM_ACK); return len; } /* Raw binary payload for an ongoing program operation */ if (qdl_sim->state == SIM_STATE_RAW_IN) { /* * Each qdl_write() call maps to exactly one VIP chunk * (firehose_do_program() loops over max_payload_size chunks, * calling vip_gen_chunk_init/update/store once per iteration). * Hash the entire write and verify against the digest table. */ sim_vip_check_chunk(qdl_sim, buf, len); if (len >= qdl_sim->raw_remaining) { sim_enqueue(qdl_sim, SIM_ACK); qdl_sim->state = SIM_STATE_XML; qdl_sim->raw_remaining = 0; } else { qdl_sim->raw_remaining -= len; } return len; } /* XML command: parse and enqueue appropriate response(s) */ doc = xmlReadMemory(buf, len, NULL, NULL, XML_PARSE_NOWARNING | XML_PARSE_NOERROR); if (!doc) return len; /* * Valid XML command: verify it against the digest table before * dispatching so that the hash index stays in step with the * vip_gen_chunk_store() calls made during the dry-run that * produced the table. */ sim_vip_check_chunk(qdl_sim, buf, len); root = xmlDocGetRootElement(doc); /* Locate the element */ for (node = root; node; node = node->next) { if (node->type == XML_ELEMENT_NODE && xmlStrcmp(node->name, (xmlChar *)"data") == 0) break; } if (!node) goto out; /* First child element is the command */ for (child = node->children; child && child->type != XML_ELEMENT_NODE; child = child->next) ; if (!child) goto out; if (xmlStrcmp(child->name, (xmlChar *)"configure") == 0) { sim_enqueue_log(qdl_sim, "configure"); sim_enqueue(qdl_sim, SIM_CONFIGURE_ACK); } else if (xmlStrcmp(child->name, (xmlChar *)"program") == 0) { num_sectors = sim_get_uint_attr(child, "num_partition_sectors"); sector_size = sim_get_uint_attr(child, "SECTOR_SIZE_IN_BYTES"); sim_enqueue_log(qdl_sim, "program"); sim_enqueue(qdl_sim, SIM_ACK_RAWMODE); qdl_sim->state = SIM_STATE_RAW_IN; qdl_sim->raw_remaining = (size_t)num_sectors * sector_size; } else if (xmlStrcmp(child->name, (xmlChar *)"read") == 0) { num_sectors = sim_get_uint_attr(child, "num_partition_sectors"); sector_size = sim_get_uint_attr(child, "SECTOR_SIZE_IN_BYTES"); sim_enqueue_log(qdl_sim, "read"); sim_enqueue(qdl_sim, SIM_ACK_RAWMODE); qdl_sim->state = SIM_STATE_RAW_OUT; qdl_sim->raw_remaining = (size_t)num_sectors * sector_size; } else if (xmlStrcmp(child->name, (xmlChar *)"erase") == 0) { sim_enqueue_log(qdl_sim, "erase"); sim_enqueue(qdl_sim, SIM_ACK); } else if (xmlStrcmp(child->name, (xmlChar *)"patch") == 0) { sim_enqueue_log(qdl_sim, "patch"); sim_enqueue(qdl_sim, SIM_ACK); } else if (xmlStrcmp(child->name, (xmlChar *)"setbootablestoragedrive") == 0) { sim_enqueue_log(qdl_sim, "setbootablestoragedrive"); sim_enqueue(qdl_sim, SIM_ACK); } else if (xmlStrcmp(child->name, (xmlChar *)"power") == 0) { sim_enqueue_log(qdl_sim, "power"); sim_enqueue(qdl_sim, SIM_ACK); /* * Mark the sim closed so that the trailing drain read in * firehose_reset() returns -EIO immediately rather than * spinning until the timeout expires. */ qdl_sim->closed = true; } else if (xmlStrcmp(child->name, (xmlChar *)"ufs") == 0) { sim_enqueue_log(qdl_sim, "ufs"); sim_enqueue(qdl_sim, SIM_ACK); } else { /* Unknown command: respond with a generic ACK */ sim_enqueue(qdl_sim, SIM_ACK); } out: xmlFreeDoc(doc); return len; } static void sim_set_out_chunk_size(struct qdl_device *qdl __unused, long size __unused) {} 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; qdl->max_payload_size = 1048576; /* * Pre-set a non-zero sector size so that firehose_try_configure() * skips the sector-size probe reads. Those probe commands would * otherwise be hashed into the VIP digest table during dry-run * digest generation (--createdigest), but they are never sent * during the real VIP flash -- causing a hash mismatch. */ qdl->sector_size = 4096; return qdl; } struct vip_table_generator *sim_get_vip_generator(struct qdl_device *qdl) { struct qdl_device_sim *qdl_sim; if (qdl->dev_type != QDL_DEVICE_SIM) return NULL; qdl_sim = container_of(qdl, struct qdl_device_sim, base); if (!qdl_sim->create_digests) return NULL; return qdl_sim->vip_gen; } bool sim_set_digest_generation(bool create_digests, struct qdl_device *qdl, struct vip_table_generator *vip_gen) { struct qdl_device_sim *qdl_sim; if (qdl->dev_type != QDL_DEVICE_SIM) return false; qdl_sim = container_of(qdl, struct qdl_device_sim, base); qdl_sim->create_digests = create_digests; qdl_sim->vip_gen = vip_gen; return true; } linux-msm-qdl-f540e59/sim.h000066400000000000000000000006471521156022300155310ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved. */ #ifndef __SIM_H__ #define __SIM_H__ #include "qdl.h" #include "vip.h" struct vip_table_generator *sim_get_vip_generator(struct qdl_device *qdl); bool sim_set_digest_generation(bool create_digests, struct qdl_device *qdl, struct vip_table_generator *vip_gen); #endif /* __SIM_H__ */ linux-msm-qdl-f540e59/sparse.c000066400000000000000000000061751521156022300162330ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2025, Maksim Paimushkin * All rights reserved. */ #define _FILE_OFFSET_BITS 64 #ifdef _WIN32 #include #else #include #endif #include #include #include #include #include #include #include #include #include "file.h" #include "sparse.h" #include "qdl.h" int sparse_header_parse(struct qdl_file *file, sparse_header_t *sparse_header) { qdl_file_seek(file, 0, SEEK_SET); if (qdl_file_read(file, sparse_header, sizeof(sparse_header_t)) != sizeof(sparse_header_t)) { ux_err("[SPARSE] Unable to read sparse header\n"); return -EINVAL; } if (sparse_header->magic != SPARSE_HEADER_MAGIC) { ux_err("[SPARSE] Invalid magic in sparse header\n"); return -EINVAL; } if (sparse_header->major_version != SPARSE_HEADER_MAJOR_VER) { ux_err("[SPARSE] Invalid major version in sparse header\n"); return -EINVAL; } if (sparse_header->minor_version != SPARSE_HEADER_MINOR_VER) { ux_err("[SPARSE] Invalid minor version in sparse header\n"); return -EINVAL; } if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) qdl_file_seek(file, sparse_header->file_hdr_sz - sizeof(sparse_header_t), SEEK_CUR); return 0; } int sparse_chunk_header_parse(struct qdl_file *file, sparse_header_t *sparse_header, uint64_t *chunk_size, uint32_t *value, off_t *offset) { chunk_header_t chunk_header; uint32_t fill_value = 0; unsigned int type; *chunk_size = 0; *value = 0; if (qdl_file_read(file, &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)) qdl_file_seek(file, sparse_header->chunk_hdr_sz - sizeof(chunk_header_t), SEEK_CUR); type = chunk_header.chunk_type; *chunk_size = (uint64_t)chunk_header.chunk_sz * sparse_header->blk_sz; switch (type) { case CHUNK_TYPE_RAW: 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 */ *offset = qdl_file_seek(file, 0, SEEK_CUR); /* Move the file cursor forward by the size of the chunk */ qdl_file_seek(file, *chunk_size, SEEK_CUR); break; case CHUNK_TYPE_DONT_CARE: if (chunk_header.total_sz != sparse_header->chunk_hdr_sz) { ux_err("[SPARSE] Bogus chunk size, type Don't Care\n"); return -EINVAL; } break; case CHUNK_TYPE_FILL: 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 (qdl_file_read(file, &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; break; default: ux_err("[SPARSE] Unknown chunk type: %#x\n", type); return -EINVAL; } return type; } linux-msm-qdl-f540e59/sparse.h000066400000000000000000000041661521156022300162360ustar00rootroot00000000000000/* SPDX-License-Identifier: Apache-2.0 */ /* * Copyright (C) 2010 The Android Open Source Project */ #ifndef __SPARSE_H__ #define __SPARSE_H__ #include #include struct qdl_file; typedef struct __attribute__((__packed__)) sparse_header { /* 0xed26ff3a */ uint32_t magic; /* (0x1) - reject images with higher major versions */ uint16_t major_version; /* (0x0) - allow images with higer minor versions */ uint16_t minor_version; /* 28 bytes for first revision of the file format */ uint16_t file_hdr_sz; /* 12 bytes for first revision of the file format */ uint16_t chunk_hdr_sz; /* block size in bytes, must be a multiple of 4 (4096) */ uint32_t blk_sz; /* total blocks in the non-sparse output image */ uint32_t total_blks; /* total chunks in the sparse input image */ uint32_t total_chunks; /* * CRC32 checksum of the original data, counting "don't care" * as 0. Standard 802.3 polynomial, use a Public Domain * table implementation */ uint32_t image_checksum; } sparse_header_t; #define SPARSE_HEADER_MAGIC 0xed26ff3a #define SPARSE_HEADER_MAJOR_VER 0x0001 #define SPARSE_HEADER_MINOR_VER 0x0000 typedef struct __attribute__((__packed__)) chunk_header { uint16_t chunk_type; /* 0xCAC1 -> raw; 0xCAC2 -> fill; 0xCAC3 -> don't care */ uint16_t reserved1; uint32_t chunk_sz; /* in blocks in output image */ uint32_t total_sz; /* in bytes of chunk input file including chunk header and data */ } chunk_header_t; #define CHUNK_TYPE_RAW 0xCAC1 #define CHUNK_TYPE_FILL 0xCAC2 #define CHUNK_TYPE_DONT_CARE 0xCAC3 /* * Parses the sparse image header from the file descriptor. * Returns 0 on success, or an error code otherwise. */ int sparse_header_parse(struct qdl_file *file, sparse_header_t *sparse_header); /* * Parses the sparse image chunk header from the file descriptor. * Sets the chunk size, and value or offset based on the parsed data. * Returns the chunk type on success, or an error code otherwise. */ int sparse_chunk_header_parse(struct qdl_file *file, sparse_header_t *sparse_header, uint64_t *chunk_size, uint32_t *value, off_t *offset); #endif linux-msm-qdl-f540e59/tests/000077500000000000000000000000001521156022300157235ustar00rootroot00000000000000linux-msm-qdl-f540e59/tests/common.c000066400000000000000000000012331521156022300173560ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause #include #include #include #include "common.h" int test_make_temp_dir(char *buf, size_t size, const char *prefix) { int ret; #ifdef _WIN32 const char *tmp_base = getenv("TMPDIR"); if (!tmp_base || tmp_base[0] == '\0') tmp_base = getenv("TEMP"); if (!tmp_base || tmp_base[0] == '\0') tmp_base = getenv("TMP"); if (!tmp_base || tmp_base[0] == '\0') tmp_base = "."; ret = snprintf(buf, size, "%s/%s-XXXXXX", tmp_base, prefix); #else ret = snprintf(buf, size, "/tmp/%s-XXXXXX", prefix); #endif if (ret <= 0 || (size_t)ret >= size) return -1; return mkdtemp(buf) ? 0 : -1; } linux-msm-qdl-f540e59/tests/common.h000066400000000000000000000003041521156022300173610ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __TESTS_COMMON_H__ #define __TESTS_COMMON_H__ #include int test_make_temp_dir(char *buf, size_t size, const char *prefix); #endif linux-msm-qdl-f540e59/tests/data/000077500000000000000000000000001521156022300166345ustar00rootroot00000000000000linux-msm-qdl-f540e59/tests/data/flashmap.json000066400000000000000000000010251521156022300213200ustar00rootroot00000000000000{ "version": "1.1.0", "products": [ { "name": "unit-test", "layouts": [ { "name": "layout0", "programmer": [ "prog_firehose_ddr.elf" ], "programmable": [ { "memory": "spinor", "slot": 0, "files": [ "rawprogram0.xml", "rawprogram1.xml", "patch0.xml", "patch1.xml" ] } ] } ] } ] } linux-msm-qdl-f540e59/tests/data/generate_flat_build.sh000077500000000000000000000026751521156022300231640ustar00rootroot00000000000000# SPDX-License-Identifier: BSD-3-Clause #!/bin/sh SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" OUTDIR="${1:-$SCRIPT_PATH}" mkdir -p "$OUTDIR" create_file_with_size() { filename="$1" size_kbytes="$2" # Create a sparse file of the requested size: dd with count=0 seek=N # extends the file to N*bs bytes via lseek without writing any data. # Reads still yield zeros, so callers (sha256, qdl --dry-run, zip) see # the same content as a fully-written file, but disk I/O and allocation # are O(1). Avoids ~1 GB of writes on slow CI runners. dd if=/dev/zero of="$OUTDIR/$filename" bs=1024 count=0 seek="$size_kbytes" status=none } create_file_with_size prog_firehose_ddr.elf 20 create_file_with_size efi.bin 8192 create_file_with_size gpt_backup0.bin 20 create_file_with_size gpt_backup1.bin 20 create_file_with_size gpt_main0.bin 24 create_file_with_size gpt_main1.bin 24 create_file_with_size rootfs.img 8192 create_file_with_size xbl_config.elf 320 create_file_with_size xbl.elf 800 # Copy static test data (XML descriptors and flashmap.json) into the output directory if [ "$OUTDIR" != "$SCRIPT_PATH" ]; then cp "$SCRIPT_PATH"/*.xml "$OUTDIR"/ cp "$SCRIPT_PATH"/flashmap.json "$OUTDIR"/ fi (cd $OUTDIR ; zip flashmap.zip prog_firehose_ddr.elf efi.bin gpt_backup0.bin \ gpt_backup1.bin gpt_main0.bin gpt_main1.bin rootfs.img \ xbl_config.elf xbl.elf rawprogram0.xml rawprogram1.xml \ patch0.xml patch1.xml flashmap.json) linux-msm-qdl-f540e59/tests/data/patch0.xml000066400000000000000000000141471521156022300205440ustar00rootroot00000000000000 linux-msm-qdl-f540e59/tests/data/patch1.xml000066400000000000000000000141671521156022300205470ustar00rootroot00000000000000 linux-msm-qdl-f540e59/tests/data/rawprogram0.xml000066400000000000000000000024261521156022300216230ustar00rootroot00000000000000 linux-msm-qdl-f540e59/tests/data/rawprogram1.xml000066400000000000000000000041541521156022300216240ustar00rootroot00000000000000 linux-msm-qdl-f540e59/tests/test_contents_selectors.c000066400000000000000000000253311521156022300230520ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #define _FILE_OFFSET_BITS 64 #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include "common.h" #include "../contents.c" bool qdl_debug; void ux_err(const char *fmt, ...) { (void)fmt; } void ux_info(const char *fmt, ...) { (void)fmt; } void ux_debug(const char *fmt, ...) { (void)fmt; } enum qdl_storage_type decode_storage_type(const char *storage) { if (!strcmp(storage, "emmc")) return QDL_STORAGE_EMMC; if (!strcmp(storage, "nand")) return QDL_STORAGE_NAND; if (!strcmp(storage, "ufs")) return QDL_STORAGE_UFS; if (!strcmp(storage, "nvme")) return QDL_STORAGE_NVME; if (!strcmp(storage, "spinor")) return QDL_STORAGE_SPINOR; return QDL_STORAGE_UNKNOWN; } const char *encode_storage_type(enum qdl_storage_type storage) { switch (storage) { case QDL_STORAGE_EMMC: return "emmc"; case QDL_STORAGE_NAND: return "nand"; case QDL_STORAGE_UFS: return "ufs"; case QDL_STORAGE_NVME: return "nvme"; case QDL_STORAGE_SPINOR: return "spinor"; case QDL_STORAGE_UNKNOWN: default: return NULL; } } int load_sahara_image(struct qdl_zip *zip, const char *filename, struct sahara_image *image) { (void)zip; (void)filename; (void)image; return -1; } void sahara_images_free(struct sahara_image *images, size_t count) { (void)images; (void)count; } int decode_sahara_config(struct sahara_image *blob, struct sahara_image *images, struct contents_filter *contents_filter) { (void)blob; (void)images; (void)contents_filter; return -1; } int program_load(struct list_head *ops, const char *program_file, bool is_nand, bool allow_missing, struct contents_filter *contents_filter, const char *incdir) { (void)ops; (void)program_file; (void)is_nand; (void)allow_missing; (void)contents_filter; (void)incdir; return -1; } int patch_load(struct list_head *ops, const char *patch_file) { (void)ops; (void)patch_file; return -1; } struct selector_fixture { struct contents contents; char *flavors[3]; }; static void add_entry(struct contents *contents, enum contents_file_type file_type, enum qdl_storage_type storage_type, const char *flavor) { struct contents_entry *entry; entry = calloc(1, sizeof(*entry)); assert_non_null(entry); entry->file_type = file_type; entry->storage_type = storage_type; entry->flavor = flavor ? strdup(flavor) : NULL; assert_true(!flavor || entry->flavor); list_append(&contents->entries, &entry->node); } static int setup_single(void **state) { struct selector_fixture *fixture; fixture = calloc(1, sizeof(*fixture)); assert_non_null(fixture); fixture->flavors[0] = "flavor_a"; list_init(&fixture->contents.entries); fixture->contents.flavors = fixture->flavors; fixture->contents.num_flavors = 1; add_entry(&fixture->contents, CONTENTS_FILE_PROGRAM, QDL_STORAGE_UFS, "flavor_a"); add_entry(&fixture->contents, CONTENTS_FILE_PROGRAM, QDL_STORAGE_UFS, NULL); add_entry(&fixture->contents, CONTENTS_FILE_PATCH, QDL_STORAGE_UFS, NULL); add_entry(&fixture->contents, CONTENTS_FILE_OTHER, QDL_STORAGE_UNKNOWN, "flavor_a"); *state = fixture; return 0; } static int setup_multi(void **state) { struct selector_fixture *fixture; fixture = calloc(1, sizeof(*fixture)); assert_non_null(fixture); fixture->flavors[0] = "flavor_a"; fixture->flavors[1] = "flavor_b"; fixture->flavors[2] = "flavor_c"; list_init(&fixture->contents.entries); fixture->contents.flavors = fixture->flavors; fixture->contents.num_flavors = 3; add_entry(&fixture->contents, CONTENTS_FILE_PROGRAM, QDL_STORAGE_UFS, "flavor_a"); add_entry(&fixture->contents, CONTENTS_FILE_PATCH, QDL_STORAGE_UFS, "flavor_a"); add_entry(&fixture->contents, CONTENTS_FILE_PROGRAM, QDL_STORAGE_SPINOR, "flavor_b"); add_entry(&fixture->contents, CONTENTS_FILE_PATCH, QDL_STORAGE_SPINOR, "flavor_b"); add_entry(&fixture->contents, CONTENTS_FILE_PROGRAM, QDL_STORAGE_SPINOR, "flavor_c"); add_entry(&fixture->contents, CONTENTS_FILE_PATCH, QDL_STORAGE_SPINOR, "flavor_c"); add_entry(&fixture->contents, CONTENTS_FILE_OTHER, QDL_STORAGE_UNKNOWN, "flavor_a"); *state = fixture; return 0; } static int teardown_fixture(void **state) { struct selector_fixture *fixture = *state; struct contents_entry *entry; struct contents_entry *next; if (!fixture) return 0; list_for_each_entry_safe(entry, next, &fixture->contents.entries, node) { list_del(&entry->node); free(entry->flavor); free(entry); } free(fixture); return 0; } static void assert_selector(struct contents_selector *selector, enum qdl_storage_type storage_type, const char *flavor) { assert_int_equal(selector->storage_type, storage_type); assert_string_equal(selector->flavor, flavor); } static void assert_decode_success(struct contents *contents, const char *pattern, int expected_count, const struct contents_selector *expected) { struct contents_selector *selectors = NULL; char *pattern_copy = NULL; int ret; int i; if (pattern) { pattern_copy = strdup(pattern); assert_non_null(pattern_copy); } ret = contents_decode_selectors(contents, pattern_copy, &selectors); assert_int_equal(ret, expected_count); assert_non_null(selectors); for (i = 0; i < expected_count; i++) assert_selector(&selectors[i], expected[i].storage_type, expected[i].flavor); free(selectors); free(pattern_copy); } static void assert_decode_failure(struct contents *contents, const char *pattern) { struct contents_selector *selectors = NULL; char *pattern_copy = NULL; int ret; if (pattern) { pattern_copy = strdup(pattern); assert_non_null(pattern_copy); } ret = contents_decode_selectors(contents, pattern_copy, &selectors); assert_int_equal(ret, -1); assert_null(selectors); free(pattern_copy); } static void test_single_autoselects_only_combination(void **state) { struct selector_fixture *fixture = *state; const struct contents_selector expected[] = { { QDL_STORAGE_UFS, "flavor_a" }, }; assert_decode_success(&fixture->contents, NULL, 1, expected); } static void test_single_accepts_storage_shorthand(void **state) { struct selector_fixture *fixture = *state; const struct contents_selector expected[] = { { QDL_STORAGE_UFS, "flavor_a" }, }; assert_decode_success(&fixture->contents, "ufs", 1, expected); } static void test_single_accepts_flavor_shorthand(void **state) { struct selector_fixture *fixture = *state; const struct contents_selector expected[] = { { QDL_STORAGE_UFS, "flavor_a" }, }; assert_decode_success(&fixture->contents, "flavor_a", 1, expected); } static void test_multi_requires_explicit_selector(void **state) { struct selector_fixture *fixture = *state; assert_decode_failure(&fixture->contents, NULL); } static void test_multi_accepts_full_multi_selector(void **state) { struct selector_fixture *fixture = *state; const struct contents_selector expected[] = { { QDL_STORAGE_UFS, "flavor_a" }, { QDL_STORAGE_SPINOR, "flavor_b" }, }; assert_decode_success(&fixture->contents, "ufs/flavor_a,spinor/flavor_b", 2, expected); } static void test_multi_accepts_storage_shorthand_when_unique(void **state) { struct selector_fixture *fixture = *state; const struct contents_selector expected[] = { { QDL_STORAGE_UFS, "flavor_a" }, }; assert_decode_success(&fixture->contents, "ufs", 1, expected); } static void test_multi_accepts_flavor_shorthand_when_unique(void **state) { struct selector_fixture *fixture = *state; const struct contents_selector expected[] = { { QDL_STORAGE_SPINOR, "flavor_b" }, }; assert_decode_success(&fixture->contents, "flavor_b", 1, expected); } static void test_multi_accepts_mixed_shorthand_selector(void **state) { struct selector_fixture *fixture = *state; const struct contents_selector expected[] = { { QDL_STORAGE_UFS, "flavor_a" }, { QDL_STORAGE_SPINOR, "flavor_b" }, }; assert_decode_success(&fixture->contents, "ufs,spinor/flavor_b", 2, expected); } static void test_multi_rejects_ambiguous_storage_shorthand(void **state) { struct selector_fixture *fixture = *state; assert_decode_failure(&fixture->contents, "spinor"); } static void test_multi_rejects_invalid_combination(void **state) { struct selector_fixture *fixture = *state; assert_decode_failure(&fixture->contents, "ufs/flavor_b"); } static void test_multi_rejects_duplicate_storage_full_selectors(void **state) { struct selector_fixture *fixture = *state; assert_decode_failure(&fixture->contents, "spinor/flavor_b,spinor/flavor_c"); } static void test_multi_rejects_duplicate_storage_after_shorthand(void **state) { struct selector_fixture *fixture = *state; assert_decode_failure(&fixture->contents, "flavor_b,spinor/flavor_c"); } static void test_multi_rejects_unknown_selector(void **state) { struct selector_fixture *fixture = *state; assert_decode_failure(&fixture->contents, "does_not_exist"); } static void test_multi_rejects_empty_selector(void **state) { struct selector_fixture *fixture = *state; assert_decode_failure(&fixture->contents, ""); } int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(test_single_autoselects_only_combination, setup_single, teardown_fixture), cmocka_unit_test_setup_teardown(test_single_accepts_storage_shorthand, setup_single, teardown_fixture), cmocka_unit_test_setup_teardown(test_single_accepts_flavor_shorthand, setup_single, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_requires_explicit_selector, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_accepts_full_multi_selector, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_accepts_storage_shorthand_when_unique, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_accepts_flavor_shorthand_when_unique, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_accepts_mixed_shorthand_selector, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_rejects_ambiguous_storage_shorthand, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_rejects_invalid_combination, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_rejects_duplicate_storage_full_selectors, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_rejects_duplicate_storage_after_shorthand, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_rejects_unknown_selector, setup_multi, teardown_fixture), cmocka_unit_test_setup_teardown(test_multi_rejects_empty_selector, setup_multi, teardown_fixture), }; return cmocka_run_group_tests(tests, NULL, NULL); } linux-msm-qdl-f540e59/tests/test_contents_xml.c000066400000000000000000000366741521156022300216630ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #define _FILE_OFFSET_BITS 64 #if defined(__APPLE__) #define _DARWIN_C_SOURCE #endif #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include #include #include static xmlDocPtr mock_xmlReadFile(const char *filename, const char *encoding, int options); #define xmlReadFile mock_xmlReadFile #include "../contents.c" #undef xmlReadFile bool qdl_debug; static int mock_load_sahara_ret = -1; static int mock_decode_sahara_ret = -1; static unsigned int mock_load_call_count; static unsigned int mock_decode_call_count; static char mock_last_load_filename[PATH_MAX]; void ux_err(const char *fmt, ...) { (void)fmt; } void ux_info(const char *fmt, ...) { (void)fmt; } void ux_debug(const char *fmt, ...) { (void)fmt; } enum qdl_storage_type decode_storage_type(const char *storage) { if (!strcmp(storage, "emmc")) return QDL_STORAGE_EMMC; if (!strcmp(storage, "nand")) return QDL_STORAGE_NAND; if (!strcmp(storage, "ufs")) return QDL_STORAGE_UFS; if (!strcmp(storage, "nvme")) return QDL_STORAGE_NVME; if (!strcmp(storage, "spinor")) return QDL_STORAGE_SPINOR; return QDL_STORAGE_UNKNOWN; } const char *encode_storage_type(enum qdl_storage_type storage) { switch (storage) { case QDL_STORAGE_EMMC: return "emmc"; case QDL_STORAGE_NAND: return "nand"; case QDL_STORAGE_UFS: return "ufs"; case QDL_STORAGE_NVME: return "nvme"; case QDL_STORAGE_SPINOR: return "spinor"; case QDL_STORAGE_UNKNOWN: default: return NULL; } } int load_sahara_image(struct qdl_zip *zip, const char *filename, struct sahara_image *image) { (void)zip; mock_load_call_count++; snprintf(mock_last_load_filename, sizeof(mock_last_load_filename), "%s", filename); if (image) { image->name = mock_last_load_filename; image->ptr = NULL; image->len = 0; } return mock_load_sahara_ret; } void sahara_images_free(struct sahara_image *images, size_t count) { (void)images; (void)count; } int decode_sahara_config(struct sahara_image *blob, struct sahara_image *images, struct contents_filter *contents_filter) { (void)blob; (void)images; (void)contents_filter; mock_decode_call_count++; return mock_decode_sahara_ret; } int program_load(struct list_head *ops, const char *program_file, bool is_nand, bool allow_missing, struct contents_filter *contents_filter, const char *incdir) { (void)ops; (void)program_file; (void)is_nand; (void)allow_missing; (void)contents_filter; (void)incdir; return -1; } int patch_load(struct list_head *ops, const char *patch_file) { (void)ops; (void)patch_file; return -1; } struct firehose_op *firehose_alloc_op(int type) { struct firehose_op *op = calloc(1, sizeof(*op)); if (!op) return NULL; op->type = type; return op; } #define TEST_ROOT "/mock" #define TEST_CONTENTS_XML TEST_ROOT "/contents.xml" #define TEST_INVALID_XML TEST_ROOT "/not-contents.xml" struct xml_fixture { const char *root; const char *contents_xml; const char *invalid_xml; }; static const char contents_xml[] = "" "" " " " flavor_a" " flavor_b" " " " " " " " ./root/" " .\\root\\" " " " programmer.xml" " common/build/" " " " " " prog_firehose.elf" " boot/" " " " " " payload.bin" " images/" " " " " " rawprogram0.xml" " ufs/${variant:raw}/" " " " " " patch0.xml" " ufs/" " " " " " spinor_rawprogram.xml" " spinor/" " " " " " ignored.bin" " ignored/" " " " " " *.bin" " wildcard/" " " " " " " ""; static const char contents_xml_device_programmer_only[] = "" "" " " " " " ./root/" " .\\root\\" " " " prog_firehose_lite.elf" " boot/" " " " " " prog_firehose_ddr.elf" " boot/" " " " " " " ""; static const char contents_xml_device_programmer_lite_only[] = "" "" " " " " " ./root/" " .\\root\\" " " " prog_firehose_lite.elf" " boot/" " " " " " " ""; static const char contents_xml_storage_only[] = "" "" " " " " " ./root/" " .\\root\\" " " " rawprogram0.xml" " common/build/ufs/" " " " " " patch0.xml" " common/build/ufs/" " " " " " " ""; static const char *mock_contents_xml_text; static const char *mock_invalid_xml_text; static xmlDocPtr mock_xmlReadFile(const char *filename, const char *encoding, int options) { const char *xml; (void)encoding; if (!strcmp(filename, TEST_CONTENTS_XML)) xml = mock_contents_xml_text; else if (!strcmp(filename, TEST_INVALID_XML)) xml = mock_invalid_xml_text; else return NULL; return xml ? xmlReadMemory(xml, strlen(xml), filename, NULL, options) : NULL; } static void reset_programmer_mocks(void) { mock_load_sahara_ret = -1; mock_decode_sahara_ret = -1; mock_load_call_count = 0; mock_decode_call_count = 0; mock_last_load_filename[0] = '\0'; } static int setup_xml_fixture(void **state) { struct xml_fixture *fixture; fixture = calloc(1, sizeof(*fixture)); assert_non_null(fixture); fixture->root = TEST_ROOT; fixture->contents_xml = TEST_CONTENTS_XML; fixture->invalid_xml = TEST_INVALID_XML; mock_contents_xml_text = contents_xml; mock_invalid_xml_text = "\n"; reset_programmer_mocks(); *state = fixture; return 0; } static int teardown_xml_fixture(void **state) { free(*state); return 0; } static void init_contents(struct contents *contents) { list_init(&contents->entries); } static void free_contents(struct contents *contents) { struct contents_entry *entry; struct contents_entry *next; size_t flavor_idx; list_for_each_entry_safe(entry, next, &contents->entries, node) { list_del(&entry->node); free(entry->filename); free(entry->flavor); free(entry); } for (flavor_idx = 0; flavor_idx < contents->num_flavors; flavor_idx++) free(contents->flavors[flavor_idx]); free(contents->flavors); } static size_t count_entries(struct contents *contents) { struct contents_entry *entry; size_t count = 0; list_for_each_entry(entry, &contents->entries, node) count++; return count; } static struct contents_entry *find_entry(struct contents *contents, const char *filename) { struct contents_entry *entry; list_for_each_entry(entry, &contents->entries, node) { if (!strcmp(entry->filename, filename)) return entry; } return NULL; } static void assert_entry(struct xml_fixture *fixture, struct contents *contents, const char *filename, enum contents_file_type file_type, enum qdl_storage_type storage_type, const char *flavor, enum firehose_type firehose_type, const char *relative_path) { struct contents_entry *entry; char expected_path[PATH_MAX]; int ret; entry = find_entry(contents, filename); assert_non_null(entry); assert_int_equal(entry->file_type, file_type); assert_int_equal(entry->storage_type, storage_type); if (flavor) assert_string_equal(entry->flavor, flavor); else assert_null(entry->flavor); assert_int_equal(entry->firehose_type, firehose_type); ret = snprintf(expected_path, sizeof(expected_path), "%s/root/%s", fixture->root, relative_path); assert_true(ret > 0 && (size_t)ret < sizeof(expected_path)); assert_string_equal(qdl_pathbuf_str(&entry->path), expected_path); } static void test_load_xml_parses_product_flavors(void **state) { struct xml_fixture *fixture = *state; struct contents contents = {}; init_contents(&contents); assert_int_equal(contents_load_xml(&contents, fixture->contents_xml), 0); assert_int_equal(contents.num_flavors, 2); assert_string_equal(contents.flavors[0], "flavor_a"); assert_string_equal(contents.flavors[1], "flavor_b"); assert_string_equal(qdl_pathbuf_str(&contents.base_dir), fixture->root); free_contents(&contents); } static void test_load_xml_parses_file_entries(void **state) { struct xml_fixture *fixture = *state; struct contents contents = {}; init_contents(&contents); assert_int_equal(contents_load_xml(&contents, fixture->contents_xml), 0); assert_int_equal(count_entries(&contents), 6); assert_entry(fixture, &contents, "programmer.xml", CONTENTS_FILE_PROGRAMMER_XML, QDL_STORAGE_UNKNOWN, NULL, 2, "common/build/programmer.xml"); assert_entry(fixture, &contents, "prog_firehose.elf", CONTENTS_FILE_DEVICE_PROGRAMMER, QDL_STORAGE_UFS, NULL, 0, "boot/prog_firehose.elf"); assert_entry(fixture, &contents, "payload.bin", CONTENTS_FILE_OTHER, QDL_STORAGE_UFS, "flavor_a", 0, "images/payload.bin"); assert_entry(fixture, &contents, "rawprogram0.xml", CONTENTS_FILE_PROGRAM, QDL_STORAGE_UFS, "flavor_a", 0, "ufs/raw/rawprogram0.xml"); assert_entry(fixture, &contents, "patch0.xml", CONTENTS_FILE_PATCH, QDL_STORAGE_UFS, NULL, 0, "ufs/patch0.xml"); assert_entry(fixture, &contents, "spinor_rawprogram.xml", CONTENTS_FILE_PROGRAM, QDL_STORAGE_SPINOR, "flavor_b", 0, "spinor/spinor_rawprogram.xml"); free_contents(&contents); } static void test_load_xml_skips_ignored_and_wildcards(void **state) { struct xml_fixture *fixture = *state; struct contents contents = {}; init_contents(&contents); assert_int_equal(contents_load_xml(&contents, fixture->contents_xml), 0); assert_null(find_entry(&contents, "ignored.bin")); assert_null(find_entry(&contents, "*.bin")); free_contents(&contents); } static void test_load_xml_rejects_non_contents_document(void **state) { struct xml_fixture *fixture = *state; struct contents contents = {}; init_contents(&contents); assert_int_equal(contents_load_xml(&contents, fixture->invalid_xml), -1); } static void test_find_programmers_prefers_programmer_xml(void **state) { struct xml_fixture *fixture = *state; struct sahara_image images[MAPPING_SZ] = {}; struct contents contents = {}; mock_load_sahara_ret = 0; mock_decode_sahara_ret = 1; init_contents(&contents); assert_int_equal(contents_load_xml(&contents, fixture->contents_xml), 0); assert_int_equal(contents_find_programmers(&contents, images), 0); assert_int_equal(mock_decode_call_count, 1); assert_int_equal(mock_load_call_count, 1); assert_non_null(strstr(mock_last_load_filename, "programmer.xml")); free_contents(&contents); } static void test_find_programmers_falls_back_to_non_lite_device_programmer(void **state) { struct xml_fixture *fixture = *state; struct sahara_image images[MAPPING_SZ] = {}; struct contents contents = {}; mock_contents_xml_text = contents_xml_device_programmer_only; mock_load_sahara_ret = 0; init_contents(&contents); assert_int_equal(contents_load_xml(&contents, fixture->contents_xml), 0); assert_int_equal(contents_find_programmers(&contents, images), 0); assert_int_equal(mock_decode_call_count, 0); assert_int_equal(mock_load_call_count, 1); assert_non_null(strstr(mock_last_load_filename, "prog_firehose_ddr.elf")); free_contents(&contents); } static void test_find_programmers_rejects_lite_only_device_programmer(void **state) { struct xml_fixture *fixture = *state; struct sahara_image images[MAPPING_SZ] = {}; struct contents contents = {}; mock_contents_xml_text = contents_xml_device_programmer_lite_only; mock_load_sahara_ret = 0; init_contents(&contents); assert_int_equal(contents_load_xml(&contents, fixture->contents_xml), 0); assert_int_equal(contents_find_programmers(&contents, images), -1); assert_int_equal(mock_decode_call_count, 0); assert_int_equal(mock_load_call_count, 0); free_contents(&contents); } static void test_decode_selectors_accepts_storage_only_contents(void **state) { struct xml_fixture *fixture = *state; struct contents_selector *selectors = NULL; struct contents contents = {}; char pattern[] = "ufs"; mock_contents_xml_text = contents_xml_storage_only; init_contents(&contents); assert_int_equal(contents_load_xml(&contents, fixture->contents_xml), 0); assert_int_equal(contents_decode_selectors(&contents, pattern, &selectors), 1); assert_non_null(selectors); assert_int_equal(selectors[0].storage_type, QDL_STORAGE_UFS); assert_null(selectors[0].flavor); free(selectors); free_contents(&contents); } int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(test_load_xml_parses_product_flavors, setup_xml_fixture, teardown_xml_fixture), cmocka_unit_test_setup_teardown(test_load_xml_parses_file_entries, setup_xml_fixture, teardown_xml_fixture), cmocka_unit_test_setup_teardown(test_load_xml_skips_ignored_and_wildcards, setup_xml_fixture, teardown_xml_fixture), cmocka_unit_test_setup_teardown(test_load_xml_rejects_non_contents_document, setup_xml_fixture, teardown_xml_fixture), cmocka_unit_test_setup_teardown(test_find_programmers_prefers_programmer_xml, setup_xml_fixture, teardown_xml_fixture), cmocka_unit_test_setup_teardown(test_find_programmers_falls_back_to_non_lite_device_programmer, setup_xml_fixture, teardown_xml_fixture), cmocka_unit_test_setup_teardown(test_find_programmers_rejects_lite_only_device_programmer, setup_xml_fixture, teardown_xml_fixture), cmocka_unit_test_setup_teardown(test_decode_selectors_accepts_storage_only_contents, setup_xml_fixture, teardown_xml_fixture), }; return cmocka_run_group_tests(tests, NULL, NULL); } linux-msm-qdl-f540e59/tests/test_flashmap.sh000066400000000000000000000025641521156022300211200ustar00rootroot00000000000000#!/bin/bash # SPDX-License-Identifier: BSD-3-Clause set -e SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" while [[ $# -gt 0 ]]; do case "$1" in --builddir) builddir="$2" shift 2 ;; *) echo "Unknown option: $1" >&2 exit 1 ;; esac done if [[ -z "${builddir}" ]]; then echo "Error: --builddir is required." >&2 exit 1 fi FLAT_BUILD=${SCRIPT_PATH}/data QDL_PATH=$builddir uname_out="$(uname -s)" case "${uname_out}" in Linux*|Darwin*) QDL=qdl ;; CYGWIN*|MINGW*|MSYS*) QDL=qdl.exe ;; *) exit 1 ;; esac DATA_SRC=${SCRIPT_PATH}/data FLAT_BUILD=${builddir}/tests/data-flashmap ${DATA_SRC}/generate_flat_build.sh "${FLAT_BUILD}" >&2 cd $FLAT_BUILD echo "1..2" n=1 fail=0 run_test() { desc="$1" shift out="$(mktemp)" err="$(mktemp)" if "$@" >"$out" 2>"$err" ; then echo "ok $n - $desc" else rc=$? echo "not ok $n - $desc" echo "# exit code: $rc" sed 's/^/# stdout: /' "$out" sed 's/^/# stderr: /' "$err" fail=1 fi n=$((n + 1)) } #echo "####### Flashing flashmap.json" run_test "flashing flashmap.json" ${QDL_PATH}/${QDL} --dry-run flash flashmap.json #echo "####### Flashing flashmap.zip" run_test "flashing flashmap.zip" ${QDL_PATH}/${QDL} --dry-run flash flashmap.zip exit $fail linux-msm-qdl-f540e59/tests/test_pathbuf.c000066400000000000000000000114051521156022300205600ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include "pathbuf.h" static void init_path(struct pathbuf *path, const char *s, bool set_len) { size_t len = strlen(s); assert_true(len < sizeof(path->buf)); memcpy(path->buf, s, len + 1); path->len = set_len ? len : 0; } static void assert_dirname(const char *in, const char *out, bool set_len) { struct pathbuf path; init_path(&path, in, set_len); qdl_pathbuf_dirname(&path); assert_string_equal(path.buf, out); assert_int_equal(path.len, strlen(out)); } static void test_dirname_posix_root(void **state) { (void)state; assert_dirname("/", "/", true); assert_dirname("/", "/", false); } static void test_dirname_posix_absolute(void **state) { (void)state; assert_dirname("/foo", "/", true); assert_dirname("/foo/bar", "/foo", true); assert_dirname("/foo/bar/", "/foo", true); } static void test_dirname_relative(void **state) { (void)state; assert_dirname("foo", "foo", true); assert_dirname("foo/bar", "foo", true); assert_dirname("foo/bar/", "foo", true); } static void test_reset_and_dup(void **state) { struct pathbuf src; struct pathbuf dst; (void)state; init_path(&src, "/tmp/example", true); qdl_pathbuf_dup(&dst, &src); assert_string_equal(dst.buf, src.buf); assert_int_equal(dst.len, src.len); qdl_pathbuf_reset(&dst); assert_string_equal(dst.buf, ""); assert_int_equal(dst.len, 0); qdl_pathbuf_reset(NULL); } static void test_push_basic_behavior(void **state) { struct pathbuf path = {0}; int ret; (void)state; ret = qdl_pathbuf_push(&path, "foo"); assert_int_equal(ret, 0); assert_string_equal(path.buf, "foo"); assert_int_equal(path.len, strlen("foo")); ret = qdl_pathbuf_push(&path, "bar"); assert_int_equal(ret, 0); assert_string_equal(path.buf, "foo/bar"); assert_int_equal(path.len, strlen("foo/bar")); ret = qdl_pathbuf_push(&path, ""); assert_int_equal(ret, 0); assert_string_equal(path.buf, "foo/bar"); assert_int_equal(path.len, strlen("foo/bar")); ret = qdl_pathbuf_push(&path, "./baz"); assert_int_equal(ret, 0); assert_string_equal(path.buf, "foo/bar/baz"); assert_int_equal(path.len, strlen("foo/bar/baz")); #ifdef _WIN32 ret = qdl_pathbuf_push(&path, ".\\qux"); assert_int_equal(ret, 0); assert_string_equal(path.buf, "foo/bar/baz/qux"); assert_int_equal(path.len, strlen("foo/bar/baz/qux")); ret = qdl_pathbuf_push(&path, "nested\\leaf"); assert_int_equal(ret, 0); assert_string_equal(path.buf, "foo/bar/baz/qux/nested/leaf"); assert_int_equal(path.len, strlen("foo/bar/baz/qux/nested/leaf")); ret = qdl_pathbuf_push(&path, "C:/abs"); assert_int_equal(ret, 0); assert_string_equal(path.buf, "C:/abs"); assert_int_equal(path.len, strlen("C:/abs")); #else ret = qdl_pathbuf_push(&path, "/abs"); assert_int_equal(ret, 0); assert_string_equal(path.buf, "/abs"); assert_int_equal(path.len, strlen("/abs")); #endif } static void test_push_invalid_args(void **state) { struct pathbuf path = {0}; (void)state; assert_int_equal(qdl_pathbuf_push(NULL, "foo"), -EINVAL); assert_int_equal(qdl_pathbuf_push(&path, NULL), -EINVAL); } static void test_push_overflow_keeps_state(void **state) { struct pathbuf path = {0}; char before[PATH_MAX]; size_t max_prefix_len; int ret; (void)state; max_prefix_len = sizeof(path.buf) - 1; memset(path.buf, 'a', max_prefix_len); path.buf[max_prefix_len] = '\0'; path.len = max_prefix_len; memcpy(before, path.buf, sizeof(before)); ret = qdl_pathbuf_push(&path, "b"); assert_int_equal(ret, -ENAMETOOLONG); assert_string_equal(path.buf, before); assert_int_equal(path.len, max_prefix_len); } #ifdef _WIN32 static void test_dirname_windows_drive(void **state) { (void)state; assert_dirname("C:\\", "C:\\", true); assert_dirname("C:\\foo", "C:\\", true); assert_dirname("C:\\foo\\bar", "C:\\foo", true); assert_dirname("C:/foo/bar", "C:/foo", true); } static void test_dirname_windows_unc(void **state) { (void)state; assert_dirname("\\\\server\\share", "\\\\server\\share", true); assert_dirname("\\\\server\\share\\dir", "\\\\server\\share", true); assert_dirname("\\\\server\\share\\dir\\sub", "\\\\server\\share\\dir", true); } #endif int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_dirname_posix_root), cmocka_unit_test(test_dirname_posix_absolute), cmocka_unit_test(test_dirname_relative), cmocka_unit_test(test_reset_and_dup), cmocka_unit_test(test_push_basic_behavior), cmocka_unit_test(test_push_invalid_args), cmocka_unit_test(test_push_overflow_keeps_state), #ifdef _WIN32 cmocka_unit_test(test_dirname_windows_drive), cmocka_unit_test(test_dirname_windows_unc), #endif }; return cmocka_run_group_tests(tests, NULL, NULL); } linux-msm-qdl-f540e59/tests/test_program_load_xml.c000066400000000000000000000233131521156022300224560ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause #define _FILE_OFFSET_BITS 64 #if defined(__APPLE__) #define _DARWIN_C_SOURCE #endif #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "file.h" #include "firehose.h" #include "list.h" #include "pathbuf.h" #include "program.h" #include "qdl.h" #include "sparse.h" #include "common.h" #define TEST_INCDIR "/mock/incdir" #define TEST_XMLDIR "/mock/xml" #define TEST_PROGRAM_FILE TEST_XMLDIR "/program.xml" #define TEST_PAYLOAD "payload.bin" #define TEST_INCDIR_PAYLOAD TEST_INCDIR "/" TEST_PAYLOAD #define TEST_XMLDIR_PAYLOAD TEST_XMLDIR "/" TEST_PAYLOAD #ifdef _WIN32 const char *__progname = "test_program_load_xml"; #endif bool qdl_debug; static const char *existing_paths[4]; static void set_existing_paths(const char *path0, const char *path1, const char *path2) { existing_paths[0] = path0; existing_paths[1] = path1; existing_paths[2] = path2; existing_paths[3] = NULL; } static bool mock_path_exists(const char *path) { for (size_t i = 0; existing_paths[i]; i++) { if (!strcmp(path, existing_paths[i])) return true; } return false; } int mock_access(const char *path, int mode) { assert_int_equal(mode, F_OK); if (mock_path_exists(path)) return 0; errno = ENOENT; return -1; } int qdl_file_open(struct qdl_zip *qzip, const char *filename, struct qdl_file *file) { assert_null(qzip); if (!mock_path_exists(filename)) return -1; file->type = QDL_FILE_TYPE_POSIX; file->size = 1; file->fd = -1; file->zip_file = NULL; return 0; } void qdl_file_close(struct qdl_file *file) { file->type = QDL_FILE_TYPE_UNKNOWN; } void *qdl_file_load(struct qdl_file *file, size_t *len) { (void)file; (void)len; return NULL; } size_t qdl_file_getsize(struct qdl_file *file) { return file->size; } off_t qdl_file_seek(struct qdl_file *file, off_t offset, int whence) { (void)file; (void)offset; (void)whence; return -1; } ssize_t qdl_file_read(struct qdl_file *file, void *buf, size_t len) { (void)file; (void)buf; (void)len; return -1; } struct qdl_zip *qdl_zip_get(struct qdl_zip *qzip) { return qzip; } void qdl_zip_put(struct qdl_zip *qzip) { (void)qzip; } int sparse_header_parse(struct qdl_file *file, sparse_header_t *sparse_header) { (void)file; (void)sparse_header; return -1; } int sparse_chunk_header_parse(struct qdl_file *file, sparse_header_t *sparse_header, uint64_t *chunk_size, uint32_t *value, off_t *offset) { (void)file; (void)sparse_header; (void)chunk_size; (void)value; (void)offset; return -1; } void ux_init(void) { } void ux_err(const char *fmt, ...) { (void)fmt; } void ux_info(const char *fmt, ...) { (void)fmt; } void ux_log(const char *fmt, ...) { (void)fmt; } void ux_debug(const char *fmt, ...) { (void)fmt; } void ux_progress(const char *fmt, unsigned int value, unsigned int size, ...) { (void)fmt; (void)value; (void)size; } struct firehose_op *firehose_alloc_op(int type) { struct firehose_op *op; op = calloc(1, sizeof(*op)); if (!op) return NULL; op->type = type; return op; } void firehose_free_ops(struct list_head *ops) { struct firehose_op *next; struct firehose_op *op; list_for_each_entry_safe(op, next, ops, node) { list_del(&op->node); qdl_zip_put(op->zip); free((void *)op->filename); free((void *)op->label); free((void *)op->start_sector); free((void *)op->gpt_partition); free((void *)op->value); free((void *)op->what); free(op); } } int contents_resolve_path(struct contents_filter *filter, const char *filename, struct pathbuf *path) { (void)filter; (void)filename; (void)path; return 0; } static xmlDoc *build_program_doc(const char *filename) { char xml[1024]; int ret; ret = snprintf(xml, sizeof(xml), "" "" "", filename); assert_true(ret > 0 && (size_t)ret < sizeof(xml)); return xmlReadMemory(xml, ret, "program.xml", NULL, 0); } static struct firehose_op *get_only_op(struct list_head *ops) { struct firehose_op *op; struct list_head *it; size_t count = 0; list_for_each(it, ops) count++; if (count != 1) fail_msg("expected exactly one firehose op, got %zu", count); op = list_entry_first(ops, struct firehose_op, node); if (op->type != FIREHOSE_OP_PROGRAM) fail_msg("expected firehose op type PROGRAM, got %d", op->type); return op; } static void test_incdir_takes_precedence(void **state) { struct firehose_op *op; struct list_head ops = LIST_INIT(ops); xmlDoc *doc; int ret; (void)state; set_existing_paths(TEST_INCDIR_PAYLOAD, TEST_XMLDIR_PAYLOAD, NULL); doc = build_program_doc(TEST_PAYLOAD); if (!doc) fail_msg("failed to parse synthetic program XML"); ret = program_load_xml(&ops, doc, NULL, TEST_PROGRAM_FILE, false, false, NULL, TEST_INCDIR); if (ret) fail_msg("incdir payload should be accepted, got %d", ret); op = get_only_op(&ops); if (!op->filename || strcmp(op->filename, TEST_INCDIR_PAYLOAD)) fail_msg("incdir should take precedence, got '%s'", op->filename ? op->filename : "(null)"); firehose_free_ops(&ops); xmlFreeDoc(doc); } static void test_program_xml_directory_is_used(void **state) { struct firehose_op *op; struct list_head ops = LIST_INIT(ops); xmlDoc *doc; int ret; (void)state; set_existing_paths(TEST_XMLDIR_PAYLOAD, NULL, NULL); doc = build_program_doc(TEST_PAYLOAD); if (!doc) fail_msg("failed to parse synthetic program XML"); ret = program_load_xml(&ops, doc, NULL, TEST_PROGRAM_FILE, false, false, NULL, TEST_INCDIR); if (ret) fail_msg("XML-adjacent payload should be accepted, got %d", ret); op = get_only_op(&ops); if (!op->filename || strcmp(op->filename, TEST_XMLDIR_PAYLOAD)) fail_msg("program XML directory should be used, got '%s'", op->filename ? op->filename : "(null)"); firehose_free_ops(&ops); xmlFreeDoc(doc); } static void test_program_xml_directory_takes_precedence_over_current_directory(void **state) { struct firehose_op *op; struct list_head ops = LIST_INIT(ops); xmlDoc *doc; int ret; (void)state; set_existing_paths(TEST_XMLDIR_PAYLOAD, TEST_PAYLOAD, NULL); doc = build_program_doc(TEST_PAYLOAD); if (!doc) fail_msg("failed to parse synthetic program XML"); ret = program_load_xml(&ops, doc, NULL, TEST_PROGRAM_FILE, false, false, NULL, TEST_INCDIR); if (ret) fail_msg("XML-adjacent payload should beat current-directory fallback, got %d", ret); op = get_only_op(&ops); if (!op->filename || strcmp(op->filename, TEST_XMLDIR_PAYLOAD)) fail_msg("program XML directory should take precedence over current directory, got '%s'", op->filename ? op->filename : "(null)"); firehose_free_ops(&ops); xmlFreeDoc(doc); } static void test_current_directory_is_used_when_path_resolution_misses(void **state) { struct firehose_op *op; struct list_head ops = LIST_INIT(ops); xmlDoc *doc; int ret; (void)state; set_existing_paths(TEST_PAYLOAD, NULL, NULL); doc = build_program_doc(TEST_PAYLOAD); if (!doc) fail_msg("failed to parse synthetic program XML"); /* Compatibility fallback: leave the original relative filename intact. */ ret = program_load_xml(&ops, doc, NULL, TEST_PROGRAM_FILE, false, false, NULL, TEST_INCDIR); if (ret) fail_msg("relative fallback payload should be accepted, got %d", ret); op = get_only_op(&ops); if (!op->filename || strcmp(op->filename, TEST_PAYLOAD)) fail_msg("unresolved relative filename should be preserved, got '%s'", op->filename ? op->filename : "(null)"); firehose_free_ops(&ops); xmlFreeDoc(doc); } static void test_missing_file_fails_without_allow_missing(void **state) { struct list_head ops = LIST_INIT(ops); xmlDoc *doc; int ret; (void)state; set_existing_paths(NULL, NULL, NULL); doc = build_program_doc(TEST_PAYLOAD); if (!doc) fail_msg("failed to parse synthetic program XML"); ret = program_load_xml(&ops, doc, NULL, TEST_PROGRAM_FILE, false, false, NULL, TEST_INCDIR); if (ret != -1) fail_msg("missing payload should fail without allow_missing, got %d", ret); if (!list_empty(&ops)) fail_msg("missing payload without allow_missing should not append firehose ops"); xmlFreeDoc(doc); } static void test_missing_file_is_tolerated_with_allow_missing(void **state) { struct firehose_op *op; struct list_head ops = LIST_INIT(ops); xmlDoc *doc; int ret; (void)state; set_existing_paths(NULL, NULL, NULL); doc = build_program_doc(TEST_PAYLOAD); if (!doc) fail_msg("failed to parse synthetic program XML"); ret = program_load_xml(&ops, doc, NULL, TEST_PROGRAM_FILE, false, true, NULL, TEST_INCDIR); if (ret) fail_msg("missing payload should be tolerated with allow_missing, got %d", ret); op = get_only_op(&ops); if (op->filename) fail_msg("allow_missing should clear the missing program filename, got '%s'", op->filename); firehose_free_ops(&ops); xmlFreeDoc(doc); } int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_incdir_takes_precedence), cmocka_unit_test(test_program_xml_directory_is_used), cmocka_unit_test(test_program_xml_directory_takes_precedence_over_current_directory), cmocka_unit_test(test_current_directory_is_used_when_path_resolution_misses), cmocka_unit_test(test_missing_file_fails_without_allow_missing), cmocka_unit_test(test_missing_file_is_tolerated_with_allow_missing), }; return cmocka_run_group_tests(tests, NULL, NULL); } linux-msm-qdl-f540e59/tests/test_vip_generation.sh000077500000000000000000000035131521156022300223340ustar00rootroot00000000000000#!/bin/bash # SPDX-License-Identifier: BSD-3-Clause set -e SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" while [[ $# -gt 0 ]]; do case "$1" in --builddir) builddir="$2" shift 2 ;; *) echo "Unknown option: $1" >&2 exit 1 ;; esac done if [[ -z "${builddir}" ]]; then echo "Error: --builddir is required." >&2 exit 1 fi DATA_SRC=${SCRIPT_PATH}/data FLAT_BUILD=${builddir}/tests/data-vip # Generate test fixtures in the build directory ${DATA_SRC}/generate_flat_build.sh "${FLAT_BUILD}" cleanup() { rm -rf "${FLAT_BUILD}" } trap cleanup EXIT QDL_PATH=$builddir VIP_PATH=${FLAT_BUILD}/vip EXPECTED_DIGEST="3ca3c745c7bf60d9f51626ad819dce1e14788bc094c258da6656b0a469afbf15" VIP_TABLE_FILE=${VIP_PATH}/DigestsToSign.bin uname_out="$(uname -s)" case "${uname_out}" in Linux*|Darwin*) QDL=qdl ;; CYGWIN*|MINGW*|MSYS*) QDL=qdl.exe ;; *) exit 1 ;; esac mkdir -p $VIP_PATH cd $FLAT_BUILD ${QDL_PATH}/${QDL} --dry-run --create-digests=${VIP_PATH} \ prog_firehose_ddr.elf rawprogram*.xml patch*.xml if command -v sha256sum >/dev/null 2>&1; then shacmd="sha256sum" elif command -v shasum >/dev/null 2>&1; then shacmd="shasum -a 256" else echo "No SHA-256 checksum tool found (need 'sha256sum' or 'shasum')" exit 1 fi actual_digest=`${shacmd} "${VIP_TABLE_FILE}" | cut -d ' ' -f1` if [ "$actual_digest" != "${EXPECTED_DIGEST}" ]; then echo "Expected SHA256 digest of ${VIP_TABLE_FILE} file is ${EXPECTED_DIGEST}" echo "Calculated SHA256 digest of ${VIP_TABLE_FILE} file is $actual_digest" echo "VIP table folder contents:" ls -la ${VIP_PATH} exit 1 fi echo "VIP tables are generated successfully and validated" rm -r ${VIP_PATH}/*.bin rmdir ${VIP_PATH} linux-msm-qdl-f540e59/ufs.c000066400000000000000000000177571521156022300155430ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2018, The Linux Foundation. All rights reserved. */ #include #include #include #include #include #include #include #include #include "ufs.h" #include "qdl.h" #include "list.h" #include "patch.h" struct ufs_common *ufs_common_p; struct ufs_epilogue *ufs_epilogue_p; static struct list_head ufs_bodies = LIST_INIT(ufs_bodies); static const char notice_bconfigdescrlock[] = "\n" "Please pay attention that UFS provisioning is irreversible (OTP) operation unless parameter bConfigDescrLock = 0.\n" "In order to prevent unintentional device locking the tool has the following safety:\n\n" " if you REALLY intend to perform OTP, please ensure that your XML includes property\n" " bConfigDescrLock = 1 AND provide command line parameter --finalize-provisioning.\n\n" " Unless you intend to lock your device, please set bConfigDescrLock = 0 in your XML\n" " and don't use command line parameter --finalize-provisioning.\n\n" "In case of mismatch between CL and XML provisioning is not performed.\n\n"; bool ufs_need_provisioning(void) { return !!ufs_epilogue_p; } struct ufs_common *ufs_parse_common_params(xmlNode *node, bool finalize_provisioning __unused) { struct ufs_common *result; int errors; result = calloc(1, sizeof(struct ufs_common)); errors = 0; result->bNumberLU = attr_as_unsigned(node, "bNumberLU", &errors); result->bBootEnable = !!attr_as_unsigned(node, "bBootEnable", &errors); result->bDescrAccessEn = !!attr_as_unsigned(node, "bDescrAccessEn", &errors); result->bInitPowerMode = attr_as_unsigned(node, "bInitPowerMode", &errors); result->bHighPriorityLUN = attr_as_unsigned(node, "bHighPriorityLUN", &errors); result->bSecureRemovalType = attr_as_unsigned(node, "bSecureRemovalType", &errors); result->bInitActiveICCLevel = attr_as_unsigned(node, "bInitActiveICCLevel", &errors); result->wPeriodicRTCUpdate = attr_as_unsigned(node, "wPeriodicRTCUpdate", &errors); result->bConfigDescrLock = !!attr_as_unsigned(node, "bConfigDescrLock", &errors); if (errors) { ux_err("errors while parsing UFS common tag\n"); free(result); return NULL; } /* These parameters are optional */ errors = 0; result->bWriteBoosterBufferPreserveUserSpaceEn = !!attr_as_unsigned(node, "bWriteBoosterBufferPreserveUserSpaceEn", &errors); result->bWriteBoosterBufferType = !!attr_as_unsigned(node, "bWriteBoosterBufferType", &errors); result->shared_wb_buffer_size_in_kb = attr_as_unsigned(node, "shared_wb_buffer_size_in_kb", &errors); result->wb = !errors; return result; } struct ufs_body *ufs_parse_body(xmlNode *node) { struct ufs_body *result; int errors; result = calloc(1, sizeof(struct ufs_body)); errors = 0; result->LUNum = attr_as_unsigned(node, "LUNum", &errors); result->bLUEnable = !!attr_as_unsigned(node, "bLUEnable", &errors); result->bBootLunID = attr_as_unsigned(node, "bBootLunID", &errors); result->size_in_kb = attr_as_unsigned(node, "size_in_kb", &errors); result->bDataReliability = attr_as_unsigned(node, "bDataReliability", &errors); result->bLUWriteProtect = attr_as_unsigned(node, "bLUWriteProtect", &errors); result->bMemoryType = attr_as_unsigned(node, "bMemoryType", &errors); result->bLogicalBlockSize = attr_as_unsigned(node, "bLogicalBlockSize", &errors); result->bProvisioningType = attr_as_unsigned(node, "bProvisioningType", &errors); result->wContextCapabilities = attr_as_unsigned(node, "wContextCapabilities", &errors); result->desc = attr_as_string(node, "desc", &errors); if (errors) { ux_err("errors while parsing UFS body tag\n"); free(result); return NULL; } return result; } struct ufs_epilogue *ufs_parse_epilogue(xmlNode *node) { struct ufs_epilogue *result; int errors = 0; result = calloc(1, sizeof(struct ufs_epilogue)); result->LUNtoGrow = attr_as_unsigned(node, "LUNtoGrow", &errors); if (errors) { ux_err("errors while parsing UFS epilogue tag\n"); free(result); return NULL; } return result; } int ufs_load(const char *ufs_file, bool finalize_provisioning) { xmlNode *node; xmlNode *root; xmlDoc *doc; int retval = 0; struct ufs_body *ufs_body_tmp; struct ufs_body *ufs_body; if (ufs_common_p) { ux_err("Only one UFS provisioning XML allowed, \"%s\" ignored\n", ufs_file); return -EEXIST; } doc = xmlReadFile(ufs_file, NULL, 0); if (!doc) { ux_err("failed to parse ufs-type file \"%s\"\n", ufs_file); return -EINVAL; } root = xmlDocGetRootElement(doc); for (node = root->children; node ; node = node->next) { if (node->type != XML_ELEMENT_NODE) continue; if (xmlStrcmp(node->name, (xmlChar *)"ufs")) { ux_err("unrecognized tag \"%s\" in ufs-type file \"%s\", ignoring\n", ufs_file, node->name); continue; } if (xmlGetProp(node, (xmlChar *)"bNumberLU")) { if (!ufs_common_p) { ufs_common_p = ufs_parse_common_params(node, finalize_provisioning); } else { ux_err("multiple UFS common tags found in \"%s\"\n", ufs_file); retval = -EINVAL; break; } if (!ufs_common_p) { ux_err("invalid UFS common tag found in \"%s\"\n", ufs_file); retval = -EINVAL; break; } } else if (xmlGetProp(node, (xmlChar *)"LUNum")) { ufs_body_tmp = ufs_parse_body(node); if (ufs_body_tmp) { list_append(&ufs_bodies, &ufs_body_tmp->node); } else { ux_err("invalid UFS body tag found in \"%s\"\n", ufs_file); retval = -EINVAL; break; } } else if (xmlGetProp(node, (xmlChar *)"commit")) { if (!ufs_epilogue_p) { ufs_epilogue_p = ufs_parse_epilogue(node); if (ufs_epilogue_p) continue; } else { ux_err("multiple UFS finalizing tags found in \"%s\"\n", ufs_file); retval = -EINVAL; break; } if (!ufs_epilogue_p) { ux_err("invalid UFS finalizing tag found in \"%s\"\n", ufs_file); retval = -EINVAL; break; } } else { ux_err("unknown tag found in ufs-type file \"%s\"\n", ufs_file); retval = -EINVAL; break; } } xmlFreeDoc(doc); if (!retval && (!ufs_common_p || list_empty(&ufs_bodies) || !ufs_epilogue_p)) { ux_err("incomplete UFS provisioning information in \"%s\"\n", ufs_file); retval = -EINVAL; } if (retval) { if (ufs_common_p) { free(ufs_common_p); } list_for_each_entry_safe(ufs_body, ufs_body_tmp, &ufs_bodies, node) { free(ufs_body); } if (ufs_epilogue_p) { free(ufs_epilogue_p); } return retval; } if (!finalize_provisioning != !ufs_common_p->bConfigDescrLock) { ux_err("UFS provisioning value bConfigDescrLock %d in file \"%s\" don't match command line parameter --finalize-provisioning %d\n", ufs_common_p->bConfigDescrLock, ufs_file, finalize_provisioning); ux_err(notice_bconfigdescrlock); return -EINVAL; } return 0; } int ufs_provisioning_execute(struct qdl_device *qdl, int (*apply_ufs_common)(struct qdl_device *, struct ufs_common*), int (*apply_ufs_body)(struct qdl_device *, struct ufs_body*), int (*apply_ufs_epilogue)(struct qdl_device *, struct ufs_epilogue*, bool)) { int ret; struct ufs_body *body; if (ufs_common_p->bConfigDescrLock) { int i; ux_info("WARNING: irreversible provisioning will start in 5s"); for (i = 5; i > 0; i--) { ux_info(".\a"); fflush(stdout); sleep(1); } ux_info("\n"); } // Just ask a target to check the XML w/o real provisioning ret = apply_ufs_common(qdl, ufs_common_p); if (ret) return ret; list_for_each_entry(body, &ufs_bodies, node) { ret = apply_ufs_body(qdl, body); if (ret) return ret; } ret = apply_ufs_epilogue(qdl, ufs_epilogue_p, false); if (ret) { ux_err("UFS provisioning impossible, provisioning XML may be corrupted\n"); return ret; } // Real provisioning -- target didn't refuse a given XML ret = apply_ufs_common(qdl, ufs_common_p); if (ret) return ret; list_for_each_entry(body, &ufs_bodies, node) { ret = apply_ufs_body(qdl, body); if (ret) return ret; } return apply_ufs_epilogue(qdl, ufs_epilogue_p, true); } linux-msm-qdl-f540e59/ufs.h000066400000000000000000000027221521156022300155320ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) 2018, The Linux Foundation. All rights reserved. */ #ifndef __UFS_H__ #define __UFS_H__ #include #include "list.h" struct qdl_device; struct ufs_common { unsigned int bNumberLU; bool bBootEnable; bool bDescrAccessEn; unsigned int bInitPowerMode; unsigned int bHighPriorityLUN; unsigned int bSecureRemovalType; unsigned int bInitActiveICCLevel; unsigned int wPeriodicRTCUpdate; bool bConfigDescrLock; bool wb; bool bWriteBoosterBufferPreserveUserSpaceEn; bool bWriteBoosterBufferType; unsigned int shared_wb_buffer_size_in_kb; }; struct ufs_body { unsigned int LUNum; bool bLUEnable; unsigned int bBootLunID; unsigned int size_in_kb; unsigned int bDataReliability; unsigned int bLUWriteProtect; unsigned int bMemoryType; unsigned int bLogicalBlockSize; unsigned int bProvisioningType; unsigned int wContextCapabilities; const char *desc; struct list_head node; }; struct ufs_epilogue { unsigned int LUNtoGrow; bool commit; }; int ufs_load(const char *ufs_file, bool finalize_provisioning); int ufs_provisioning_execute(struct qdl_device *qdl, int (*apply_ufs_common)(struct qdl_device *qdl, struct ufs_common *ufs), int (*apply_ufs_body)(struct qdl_device *qdl, struct ufs_body *ufs), int (*apply_ufs_epilogue)(struct qdl_device *qdl, struct ufs_epilogue *ufs, bool commit)); bool ufs_need_provisioning(void); #endif linux-msm-qdl-f540e59/usb.c000066400000000000000000000312271521156022300155230ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause #include #include #include #include #include #include #include #include #include #include "oscompat.h" #include "qdl.h" #define DEFAULT_OUT_CHUNK_SIZE (1024 * 1024) struct qdl_device_usb { struct qdl_device base; struct libusb_device_handle *usb_handle; int in_ep; int out_ep; size_t in_maxpktsize; size_t out_maxpktsize; size_t out_chunk_size; }; /* * libusb commit f0cce43f882d ("core: Fix definition and use of enum * libusb_transfer_type") split transfer type and endpoint transfer types. * Provide an alias in order to make the code compile with the old (non-split) * definition. */ #ifndef LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK #define LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK LIBUSB_TRANSFER_TYPE_BULK #endif static bool usb_read_serial(struct libusb_device_handle *handle, const struct libusb_device_descriptor *desc, char *out, size_t out_len) { char buf[128]; char *p; int ret; ret = libusb_get_string_descriptor_ascii(handle, desc->iProduct, (unsigned char *)buf, sizeof(buf)); if (ret < 0) { warnx("failed to read iProduct descriptor: %s", libusb_strerror(ret)); return false; } p = strstr(buf, "_SN:"); if (!p) return false; p += strlen("_SN:"); p[strcspn(p, " _")] = '\0'; snprintf(out, out_len, "%s", p); return true; } static bool usb_match_usb_serial(struct libusb_device_handle *handle, const char *serial, const struct libusb_device_descriptor *desc) { char buf[64]; /* If no serial is requested, consider everything a match */ if (!serial) return true; if (!usb_read_serial(handle, desc, buf, sizeof(buf))) return false; return strcmp(buf, serial) == 0; } static int usb_try_open(libusb_device *dev, struct qdl_device_usb *qdl, const char *serial) { const struct libusb_endpoint_descriptor *endpoint; const struct libusb_interface_descriptor *ifc; struct libusb_config_descriptor *config; struct libusb_device_descriptor desc; struct libusb_device_handle *handle; size_t out_size; size_t in_size; uint8_t type; int ret; int out; int in; int k; int l; ret = libusb_get_device_descriptor(dev, &desc); if (ret < 0) { warnx("failed to get USB device descriptor"); return -1; } /* Consider only devices with vid 0x0506 and known product id */ if (desc.idVendor != 0x05c6) return 0; if (desc.idProduct != 0x9008 && desc.idProduct != 0x900e && desc.idProduct != 0x901d) return 0; ret = libusb_get_active_config_descriptor(dev, &config); if (ret < 0) { warnx("failed to acquire USB device's active config descriptor"); return -1; } for (k = 0; k < config->bNumInterfaces; k++) { ifc = config->interface[k].altsetting; in = -1; out = -1; in_size = 0; out_size = 0; for (l = 0; l < ifc->bNumEndpoints; l++) { endpoint = &ifc->endpoint[l]; type = endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK; if (type != LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK) continue; if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { in = endpoint->bEndpointAddress; in_size = endpoint->wMaxPacketSize; } else { out = endpoint->bEndpointAddress; out_size = endpoint->wMaxPacketSize; } } if (ifc->bInterfaceClass != 0xff) continue; if (ifc->bInterfaceSubClass != 0xff) continue; /* bInterfaceProtocol of 0xff, 0x10 and 0x11 has been seen */ if (ifc->bInterfaceProtocol != 0xff && ifc->bInterfaceProtocol != 16 && ifc->bInterfaceProtocol != 17) continue; ret = libusb_open(dev, &handle); if (ret < 0) { warnx("unable to open USB device"); continue; } if (!usb_match_usb_serial(handle, serial, &desc)) { libusb_close(handle); continue; } libusb_detach_kernel_driver(handle, ifc->bInterfaceNumber); ret = libusb_claim_interface(handle, ifc->bInterfaceNumber); if (ret < 0) { warnx("failed to claim USB interface"); libusb_close(handle); continue; } qdl->usb_handle = handle; qdl->in_ep = in; qdl->out_ep = out; qdl->in_maxpktsize = in_size; qdl->out_maxpktsize = out_size; if (qdl->out_chunk_size && qdl->out_chunk_size % out_size) { ux_err("WARNING: requested out-chunk-size must be multiple of the device's wMaxPacketSize %ld, using %ld\n", out_size, out_size); qdl->out_chunk_size = out_size; } else if (!qdl->out_chunk_size) { qdl->out_chunk_size = DEFAULT_OUT_CHUNK_SIZE; } ux_debug("USB: using out-chunk-size of %ld\n", qdl->out_chunk_size); break; } libusb_free_config_descriptor(config); return !!qdl->usb_handle; } static bool usb_is_edl_pid(uint16_t pid) { return pid == 0x9008 || pid == 0x900e || pid == 0x901d; } /* * try_usb_open() - one libusb scan-and-open pass. * * Used as the single iteration unit by both usb_open() (the --backend usb * wait loop) and auto_open() (the unified Windows libusb+QUD wait loop). * * On success, populates @qdl and emits the "Flashing/Collecting device" * UX line; the caller need not log anything. * * Returns: * 0 - device opened, @qdl is ready * -ENODEV - no EDL device currently visible * -EBUSY - EDL device(s) visible but none could be opened * (typically: another driver, usually the Qualcomm * QDLoader 9008 driver behind the QUD backend, has * claimed the USB interface) * -EIO - libusb itself failed (init/get_device_list) * * @visible_out, when non-NULL, receives the number of EDL devices that * libusb saw on this pass, so callers that loop can print transition * diagnostics without re-enumerating. * * Does its own libusb_init()/libusb_exit() so the next call sees a * fresh enumeration (libusb's udev-less backends otherwise cache the * list, which would mask newly-attached devices in containerised setups). */ int try_usb_open(struct qdl_device *qdl, const char *serial, int *visible_out) { struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base); struct libusb_device_descriptor desc; struct libusb_device **devs; struct libusb_device *dev; char matched_serial[64]; uint16_t matched_pid = 0; bool found = false; int visible = 0; ssize_t n; int ret; int i; if (visible_out) *visible_out = 0; ret = libusb_init(NULL); if (ret < 0) { ux_err("failed to initialize libusb: %s\n", libusb_strerror(ret)); return -EIO; } n = libusb_get_device_list(NULL, &devs); if (n < 0) { ux_err("failed to list USB devices: %s\n", libusb_strerror(n)); libusb_exit(NULL); return -EIO; } for (i = 0; devs[i]; i++) { dev = devs[i]; if (libusb_get_device_descriptor(dev, &desc) < 0) continue; if (desc.idVendor != 0x05c6 || !usb_is_edl_pid(desc.idProduct)) continue; visible++; ret = usb_try_open(dev, qdl_usb, serial); if (ret == 1) { found = true; matched_pid = desc.idProduct; if (!usb_read_serial(qdl_usb->usb_handle, &desc, matched_serial, sizeof(matched_serial))) matched_serial[0] = '\0'; break; } } if (visible_out) *visible_out = visible; if (found) { const char *action = matched_pid == 0x900e ? "Collecting crash dump from" : "Flashing"; libusb_free_device_list(devs, 1); if (matched_serial[0]) ux_info("%s device (PID 0x%04x, serial: %s)\n", action, matched_pid, matched_serial); else ux_info("%s device (PID 0x%04x)\n", action, matched_pid); return 0; } libusb_free_device_list(devs, 1); libusb_exit(NULL); return visible == 0 ? -ENODEV : -EBUSY; } static int usb_open(struct qdl_device *qdl, const char *serial) { int visible_prev = -1; int visible; int ret; for (;;) { ret = try_usb_open(qdl, serial, &visible); if (ret == 0) return 0; if (ret == -EIO) return -1; if (visible != visible_prev) { if (visible == 0) { ux_info("Waiting for EDL device\n"); } else if (serial) { ux_info("%d EDL device(s) visible, none match serial \"%s\"\n", visible, serial); } else { ux_info("%d EDL device(s) visible, none could be opened\n", visible); } visible_prev = visible; } usleep(250000); } } struct qdl_device_desc *usb_list(unsigned int *devices_found) { struct libusb_device_descriptor desc; struct libusb_device_handle *handle; struct qdl_device_desc *result; struct libusb_device **devices; struct libusb_device *dev; unsigned long serial_len; unsigned char buf[128]; ssize_t device_count; unsigned int count = 0; char *serial; int ret; int i; ret = libusb_init(NULL); if (ret < 0) { ux_err("failed to initialize libusb: %s\n", libusb_strerror(ret)); return NULL; } device_count = libusb_get_device_list(NULL, &devices); if (device_count < 0) { ux_err("failed to list USB devices: %s\n", libusb_strerror(device_count)); libusb_exit(NULL); return NULL; } if (device_count == 0) return NULL; result = calloc(device_count, sizeof(struct qdl_device_desc)); if (!result) { ux_err("failed to allocate devices array\n"); libusb_free_device_list(devices, 1); libusb_exit(NULL); return NULL; } for (i = 0; i < device_count; i++) { dev = devices[i]; ret = libusb_get_device_descriptor(dev, &desc); if (ret < 0) { warnx("failed to get USB device descriptor"); continue; } if (desc.idVendor != 0x05c6) continue; if (desc.idProduct != 0x9008 && desc.idProduct != 0x900e && desc.idProduct != 0x901d) continue; ret = libusb_open(dev, &handle); if (ret < 0) continue; ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, buf, sizeof(buf) - 1); if (ret < 0) { warnx("failed to read iProduct descriptor: %s", libusb_strerror(ret)); libusb_close(handle); continue; } buf[ret] = '\0'; serial = strstr((char *)buf, "_SN:"); if (!serial) { memcpy(result[count].serial, "(none)", sizeof("(none)")); } else { serial += strlen("_SN:"); serial_len = strcspn(serial, " _"); if (serial_len + 1 > sizeof(result[count].serial)) { ux_err("ignoring device with unexpectedly long serial number\n"); libusb_close(handle); continue; } memcpy(result[count].serial, serial, serial_len); result[count].serial[serial_len] = '\0'; } result[count].vid = desc.idVendor; result[count].pid = desc.idProduct; libusb_close(handle); count++; } libusb_free_device_list(devices, 1); *devices_found = count; return result; } static void usb_close(struct qdl_device *qdl) { struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base); libusb_close(qdl_usb->usb_handle); libusb_exit(NULL); } static int usb_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout) { struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base); int actual; int ret; ret = libusb_bulk_transfer(qdl_usb->usb_handle, qdl_usb->in_ep, buf, len, &actual, timeout); if (ret != 0 && ret != LIBUSB_ERROR_TIMEOUT) return -EIO; if (ret == LIBUSB_ERROR_TIMEOUT && actual == 0) return -ETIMEDOUT; /* If what we read equals the endpoint's Max Packet Size, consume the ZLP explicitly */ if (len == (size_t)actual && !(actual % qdl_usb->in_maxpktsize)) { ret = libusb_bulk_transfer(qdl_usb->usb_handle, qdl_usb->in_ep, NULL, 0, NULL, timeout); if (ret) warnx("Unable to read ZLP: %s", libusb_strerror(ret)); } return actual; } static int usb_write(struct qdl_device *qdl, const void *buf, size_t len, unsigned int timeout) { unsigned char *data = (unsigned char *)buf; struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base); unsigned int count = 0; size_t len_orig = len; int actual; int xfer; int ret; while (len > 0) { xfer = (len > qdl_usb->out_chunk_size) ? qdl_usb->out_chunk_size : len; ret = libusb_bulk_transfer(qdl_usb->usb_handle, qdl_usb->out_ep, data, xfer, &actual, timeout); if (ret != 0 && ret != LIBUSB_ERROR_TIMEOUT) { warnx("bulk write failed: %s", libusb_strerror(ret)); return -EIO; } if (ret == LIBUSB_ERROR_TIMEOUT && actual == 0) return -ETIMEDOUT; count += actual; len -= actual; data += actual; } if (len_orig % qdl_usb->out_maxpktsize == 0) { ret = libusb_bulk_transfer(qdl_usb->usb_handle, qdl_usb->out_ep, NULL, 0, &actual, timeout); if (ret < 0) return -EIO; } return count; } static void usb_set_out_chunk_size(struct qdl_device *qdl, long size) { struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base); qdl_usb->out_chunk_size = size; } struct qdl_device *usb_init(void) { struct qdl_device *qdl = malloc(sizeof(struct qdl_device_usb)); if (!qdl) return NULL; memset(qdl, 0, sizeof(struct qdl_device_usb)); qdl->dev_type = QDL_DEVICE_USB; qdl->open = usb_open; qdl->read = usb_read; qdl->write = usb_write; qdl->close = usb_close; qdl->set_out_chunk_size = usb_set_out_chunk_size; qdl->max_payload_size = 1048576; return qdl; } linux-msm-qdl-f540e59/util.c000066400000000000000000000136211521156022300157050ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2016, Bjorn Andersson * All rights reserved. */ #include #include #include #include #include #include #include #include #include #include #include #include "file.h" #include "oscompat.h" #include "qdl.h" #include "version.h" static uint8_t to_hex(uint8_t ch) { ch &= 0xf; return ch <= 9 ? '0' + ch : 'a' + ch - 10; } void print_version(void) { extern const char *__progname; fprintf(stdout, "%s version %s\n", __progname, VERSION); } 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; unsigned int i; unsigned int j; for (i = 0; i < len; i += 16) { linelen = MIN(16u, (size_t)(len - i)); 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); } } unsigned int attr_as_unsigned(xmlNode *node, const char *attr, int *errors) { unsigned int ret; xmlChar *value; value = xmlGetProp(node, (xmlChar *)attr); if (!value) { (*errors)++; return 0; } ret = (unsigned int)strtoul((char *)value, NULL, 0); xmlFree(value); return ret; } const char *attr_as_string(xmlNode *node, const char *attr, int *errors) { xmlChar *value; char *ret = NULL; value = xmlGetProp(node, (xmlChar *)attr); if (!value) { (*errors)++; return NULL; } if (value[0] != '\0') ret = strdup((char *)value); xmlFree(value); return ret; } bool attr_as_bool(xmlNode *node, const char *attr, int *errors) { xmlChar *value; bool ret = false; if (!xmlHasProp(node, (xmlChar *)attr)) return false; value = xmlGetProp(node, (xmlChar *)attr); if (!value) { (*errors)++; return false; } ret = (xmlStrcmp(value, (xmlChar *)"true") == 0); xmlFree(value); return ret; } static const char * const storage_types[] = { [QDL_STORAGE_EMMC] = "emmc", [QDL_STORAGE_NAND] = "nand", [QDL_STORAGE_NVME] = "nvme", [QDL_STORAGE_SPINOR] = "spinor", [QDL_STORAGE_UFS] = "ufs", }; const char *encode_storage_type(enum qdl_storage_type storage) { if ((unsigned int)storage >= ARRAY_SIZE(storage_types)) return NULL; return storage_types[storage]; } enum qdl_storage_type decode_storage_type(const char *storage) { unsigned int i; if (!storage) return QDL_STORAGE_UNKNOWN; for (i = 0; i < ARRAY_SIZE(storage_types); i++) if (storage_types[i] && !strcmp(storage, storage_types[i])) return i; return QDL_STORAGE_UNKNOWN; } /*** * 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 * @gpt_partition: GPT name * * 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 * 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 requested address, the other(s) will be zeroed. * * Returns: 0 on success, -1 on failure */ int parse_storage_address(const char *address, int *physical_partition, unsigned int *start_sector, unsigned int *num_sectors, char **gpt_partition) { unsigned long length = 0; const char *ptr = address; unsigned long sector = 0; long partition; char *end; char *gpt = NULL; errno = 0; partition = strtol(ptr, &end, 10); if (end == ptr) { partition = -1; gpt = strdup(ptr); goto done; } 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); if (end == ptr) { gpt = strdup(ptr); goto done; } 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; *gpt_partition = gpt; return 0; } /** * 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(struct qdl_zip *zip, const char *filename, struct sahara_image *image) { struct qdl_file file; size_t len; void *ptr; int ret; ret = qdl_file_open(zip, filename, &file); if (ret < 0) { ux_err("failed to read \"%s\"\n", filename); return -1; } ptr = qdl_file_load(&file, &len); if (!ptr || len == 0) goto err_close; image->name = strdup(filename); image->ptr = ptr; image->len = len; qdl_file_close(&file); return 0; err_close: qdl_file_close(&file); return -1; } 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){}; } } linux-msm-qdl-f540e59/ux.c000066400000000000000000000106171521156022300153660ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause #include #ifdef _WIN32 #include #else #include #endif #include #include #include #include #include "qdl.h" /* libxml2 2.12 const-qualified the xmlError pointer in this callback. */ #if LIBXML_VERSION >= 21200 typedef const xmlError * ux_xml_error_ptr; #else typedef xmlErrorPtr ux_xml_error_ptr; #endif #define UX_PROGRESS_REFRESH_RATE 10 #define UX_PROGRESS_SIZE_MAX 80 #define HASHES "################################################################################" #define DASHES "--------------------------------------------------------------------------------" static const char * const progress_hashes = HASHES; static const char * const progress_dashes = DASHES; static unsigned int ux_width; static unsigned int ux_cur_line_length; /* * Levels of output: * * error: used to signal errors to the user * info: used to inform the user about progress * logs: log prints from the device * debug: protocol logs */ /* Clear ux_cur_line_length characters of the progress bar from the screen */ static void ux_clear_line(void) { if (!ux_cur_line_length) return; printf("%*s\r", ux_cur_line_length, ""); fflush(stdout); ux_cur_line_length = 0; } /* * libxml2 emits parser diagnostics directly to stderr by default. Route them * through ux_err() so the file:line context is preserved while keeping output * consistent with the rest of the tool. */ static void ux_xml_error_handler(void *ctx __unused, ux_xml_error_ptr error) { const char *level; if (!error || error->level == XML_ERR_NONE) return; switch (error->level) { case XML_ERR_WARNING: level = "warning"; break; case XML_ERR_ERROR: level = "error"; break; case XML_ERR_FATAL: default: level = "fatal"; break; } if (error->file) ux_err("libxml2 %s: %s:%d: %s", level, error->file, error->line, error->message); else ux_err("libxml2 %s: %s", level, error->message); } #ifdef _WIN32 void ux_init(void) { CONSOLE_SCREEN_BUFFER_INFO csbi; int columns; HANDLE stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE); if (GetConsoleScreenBufferInfo(stdoutHandle, &csbi)) { columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; ux_width = MIN(columns, UX_PROGRESS_SIZE_MAX); } xmlSetStructuredErrorFunc(NULL, ux_xml_error_handler); } #else void ux_init(void) { struct winsize w; int ret; ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); if (!ret) ux_width = MIN(w.ws_col, UX_PROGRESS_SIZE_MAX); xmlSetStructuredErrorFunc(NULL, ux_xml_error_handler); } #endif void ux_err(const char *fmt, ...) { va_list ap; ux_clear_line(); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fflush(stderr); } void ux_info(const char *fmt, ...) { va_list ap; ux_clear_line(); va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); fflush(stdout); } void ux_log(const char *fmt, ...) { va_list ap; if (!qdl_debug) return; ux_clear_line(); va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); fflush(stdout); } void ux_debug(const char *fmt, ...) { va_list ap; if (!qdl_debug) return; ux_clear_line(); va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); fflush(stdout); } void ux_progress(const char *fmt, unsigned int value, unsigned int max, ...) { static struct timeval last_progress_update; unsigned long elapsed_us; unsigned int bar_length; unsigned int bars; unsigned int dashes; struct timeval now; char task_name[32]; float percent; va_list ap; /* Don't print progress is window is too narrow, or if stdout is redirected */ if (ux_width < 30) return; /* Avoid updating the console more than UX_PROGRESS_REFRESH_RATE per second */ if (last_progress_update.tv_sec) { gettimeofday(&now, NULL); elapsed_us = (now.tv_sec - last_progress_update.tv_sec) * 1000000 + (now.tv_usec - last_progress_update.tv_usec); if (elapsed_us < (1000000 / UX_PROGRESS_REFRESH_RATE)) return; } if (value > max) value = max; va_start(ap, max); vsnprintf(task_name, sizeof(task_name), fmt, ap); va_end(ap); bar_length = ux_width - (20 + 4 + 6); percent = (float)value / max; bars = percent * bar_length; dashes = bar_length - bars; printf("%-20.20s [%.*s%.*s] %1.2f%%%n\r", task_name, bars, progress_hashes, dashes, progress_dashes, percent * 100, &ux_cur_line_length); fflush(stdout); gettimeofday(&last_progress_update, NULL); } linux-msm-qdl-f540e59/version.h.in000066400000000000000000000000341521156022300170210ustar00rootroot00000000000000#define VERSION "@version@" linux-msm-qdl-f540e59/vip.c000066400000000000000000000303401521156022300155230ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved. */ #include #include #include #include #include #include #include #include #include "sim.h" #define DIGEST_FULL_TABLE_FILE "DIGEST_TABLE.bin" #define CHAINED_TABLE_FILE_PREF "ChainedTableOfDigests" #define CHAINED_TABLE_FILE_MAX_NAME 64 #define DIGEST_TABLE_TO_SIGN_FILE "DigestsToSign.bin" #define DIGEST_TABLE_TO_SIGN_FILE_MBN (DIGEST_TABLE_TO_SIGN_FILE ".mbn") #define MAX_DIGESTS_PER_SIGNED_TABLE (MAX_DIGESTS_PER_SIGNED_FILE - 1) #define MAX_DIGESTS_PER_CHAINED_TABLE (MAX_DIGESTS_PER_CHAINED_FILE - 1) #define MAX_DIGESTS_PER_BUF 16 #ifndef O_BINARY #define O_BINARY 0 #define O_TEXT 0 #endif struct vip_table_generator { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA2_CTX ctx; FILE *digest_table_fd; size_t digest_num_written; const char *path; }; void vip_transfer_deinit(struct qdl_device *qdl); static void print_digest(unsigned char *buf) { char hex_str[SHA256_DIGEST_STRING_LENGTH]; for (size_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) sprintf(hex_str + i * 2, "%02x", buf[i]); hex_str[SHA256_DIGEST_STRING_LENGTH - 1] = '\0'; ux_debug("FIREHOSE PACKET SHA256: %s\n", hex_str); } int vip_gen_init(struct qdl_device *qdl, const char *path) { struct vip_table_generator *vip_gen; struct stat st; char filepath[PATH_MAX]; if (qdl->dev_type != QDL_DEVICE_SIM) { ux_err("Should be executed in simulation dry-run mode\n"); return -1; } if (stat(path, &st) || !S_ISDIR(st.st_mode)) { ux_err("Directory '%s' to store VIP tables doesn't exist\n", path); return -1; } vip_gen = malloc(sizeof(struct vip_table_generator)); if (!vip_gen) { ux_err("Can't allocate memory for vip_table_generator\n"); return -1; } if (!sim_set_digest_generation(true, qdl, vip_gen)) { ux_err("Can't enable digest table generation\n"); goto out_cleanup; } vip_gen->digest_num_written = 0; vip_gen->path = path; snprintf(filepath, sizeof(filepath), "%s/%s", path, DIGEST_FULL_TABLE_FILE); vip_gen->digest_table_fd = fopen(filepath, "wb"); if (!vip_gen->digest_table_fd) { ux_err("Can't create %s file\n", filepath); goto out_cleanup; } return 0; out_cleanup: free(vip_gen); sim_set_digest_generation(false, qdl, NULL); return -1; } void vip_gen_chunk_init(struct qdl_device *qdl) { struct vip_table_generator *vip_gen; vip_gen = sim_get_vip_generator(qdl); if (!vip_gen) return; SHA256Init(&vip_gen->ctx); } void vip_gen_chunk_update(struct qdl_device *qdl, const void *buf, size_t len) { struct vip_table_generator *vip_gen; vip_gen = sim_get_vip_generator(qdl); if (!vip_gen) return; SHA256Update(&vip_gen->ctx, (uint8_t *)buf, len); } void vip_gen_chunk_store(struct qdl_device *qdl) { struct vip_table_generator *vip_gen; vip_gen = sim_get_vip_generator(qdl); if (!vip_gen) return; SHA256Final(vip_gen->hash, &vip_gen->ctx); print_digest(vip_gen->hash); if (fwrite(vip_gen->hash, SHA256_DIGEST_LENGTH, 1, vip_gen->digest_table_fd) != 1) { ux_err("Failed to write digest to the " DIGEST_FULL_TABLE_FILE); goto out_cleanup; } vip_gen->digest_num_written++; return; out_cleanup: fclose(vip_gen->digest_table_fd); vip_gen->digest_table_fd = NULL; } static int write_output_file(const char *filename, bool append, const void *data, size_t len) { FILE *fp; char *mode = "wb"; if (append) mode = "ab"; fp = fopen(filename, mode); if (!fp) { ux_err("Failed to open file for appending\n"); return -1; } if (fwrite(data, 1, len, fp) != len) { ux_err("Failed to append to file\n"); fclose(fp); return -1; } fclose(fp); return 0; } static int write_digests_to_table(char *src_table, char *dest_table, size_t start_digest, size_t count, SHA2_CTX *out_ctx) { const size_t elem_size = SHA256_DIGEST_LENGTH; unsigned char buf[MAX_DIGESTS_PER_BUF * SHA256_DIGEST_LENGTH]; size_t written = 0; FILE *out = NULL; int ret = -1; int fd = open(src_table, O_RDONLY | O_BINARY); if (fd < 0) { ux_err("Failed to open %s for reading\n", src_table); return -1; } out = fopen(dest_table, "wb"); if (!out) { ux_err("Failed to open %s for writing\n", dest_table); goto out_cleanup; } if (out_ctx) SHA256Init(out_ctx); /* Seek to offset of start_digest */ off_t offset = elem_size * start_digest; if (lseek(fd, offset, SEEK_SET) != offset) { ux_err("Failed to seek in %s\n", src_table); goto out_cleanup; } while (written < (count * elem_size)) { size_t to_read = count * elem_size - written; if (to_read > sizeof(buf)) to_read = sizeof(buf); ssize_t bytes = read(fd, buf, to_read); if (bytes < 0 || (size_t)bytes != to_read) { ux_err("Failed to read from %s\n", src_table); goto out_cleanup; } if (fwrite(buf, 1, bytes, out) != (size_t)bytes) { ux_err("Can't write digests to %s\n", dest_table); goto out_cleanup; } if (out_ctx) SHA256Update(out_ctx, buf, bytes); written += to_read; } ret = 0; out_cleanup: if (out) fclose(out); close(fd); return ret; } static int create_chained_tables(struct vip_table_generator *vip_gen) { size_t total_digests = vip_gen->digest_num_written; char src_table[PATH_MAX]; char dest_table[PATH_MAX]; unsigned char hash[SHA256_DIGEST_LENGTH]; SHA2_CTX *chain_ctxs = NULL; size_t chained_num = 0; int ret = 0; snprintf(src_table, sizeof(src_table), "%s/%s", vip_gen->path, DIGEST_FULL_TABLE_FILE); /* Pre-compute the number of chained tables so we can allocate contexts */ if (total_digests > MAX_DIGESTS_PER_SIGNED_TABLE) { size_t remaining = total_digests - MAX_DIGESTS_PER_SIGNED_TABLE; chained_num = (remaining + MAX_DIGESTS_PER_CHAINED_TABLE - 1) / MAX_DIGESTS_PER_CHAINED_TABLE; chain_ctxs = calloc(chained_num, sizeof(SHA2_CTX)); if (!chain_ctxs) return -1; } /* Step 1: Write digest table to DigestsToSign.bin */ snprintf(dest_table, sizeof(dest_table), "%s/%s", vip_gen->path, DIGEST_TABLE_TO_SIGN_FILE); size_t tosign_count = total_digests < MAX_DIGESTS_PER_SIGNED_TABLE ? total_digests : MAX_DIGESTS_PER_SIGNED_TABLE; ret = write_digests_to_table(src_table, dest_table, 0, tosign_count, NULL); if (ret) { ux_err("Writing digests to %s failed\n", dest_table); goto out; } /* Step 2: Write remaining digests to ChainedTableOfDigests.bin, capturing * a SHA2 context after writing each table's digest payload so Step 3 can * compute the final hashes without re-reading the files. */ if (total_digests > MAX_DIGESTS_PER_SIGNED_TABLE) { size_t remaining_digests = total_digests - MAX_DIGESTS_PER_SIGNED_TABLE; size_t chain_idx = 0; while (remaining_digests > 0) { size_t table_digests = remaining_digests > MAX_DIGESTS_PER_CHAINED_TABLE ? MAX_DIGESTS_PER_CHAINED_TABLE : remaining_digests; snprintf(dest_table, sizeof(dest_table), "%s/%s%zu.bin", vip_gen->path, CHAINED_TABLE_FILE_PREF, chain_idx); ret = write_digests_to_table(src_table, dest_table, total_digests - remaining_digests, table_digests, &chain_ctxs[chain_idx]); if (ret) { ux_err("Writing digests to %s failed\n", dest_table); goto out; } remaining_digests -= table_digests; if (!remaining_digests) { /* Add zero (the packet can't be multiple of 512 bytes) */ ret = write_output_file(dest_table, true, "\0", 1); if (ret < 0) { ux_err("Can't write 0 to %s\n", dest_table); goto out; } SHA256Update(&chain_ctxs[chain_idx], (const uint8_t *)"\0", 1); } chain_idx++; } } /* Step 3: Hash and append backwards. Each file's final content is the * digest payload written in Step 2 plus the hash of the next file appended * in the previous iteration. Finalize a copy of the cached context (updated * with the appended hash where applicable) instead of re-reading the file. */ for (ssize_t i = chained_num - 1; i >= 0; --i) { SHA2_CTX ctx = chain_ctxs[i]; if (i < (ssize_t)chained_num - 1) SHA256Update(&ctx, hash, SHA256_DIGEST_LENGTH); SHA256Final(hash, &ctx); if (i == 0) { snprintf(dest_table, sizeof(dest_table), "%s/%s", vip_gen->path, DIGEST_TABLE_TO_SIGN_FILE); } else { snprintf(dest_table, sizeof(dest_table), "%s/%s%zd.bin", vip_gen->path, CHAINED_TABLE_FILE_PREF, (i - 1)); } ret = write_output_file(dest_table, true, hash, SHA256_DIGEST_LENGTH); if (ret < 0) { ux_err("Failed to append hash to %s\n", dest_table); goto out; } } out: free(chain_ctxs); return ret; } void vip_gen_finalize(struct qdl_device *qdl) { struct vip_table_generator *vip_gen; vip_gen = sim_get_vip_generator(qdl); if (!vip_gen) return; fclose(vip_gen->digest_table_fd); ux_debug("VIP TABLE DIGESTS: %lu\n", vip_gen->digest_num_written); if (create_chained_tables(vip_gen) < 0) ux_err("Error occurred when creating table of digests\n"); free(vip_gen); sim_set_digest_generation(false, qdl, NULL); } int vip_transfer_init(struct qdl_device *qdl, const char *vip_table_path) { char fullpath[PATH_MAX]; snprintf(fullpath, sizeof(fullpath), "%s/%s", vip_table_path, DIGEST_TABLE_TO_SIGN_FILE_MBN); qdl->vip_data.signed_table_fd = open(fullpath, O_RDONLY); if (!qdl->vip_data.signed_table_fd) { ux_err("Can't open signed table %s\n", fullpath); return -1; } qdl->vip_data.chained_num = 0; for (int i = 0; i < MAX_CHAINED_FILES; ++i) { snprintf(fullpath, sizeof(fullpath), "%s/%s%d%s", vip_table_path, CHAINED_TABLE_FILE_PREF, i, ".bin"); int fd = open(fullpath, O_RDONLY); if (fd == -1) { if (errno == ENOENT) break; ux_err("Can't open signed table %s\n", fullpath); goto out_cleanup; } qdl->vip_data.chained_fds[qdl->vip_data.chained_num++] = fd; } qdl->vip_data.state = VIP_INIT; qdl->vip_data.chained_cur = 0; return 0; out_cleanup: vip_transfer_deinit(qdl); return -1; } void vip_transfer_deinit(struct qdl_device *qdl) { close(qdl->vip_data.signed_table_fd); for (size_t i = 0; i < qdl->vip_data.chained_num; ++i) close(qdl->vip_data.chained_fds[i]); } static int vip_transfer_send_raw(struct qdl_device *qdl, int table_fd) { struct stat sb; int ret; void *buf; ssize_t n; ret = fstat(table_fd, &sb); if (ret < 0) { ux_err("Failed to stat digest table file\n"); return -1; } buf = malloc(sb.st_size); if (!buf) { ux_err("Failed to allocate transfer buffer\n"); return -1; } n = read(table_fd, buf, sb.st_size); if (n < 0 || n != sb.st_size) { ux_err("failed to read binary\n"); ret = -1; goto out; } qdl->vip_data.sending_table = true; n = qdl_write(qdl, buf, n, 1000); qdl->vip_data.sending_table = false; if (n < 0) { ux_err("USB write failed for data chunk\n"); ret = -1; goto out; } out: free(buf); return ret; } int vip_transfer_handle_tables(struct qdl_device *qdl) { struct vip_transfer_data *vip_data = &qdl->vip_data; int ret = 0; if (vip_data->state == VIP_DISABLED) return 0; if (vip_data->state == VIP_INIT) { /* Send initial signed table */ ret = vip_transfer_send_raw(qdl, vip_data->signed_table_fd); if (ret) { ux_err("VIP: failed to send the Signed VIP table\n"); return ret; } ux_debug("VIP: successfully sent the Initial VIP table\n"); vip_data->state = VIP_SEND_DATA; vip_data->frames_sent = 0; vip_data->frames_left = MAX_DIGESTS_PER_SIGNED_TABLE; vip_data->fh_parse_status = true; } if (vip_data->state == VIP_SEND_NEXT_TABLE) { if (vip_data->chained_cur >= vip_data->chained_num) { ux_err("VIP: the required quantity of chained tables is missing\n"); return -1; } ret = vip_transfer_send_raw(qdl, vip_data->chained_fds[vip_data->chained_cur]); if (ret) { ux_err("VIP: failed to send the chained VIP table\n"); return ret; } ux_debug("VIP: successfully sent " CHAINED_TABLE_FILE_PREF "%lu.bin\n", vip_data->chained_cur); vip_data->state = VIP_SEND_DATA; vip_data->frames_sent = 0; vip_data->frames_left = MAX_DIGESTS_PER_CHAINED_TABLE; vip_data->fh_parse_status = true; vip_data->chained_cur++; } vip_data->frames_sent++; if (vip_data->frames_sent >= vip_data->frames_left) vip_data->state = VIP_SEND_NEXT_TABLE; return 0; } bool vip_transfer_status_check_needed(struct qdl_device *qdl) { return qdl->vip_data.fh_parse_status; } void vip_transfer_clear_status(struct qdl_device *qdl) { qdl->vip_data.fh_parse_status = false; } linux-msm-qdl-f540e59/vip.h000066400000000000000000000036051521156022300155340ustar00rootroot00000000000000/* SPDX-License-Identifier: BSD-3-Clause */ /* * Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved. */ #ifndef __VIP_H__ #define __VIP_H__ #include "sha2.h" struct vip_table_generator; /* * Capacity of each VIP digest table file. * * Each table holds at most FILE entries: the last slot is reserved for a * chain hash linking to the next table. The TABLE constant is therefore * FILE - 1 and represents the number of actual data-chunk hashes per table. */ #define MAX_DIGESTS_PER_SIGNED_FILE 54 #define MAX_DIGESTS_PER_CHAINED_FILE 256 enum vip_state { VIP_DISABLED, VIP_INIT, VIP_SEND_NEXT_TABLE, VIP_SEND_DATA, VIP_MAX, }; #define MAX_CHAINED_FILES 32 struct vip_transfer_data { enum vip_state state; int signed_table_fd; int chained_fds[MAX_CHAINED_FILES]; size_t chained_num; size_t chained_cur; size_t digests; size_t frames_sent; size_t frames_left; size_t chained_table_size; bool fh_parse_status; bool sending_table; /* set during vip_transfer_send_raw() */ /* * Set when the programmer's startup logs announce that VIP is * active (it expects a signed digest table). Lets the host detect * a mismatch between the device's policy and the --vip-table-path * option before the configure handshake stalls. */ bool programmer_requires_vip; }; int vip_transfer_init(struct qdl_device *qdl, const char *vip_table_path); void vip_transfer_deinit(struct qdl_device *qdl); int vip_transfer_handle_tables(struct qdl_device *qdl); bool vip_transfer_status_check_needed(struct qdl_device *qdl); void vip_transfer_clear_status(struct qdl_device *qdl); int vip_gen_init(struct qdl_device *qdl, const char *path); void vip_gen_chunk_init(struct qdl_device *qdl); void vip_gen_chunk_update(struct qdl_device *qdl, const void *buf, size_t len); void vip_gen_chunk_store(struct qdl_device *qdl); void vip_gen_finalize(struct qdl_device *qdl); #endif /* __VIP_H__ */ linux-msm-qdl-f540e59/zipper.c000066400000000000000000000633121521156022300162430ustar00rootroot00000000000000// SPDX-License-Identifier: BSD-3-Clause /* * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. */ #define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include #include "flashmap.h" #include "firehose.h" #include "list.h" #include "qdl.h" #include "sparse.h" static char *zipper_basename(const char *path) { char *tmp; char *base; if (!path || !path[0]) return NULL; tmp = strdup(path); if (!tmp) return NULL; base = basename(tmp); base = base ? strdup(base) : NULL; free(tmp); return base; } static void xml_setpropf(xmlNode *node, const char *attr, const char *fmt, ...) { char *buf; va_list ap; if (!node || !attr || !fmt) return; va_start(ap, fmt); if (vasprintf(&buf, fmt, ap) < 0) { va_end(ap); return; } va_end(ap); xmlSetProp(node, (xmlChar *)attr, (xmlChar *)buf); free(buf); } struct zipper_name_entry { char *name; struct list_head node; }; struct zipper_file_rename { enum qdl_storage_type storage_type; char *source_name; char *zip_name; struct list_head node; }; struct zipper_storage_ctx { enum qdl_storage_type storage_type; const char *memory; xmlDoc *program_doc; xmlNode *program_root; xmlDoc *patch_doc; xmlNode *patch_root; char *program_xml_name; char *patch_xml_name; struct list_head node; }; struct flashmap_json_buf { char *buf; size_t len; size_t cap; bool error; }; static void zipper_free_name_entries(struct list_head *names) { struct zipper_name_entry *entry; struct zipper_name_entry *next; list_for_each_entry_safe(entry, next, names, node) { list_del(&entry->node); free(entry->name); free(entry); } } static bool zipper_name_is_used(struct list_head *names, const char *name) { struct zipper_name_entry *entry; list_for_each_entry(entry, names, node) { if (!strcmp(entry->name, name)) return true; } return false; } static int zipper_add_name_entry(struct list_head *names, const char *name) { struct zipper_name_entry *entry; entry = calloc(1, sizeof(*entry)); if (!entry) return -1; entry->name = strdup(name); if (!entry->name) { free(entry); return -1; } list_append(names, &entry->node); return 0; } static int zipper_add_buffer(zip_t *zip, const char *name, const void *data, size_t len) { zip_source_t *source; zip_int64_t idx; void *copy = NULL; if (len) { copy = malloc(len); if (!copy) { ux_err("failed to allocate zip member \"%s\"\n", name); return -1; } memcpy(copy, data, len); } source = zip_source_buffer(zip, copy, len, 1); if (!source) { ux_err("failed to create zip source for \"%s\"\n", name); free(copy); return -1; } idx = zip_file_add(zip, name, source, ZIP_FL_OVERWRITE); if (idx < 0) { ux_err("failed to add \"%s\" to zip\n", name); zip_source_free(source); return -1; } return 0; } static int zipper_add_file(zip_t *zip, const char *name, const char *path) { zip_source_t *source; zip_int64_t idx; source = zip_source_file(zip, path, 0, -1); if (!source) { ux_err("failed to create zip file source for \"%s\"\n", path); return -1; } idx = zip_file_add(zip, name, source, ZIP_FL_OVERWRITE); if (idx < 0) { ux_err("failed to add \"%s\" to zip\n", name); zip_source_free(source); return -1; } return 0; } static void zipper_close_progress_cb(zip_t *archive __unused, double progress, void *userdata) { unsigned int value; const char *label = userdata ? (const char *)userdata : "finalize"; if (progress < 0.0) progress = 0.0; else if (progress > 1.0) progress = 1.0; value = (unsigned int)(progress * 100000.0); ux_progress(label, value, 100000); } static struct zipper_file_rename *zipper_find_file_rename(struct list_head *renames, enum qdl_storage_type storage_type, const char *source_name) { struct zipper_file_rename *rename; list_for_each_entry(rename, renames, node) { if (rename->storage_type == storage_type && !strcmp(rename->source_name, source_name)) return rename; } return NULL; } static void zipper_free_file_renames(struct list_head *renames) { struct zipper_file_rename *rename; struct zipper_file_rename *next; list_for_each_entry_safe(rename, next, renames, node) { list_del(&rename->node); free(rename->source_name); free(rename->zip_name); free(rename); } } static struct zipper_storage_ctx *zipper_find_storage_ctx(struct list_head *storages, enum qdl_storage_type storage_type) { struct zipper_storage_ctx *storage; list_for_each_entry(storage, storages, node) { if (storage->storage_type == storage_type) return storage; } return NULL; } static void zipper_free_storage_contexts(struct list_head *storages) { struct zipper_storage_ctx *storage; struct zipper_storage_ctx *next; list_for_each_entry_safe(storage, next, storages, node) { list_del(&storage->node); xmlFreeDoc(storage->program_doc); xmlFreeDoc(storage->patch_doc); free(storage->program_xml_name); free(storage->patch_xml_name); free(storage); } } static struct zipper_storage_ctx *zipper_create_storage_ctx(enum qdl_storage_type storage_type) { struct zipper_storage_ctx *storage; const char *memory; memory = encode_storage_type(storage_type); if (!memory) { ux_err("unknown storage type %d\n", storage_type); return NULL; } storage = calloc(1, sizeof(*storage)); if (!storage) return NULL; storage->storage_type = storage_type; storage->memory = memory; storage->program_doc = xmlNewDoc((xmlChar *)"1.0"); storage->program_root = xmlNewNode(NULL, (xmlChar *)"data"); if (!storage->program_doc || !storage->program_root) goto err; xmlDocSetRootElement(storage->program_doc, storage->program_root); storage->patch_doc = xmlNewDoc((xmlChar *)"1.0"); storage->patch_root = xmlNewNode(NULL, (xmlChar *)"patches"); if (!storage->patch_doc || !storage->patch_root) goto err; xmlDocSetRootElement(storage->patch_doc, storage->patch_root); return storage; err: xmlFreeDoc(storage->program_doc); xmlFreeDoc(storage->patch_doc); free(storage); return NULL; } static int zipper_storage_append_program(struct zipper_storage_ctx *storage, struct firehose_op *op, const char *filename, unsigned int file_offset) { xmlNode *node; if (!filename) return 0; node = xmlNewChild(storage->program_root, NULL, (xmlChar *)"program", NULL); if (!node) return -1; xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%u", op->sector_size); xml_setpropf(node, "filename", "%s", filename); xml_setpropf(node, "num_partition_sectors", "%u", op->num_sectors); xml_setpropf(node, "physical_partition_number", "%d", op->partition); xml_setpropf(node, "start_sector", "%s", op->start_sector ? op->start_sector : "0"); if (op->label) xml_setpropf(node, "label", "%s", op->label); if (op->is_nand) { xml_setpropf(node, "PAGES_PER_BLOCK", "%u", op->pages_per_block); xml_setpropf(node, "last_sector", "%u", op->last_sector); } else { xml_setpropf(node, "file_sector_offset", "%u", file_offset); } return 0; } static int zipper_register_raw_program_file(zip_t *zip, struct list_head *name_entries, const char *memory, struct firehose_op *op, unsigned int *raw_idx, char **zip_name) { zip_source_t *source; zip_int64_t idx; uint64_t raw_size; char *filename; int ret; if (!op->sector_size || !op->num_sectors || op->sparse_offset < 0) { ux_err("invalid sparse raw chunk parameters\n"); return -1; } if ((uint64_t)op->num_sectors > INT64_MAX / op->sector_size) { ux_err("sparse raw chunk too large\n"); return -1; } raw_size = (uint64_t)op->num_sectors * op->sector_size; filename = calloc(1, strlen(memory) + 31); if (!filename) return -1; snprintf(filename, strlen(memory) + 31, "%s_raw_%06u.bin", memory, (*raw_idx)++); if (zipper_name_is_used(name_entries, filename)) { ux_err("duplicate generated raw filename \"%s\"\n", filename); goto err_free_filename; } source = zip_source_file(zip, op->filename, (zip_uint64_t)op->sparse_offset, (zip_int64_t)raw_size); if (!source) { ux_err("failed to create zip file source for \"%s\"\n", op->filename); goto err_free_filename; } idx = zip_file_add(zip, filename, source, ZIP_FL_OVERWRITE); if (idx < 0) { ux_err("failed to add \"%s\" to zip\n", filename); goto err_free_zip_source; } ret = zipper_add_name_entry(name_entries, filename); if (ret) { /* Don't free "source", it's owned by libzip since zip_file_add() */ goto err_free_filename; } *zip_name = filename; return 0; err_free_zip_source: zip_source_free(source); err_free_filename: free(filename); return -1; } static int zipper_register_fill_program_file(zip_t *zip, struct list_head *name_entries, const char *memory, struct firehose_op *op, unsigned int *fill_idx, char **zip_name) { uint32_t fill_value = op->sparse_fill_value; size_t fill_size; char *filename; void *buf; size_t i; int ret; if (!op->sector_size || !op->num_sectors) { ux_err("invalid sparse fill chunk parameters\n"); return -1; } if ((size_t)op->num_sectors > SIZE_MAX / op->sector_size) { ux_err("sparse fill chunk too large\n"); return -1; } fill_size = (size_t)op->num_sectors * op->sector_size; filename = calloc(1, strlen(memory) + 32); if (!filename) return -1; snprintf(filename, strlen(memory) + 32, "%s_fill_%06u.bin", memory, (*fill_idx)++); if (zipper_name_is_used(name_entries, filename)) { ux_err("duplicate generated fill filename \"%s\"\n", filename); free(filename); return -1; } buf = malloc(fill_size); if (!buf) { free(filename); return -1; } for (i = 0; i + sizeof(fill_value) <= fill_size; i += sizeof(fill_value)) memcpy((char *)buf + i, &fill_value, sizeof(fill_value)); while (i < fill_size) { ((char *)buf)[i] = ((char *)&fill_value)[i % sizeof(fill_value)]; i++; } ret = zipper_add_buffer(zip, filename, buf, fill_size); free(buf); if (ret) { free(filename); return -1; } ret = zipper_add_name_entry(name_entries, filename); if (ret) { free(filename); return -1; } *zip_name = filename; return 0; } static int zipper_register_program_file(zip_t *zip, struct list_head *name_entries, struct list_head *renames, enum qdl_storage_type storage_type, const char *memory, const char *source_name, const char **zip_name) { struct zipper_file_rename *rename; char *file_basename; char *renamed_file; int ret; rename = zipper_find_file_rename(renames, storage_type, source_name); if (rename) { *zip_name = rename->zip_name; return 0; } file_basename = zipper_basename(source_name); if (!file_basename) { ux_err("failed to parse basename for \"%s\"\n", source_name); return -1; } if (asprintf(&renamed_file, "%s_%s", memory, file_basename) < 0) { free(file_basename); return -1; } free(file_basename); if (zipper_name_is_used(name_entries, renamed_file)) { ux_err("duplicate zip filename \"%s\"\n", renamed_file); free(renamed_file); return -1; } ret = zipper_add_file(zip, renamed_file, source_name); if (ret) { ux_err("failed to copy \"%s\"\n", source_name); free(renamed_file); return -1; } ret = zipper_add_name_entry(name_entries, renamed_file); if (ret) { free(renamed_file); return -1; } rename = calloc(1, sizeof(*rename)); if (!rename) { free(renamed_file); return -1; } rename->storage_type = storage_type; rename->source_name = strdup(source_name); rename->zip_name = renamed_file; if (!rename->source_name) { free(rename->zip_name); free(rename); return -1; } list_append(renames, &rename->node); *zip_name = rename->zip_name; return 0; } static int zipper_storage_append_erase(struct zipper_storage_ctx *storage, struct firehose_op *op) { xmlNode *node; node = xmlNewChild(storage->program_root, NULL, (xmlChar *)"erase", NULL); if (!node) return -1; xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%u", op->sector_size); xml_setpropf(node, "num_partition_sectors", "%u", op->num_sectors); xml_setpropf(node, "physical_partition_number", "%d", op->partition); xml_setpropf(node, "start_sector", "%s", op->start_sector ? op->start_sector : "0"); if (op->is_nand) xml_setpropf(node, "PAGES_PER_BLOCK", "%u", op->pages_per_block); return 0; } static int zipper_storage_append_patch(struct zipper_storage_ctx *storage, struct firehose_op *op) { xmlNode *node; node = xmlNewChild(storage->patch_root, NULL, (xmlChar *)"patch", NULL); if (!node) return -1; xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%u", op->sector_size); xml_setpropf(node, "byte_offset", "%u", op->byte_offset); xml_setpropf(node, "filename", "%s", op->filename ? op->filename : ""); xml_setpropf(node, "physical_partition_number", "%d", op->partition); xml_setpropf(node, "size_in_bytes", "%u", op->size_in_bytes); xml_setpropf(node, "start_sector", "%s", op->start_sector ? op->start_sector : "0"); xml_setpropf(node, "value", "%s", op->value ? op->value : ""); xml_setpropf(node, "what", "%s", op->what ? op->what : ""); return 0; } static void flashmap_json_grow(struct flashmap_json_buf *json, size_t extra) { size_t needed; size_t next_cap; char *new_buf; if (json->error) return; needed = json->len + extra + 1; if (needed <= json->cap) return; next_cap = json->cap ? json->cap : 256; while (next_cap < needed) { if (next_cap > SIZE_MAX / 2) { json->error = true; return; } next_cap *= 2; } new_buf = realloc(json->buf, next_cap); if (!new_buf) { json->error = true; return; } json->buf = new_buf; json->cap = next_cap; } static void flashmap_json_append_raw(struct flashmap_json_buf *json, const char *s, size_t len) { if (json->error) return; flashmap_json_grow(json, len); if (json->error) return; memcpy(json->buf + json->len, s, len); json->len += len; json->buf[json->len] = '\0'; } static void flashmap_json_appendf(struct flashmap_json_buf *json, const char *fmt, ...) { va_list ap; va_list aq; int needed; if (json->error) return; va_start(ap, fmt); va_copy(aq, ap); needed = vsnprintf(NULL, 0, fmt, aq); va_end(aq); if (needed < 0) { va_end(ap); json->error = true; return; } flashmap_json_grow(json, needed); if (json->error) { va_end(ap); return; } vsnprintf(json->buf + json->len, json->cap - json->len, fmt, ap); json->len += needed; va_end(ap); } static void flashmap_json_append_escaped_string(struct flashmap_json_buf *json, const char *s) { unsigned char ch; char esc[7]; flashmap_json_append_raw(json, "\"", 1); for (; !json->error && *s; s++) { ch = (unsigned char)*s; switch (ch) { case '"': flashmap_json_append_raw(json, "\\\"", 2); break; case '\\': flashmap_json_append_raw(json, "\\\\", 2); break; case '\b': flashmap_json_append_raw(json, "\\b", 2); break; case '\f': flashmap_json_append_raw(json, "\\f", 2); break; case '\n': flashmap_json_append_raw(json, "\\n", 2); break; case '\r': flashmap_json_append_raw(json, "\\r", 2); break; case '\t': flashmap_json_append_raw(json, "\\t", 2); break; default: if (ch < 0x20) { snprintf(esc, sizeof(esc), "\\u%04x", ch); flashmap_json_append_raw(json, esc, 6); } else { flashmap_json_append_raw(json, (const char *)&ch, 1); } break; } } flashmap_json_append_raw(json, "\"", 1); } static int zipper_add_programmer_images(zip_t *zip, struct list_head *name_entries, char **programmer_entries, struct sahara_image *images) { char *basename_name; char *zip_name; int count = 0; int i; int ret; for (i = 0; i < MAPPING_SZ; i++) { if (!images[i].ptr) continue; basename_name = zipper_basename(images[i].name ? images[i].name : "programmer.bin"); if (!basename_name) return -1; if (zipper_name_is_used(name_entries, basename_name)) { zip_name = calloc(1, strlen(basename_name) + 20); if (!zip_name) { free(basename_name); return -1; } snprintf(zip_name, strlen(basename_name) + 20, "sahara%u_%s", i, basename_name); free(basename_name); } else { zip_name = basename_name; } if (zipper_name_is_used(name_entries, zip_name)) { ux_err("duplicate zip filename \"%s\"\n", zip_name); free(zip_name); return -1; } ret = zipper_add_buffer(zip, zip_name, images[i].ptr, images[i].len); if (ret) { free(zip_name); return ret; } ret = zipper_add_name_entry(name_entries, zip_name); if (ret) { free(zip_name); return ret; } programmer_entries[i] = zip_name; count++; } return count ? 0 : -1; } static void zipper_free_programmer_entries(char **programmer_entries) { int i; for (i = 0; i < MAPPING_SZ; i++) { free(programmer_entries[i]); programmer_entries[i] = NULL; } } static int zipper_add_storage_xml(zip_t *zip, struct list_head *name_entries, struct zipper_storage_ctx *storage) { xmlChar *xml_data; int len; int ret; storage->program_xml_name = calloc(1, strlen(storage->memory) + strlen("_rawprogram.xml") + 1); if (!storage->program_xml_name) return -1; snprintf(storage->program_xml_name, strlen(storage->memory) + strlen("_rawprogram.xml") + 1, "%s_rawprogram.xml", storage->memory); storage->patch_xml_name = calloc(1, strlen(storage->memory) + strlen("_patch.xml") + 1); if (!storage->patch_xml_name) return -1; snprintf(storage->patch_xml_name, strlen(storage->memory) + strlen("_patch.xml") + 1, "%s_patch.xml", storage->memory); if (zipper_name_is_used(name_entries, storage->program_xml_name) || zipper_name_is_used(name_entries, storage->patch_xml_name)) { ux_err("duplicate xml filename for memory \"%s\"\n", storage->memory); return -1; } xmlDocDumpMemory(storage->program_doc, &xml_data, &len); if (!xml_data || len < 0) return -1; ret = zipper_add_buffer(zip, storage->program_xml_name, xml_data, (size_t)len); xmlFree(xml_data); if (ret) return ret; ret = zipper_add_name_entry(name_entries, storage->program_xml_name); if (ret) return ret; xmlDocDumpMemory(storage->patch_doc, &xml_data, &len); if (!xml_data || len < 0) return -1; ret = zipper_add_buffer(zip, storage->patch_xml_name, xml_data, (size_t)len); xmlFree(xml_data); if (ret) return ret; ret = zipper_add_name_entry(name_entries, storage->patch_xml_name); if (ret) return ret; return 0; } static int flashmap_build_json(struct flashmap_json_buf *json, char **programmer_entries, struct list_head *storages) { struct zipper_storage_ctx *storage; bool first; int i; flashmap_json_appendf(json, "{\n"); flashmap_json_appendf(json, " \"version\": \"1.2.0-qdl\",\n"); flashmap_json_appendf(json, " \"products\": [\n"); flashmap_json_appendf(json, " {\n"); flashmap_json_appendf(json, " \"name\": \"contents\",\n"); flashmap_json_appendf(json, " \"layouts\": [\n"); flashmap_json_appendf(json, " {\n"); flashmap_json_appendf(json, " \"name\": \"layout0\",\n"); flashmap_json_appendf(json, " \"programmer\": {\n"); first = true; for (i = 0; i < MAPPING_SZ; i++) { if (!programmer_entries[i]) continue; if (!first) flashmap_json_appendf(json, ",\n"); first = false; flashmap_json_appendf(json, " \"%u\": ", i); flashmap_json_append_escaped_string(json, programmer_entries[i]); } flashmap_json_appendf(json, "\n"); flashmap_json_appendf(json, " },\n"); flashmap_json_appendf(json, " \"programmable\": [\n"); first = true; list_for_each_entry(storage, storages, node) { if (!first) flashmap_json_appendf(json, ",\n"); first = false; flashmap_json_appendf(json, " {\n"); flashmap_json_appendf(json, " \"memory\": \"%s\",\n", storage->memory); flashmap_json_appendf(json, " \"slot\": 0,\n"); flashmap_json_appendf(json, " \"files\": [\n"); flashmap_json_appendf(json, " "); flashmap_json_append_escaped_string(json, storage->program_xml_name); flashmap_json_appendf(json, ",\n"); flashmap_json_appendf(json, " "); flashmap_json_append_escaped_string(json, storage->patch_xml_name); flashmap_json_appendf(json, "\n"); flashmap_json_appendf(json, " ]\n"); flashmap_json_appendf(json, " }"); } flashmap_json_appendf(json, "\n"); flashmap_json_appendf(json, " ]\n"); flashmap_json_appendf(json, " }\n"); flashmap_json_appendf(json, " ]\n"); flashmap_json_appendf(json, " }\n"); flashmap_json_appendf(json, " ]\n"); flashmap_json_appendf(json, "}\n"); return json->error ? -1 : 0; } int zipper_write(const char *filename, struct list_head *ops, struct sahara_image *images) { char *programmer_entries[MAPPING_SZ] = {0}; struct list_head file_renames = LIST_INIT(file_renames); struct zipper_storage_ctx *storage = NULL; struct list_head storages = LIST_INIT(storages); struct list_head name_entries = LIST_INIT(name_entries); struct flashmap_json_buf json = {0}; enum qdl_storage_type current_storage = QDL_STORAGE_UNKNOWN; unsigned int raw_idx = 0; unsigned int fill_idx = 0; struct firehose_op *op; unsigned int file_offset; const char *zip_name; char *sparse_zip_name; char *tmp_filename = NULL; zip_t *zip; int tmp_fd; unsigned int programmer_count = 0; unsigned int program_count = 0; unsigned int patch_count = 0; unsigned int erase_count = 0; int zip_error = 0; int ret = -1; tmp_filename = malloc(strlen(filename) + 8); if (!tmp_filename) goto out_cleanup; sprintf(tmp_filename, "%s.XXXXXX", filename); tmp_fd = mkstemp(tmp_filename); if (tmp_fd < 0) { ux_err("failed to create temporary output zip \"%s\": %s\n", tmp_filename, strerror(errno)); goto out_cleanup; } close(tmp_fd); zip = zip_open(tmp_filename, ZIP_CREATE | ZIP_TRUNCATE, &zip_error); if (!zip) { ux_err("failed to open output zip \"%s\"\n", tmp_filename); ret = -1; goto out_cleanup; } list_for_each_entry(op, ops, node) { if (op->type == FIREHOSE_OP_CONFIGURE) { current_storage = op->storage_type; storage = zipper_find_storage_ctx(&storages, current_storage); if (!storage) { storage = zipper_create_storage_ctx(current_storage); if (!storage) goto out_discard_zip; list_append(&storages, &storage->node); } continue; } if (!storage) { ux_err("internal error: missing configure operation\n"); goto out_discard_zip; } switch (op->type) { case FIREHOSE_OP_PROGRAM: if (!op->filename) break; if (op->zip) { ux_err("create-zip does not support program entries sourced from zip archives\n"); goto out_discard_zip; } sparse_zip_name = NULL; zip_name = NULL; if (op->sparse) { switch (op->sparse_chunk_type) { case CHUNK_TYPE_RAW: ret = zipper_register_raw_program_file(zip, &name_entries, storage->memory, op, &raw_idx, &sparse_zip_name); break; case CHUNK_TYPE_FILL: ret = zipper_register_fill_program_file(zip, &name_entries, storage->memory, op, &fill_idx, &sparse_zip_name); break; default: ux_err("unsupported sparse chunk type %u in \"%s\"\n", op->sparse_chunk_type, op->filename); goto out_discard_zip; } if (ret) goto out_discard_zip; zip_name = sparse_zip_name; file_offset = 0; } else { ret = zipper_register_program_file(zip, &name_entries, &file_renames, current_storage, storage->memory, op->filename, &zip_name); if (ret) goto out_discard_zip; file_offset = op->file_offset; } ret = zipper_storage_append_program(storage, op, zip_name, file_offset); free(sparse_zip_name); if (ret) goto out_discard_zip; program_count++; break; case FIREHOSE_OP_ERASE: ret = zipper_storage_append_erase(storage, op); if (ret) goto out_discard_zip; erase_count++; break; case FIREHOSE_OP_PATCH: /* * Only filename=="DISK" entries are actually flashed, * so omit the others from the zip file as well. */ if (!op->filename || strcmp(op->filename, "DISK")) break; ret = zipper_storage_append_patch(storage, op); if (ret) goto out_discard_zip; patch_count++; break; default: break; } } if (list_empty(&storages)) { ux_err("no configurable storage operations to write\n"); goto out_discard_zip; } ret = zipper_add_programmer_images(zip, &name_entries, programmer_entries, images); if (ret) { ux_err("no programmer images available to write\n"); goto out_discard_zip; } for (int i = 0; i < MAPPING_SZ; i++) { if (programmer_entries[i]) programmer_count++; } list_for_each_entry(storage, &storages, node) { ret = zipper_add_storage_xml(zip, &name_entries, storage); if (ret) goto out_discard_zip; } ret = flashmap_build_json(&json, programmer_entries, &storages); if (ret) goto out_discard_zip; ret = zipper_add_buffer(zip, "flashmap.json", json.buf, json.len); if (ret) goto out_discard_zip; ux_info("found %u programmers, %u program entries, %u patch entries, %u erase entries\n", programmer_count, program_count, patch_count, erase_count); ux_info("finalizing archive, this can take a while for large images\n"); if (zip_register_progress_callback_with_state(zip, 0.001, zipper_close_progress_cb, NULL, (void *)filename) < 0) ux_debug("unable to register zip close progress callback\n"); if (zip_close(zip) < 0) { ux_err("failed to finalize output zip\n"); zip_discard(zip); ret = -1; goto out_cleanup; } if (rename(tmp_filename, filename) < 0) { ux_err("failed to rename temporary output zip \"%s\" to \"%s\": %s\n", tmp_filename, filename, strerror(errno)); ret = -1; goto out_cleanup; } ux_info("successfully created %s\n", filename); ret = 0; goto out_cleanup; out_discard_zip: zip_discard(zip); out_cleanup: if (ret && tmp_filename) unlink(tmp_filename); free(tmp_filename); free(json.buf); zipper_free_name_entries(&name_entries); zipper_free_file_renames(&file_renames); zipper_free_programmer_entries(programmer_entries); zipper_free_storage_contexts(&storages); return ret; }