├── .github └── workflows │ ├── cmake.yml │ └── sync.yml ├── .gitignore ├── .gitmodules ├── CMake └── cpack.cmake ├── CMakeLists.txt ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── config.h.in ├── docs ├── img_decode │ ├── config.mk │ ├── firmware │ │ ├── imagefile_new.h │ │ ├── imgdecode.c │ │ └── imgdecode.h │ ├── main │ │ └── main.c │ └── rules.mk └── pach.txt ├── snap └── snapcraft.yaml ├── src ├── CMakeLists.txt ├── GenIMG │ ├── CMakeLists.txt │ ├── GenIMG.cpp │ ├── GenIMG.h │ ├── GenimageWrapper.c │ ├── GenimageWrapper.h │ └── genimage-src │ │ ├── COPYING │ │ ├── Makefile.am │ │ ├── Makefile.in │ │ ├── README.rst │ │ ├── aclocal.m4 │ │ ├── build-aux │ │ ├── compile │ │ ├── config.guess │ │ ├── config.sub │ │ ├── depcomp │ │ ├── install-sh │ │ ├── missing │ │ └── tap-driver.sh │ │ ├── config.c │ │ ├── config.h.in │ │ ├── configure.ac │ │ ├── crc32.c │ │ ├── flash.conf │ │ ├── genimage.c │ │ ├── genimage.h │ │ ├── image-android-sparse.c │ │ ├── image-cpio.c │ │ ├── image-cramfs.c │ │ ├── image-ext2.c │ │ ├── image-f2fs.c │ │ ├── image-file.c │ │ ├── image-fip.c │ │ ├── image-fit.c │ │ ├── image-flash.c │ │ ├── image-hd.c │ │ ├── image-iso.c │ │ ├── image-jffs2.c │ │ ├── image-qemu.c │ │ ├── image-rauc.c │ │ ├── image-squashfs.c │ │ ├── image-tar.c │ │ ├── image-ubi.c │ │ ├── image-ubifs.c │ │ ├── image-vfat.c │ │ ├── list.h │ │ ├── m4 │ │ └── attributes.m4 │ │ ├── test.config │ │ └── util.c ├── OpenixCard │ ├── AW_IMG_PARA.h │ ├── CMakeLists.txt │ ├── FEX2CFG.cpp │ ├── FEX2CFG.h │ ├── LOG.cpp │ ├── LOG.h │ ├── OpenixCard.cpp │ ├── OpenixCard.h │ ├── exception.h │ └── payloads │ │ ├── android.cpp │ │ ├── chip.h │ │ └── linux.cpp ├── OpenixIMG │ ├── CMakeLists.txt │ ├── include │ │ ├── IMAGEWTY.h │ │ └── OpenixIMG.h │ ├── lib │ │ ├── rc6 │ │ │ ├── CMakeLists.txt │ │ │ └── src │ │ │ │ ├── rc6.c │ │ │ │ └── rc6.h │ │ └── twofish │ │ │ ├── CMakeLists.txt │ │ │ └── src │ │ │ ├── std_defs.h │ │ │ ├── twofish.c │ │ │ └── twofish.h │ ├── src │ │ └── OpenixIMG.c │ └── test │ │ └── T_OpenixIMG.c └── main.cpp └── test └── .gitignore /.github/workflows/cmake.yml: -------------------------------------------------------------------------------- 1 | name: CMake 2 | 3 | on: 4 | push: 5 | branches: [ master, dev ] 6 | pull_request: 7 | branches: [ master, dev ] 8 | 9 | env: 10 | # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) 11 | BUILD_TYPE: Release 12 | 13 | jobs: 14 | build: 15 | # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. 16 | # You can convert this to a matrix build if you need cross-platform coverage. 17 | # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix 18 | runs-on: ubuntu-20.04 19 | steps: 20 | - uses: actions/checkout@v3 21 | with: 22 | submodules: 'true' 23 | 24 | - name: Install dependence 25 | run: sudo apt-get install -y libconfuse-dev pkg-config autoconf automake 26 | 27 | - name: Binutils 28 | run: wget https://ftp.gnu.org/gnu/binutils/binutils-2.38.tar.xz && tar xvf binutils-2.38.tar.xz && cd binutils-2.38 && ./configure --prefix=/usr/local && make -j && sudo make install 29 | 30 | - name: Configure CMake 31 | # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. 32 | # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type 33 | run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} 34 | 35 | - name: Build 36 | # Build your program with the given configuration 37 | run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j 38 | 39 | - name: pack 40 | run: cd ${{github.workspace}}/build && cpack 41 | 42 | - name: Get test 43 | run: cd ${{github.workspace}}/test && wget https://github.com/YuzukiTsuru/OpenixCard/releases/download/TEST/tina_d1-h-yuzuki_ruler_pro_uart0.img.bin 44 | 45 | - name: Prepare test 46 | run: cd ${{github.workspace}}/test && mv tina_d1-h-yuzuki_ruler_pro_uart0.img.bin tina_d1-h-yuzuki_ruler_pro_uart0.img 47 | 48 | - name: Test Dump 49 | run: cd ${{github.workspace}}/build/dist && ./OpenixCard -d ${{github.workspace}}/test/tina_d1-h-yuzuki_ruler_pro_uart0.img 50 | 51 | - name: Test Unpack & cfg 52 | run: cd ${{github.workspace}}/build/dist && ./OpenixCard -uc ${{github.workspace}}/test/tina_d1-h-yuzuki_ruler_pro_uart0.img 53 | 54 | - name: Test Pack 55 | run: cd ${{github.workspace}}/build/dist && ./OpenixCard -p ${{github.workspace}}/test/tina_d1-h-yuzuki_ruler_pro_uart0.img.dump 56 | 57 | - name: Test Size 58 | run: cd ${{github.workspace}}/build/dist && ./OpenixCard -s ${{github.workspace}}/test/tina_d1-h-yuzuki_ruler_pro_uart0.img 59 | -------------------------------------------------------------------------------- /.github/workflows/sync.yml: -------------------------------------------------------------------------------- 1 | name: Sync To Gitee 2 | 3 | on: [ push, delete, create ] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Sync to Gitee 10 | uses: wearerequired/git-mirror-action@master 11 | env: 12 | # 注意在 Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY 13 | SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }} 14 | with: 15 | # 注意替换为你的 GitHub 源仓库地址 16 | source-repo: git@github.com:YuzukiTsuru/OpenixCard.git 17 | # 注意替换为你的 Gitee 目标仓库地址 18 | destination-repo: git@gitee.com:GloomyGhost/OpenixCard.git 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | awflash 2 | awimage 3 | log2bin 4 | parsecfg 5 | *.img 6 | *.dump 7 | *.cfg 8 | *.decrypted 9 | 10 | ### JetBrains template 11 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 12 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 13 | 14 | # User-specific stuff 15 | .idea/**/workspace.xml 16 | .idea/**/tasks.xml 17 | .idea/**/usage.statistics.xml 18 | .idea/**/dictionaries 19 | .idea/**/shelf 20 | 21 | # Generated files 22 | .idea/**/contentModel.xml 23 | 24 | # Sensitive or high-churn files 25 | .idea/**/dataSources/ 26 | .idea/**/dataSources.ids 27 | .idea/**/dataSources.local.xml 28 | .idea/**/sqlDataSources.xml 29 | .idea/**/dynamic.xml 30 | .idea/**/uiDesigner.xml 31 | .idea/**/dbnavigator.xml 32 | 33 | # Gradle 34 | .idea/**/gradle.xml 35 | .idea/**/libraries 36 | 37 | # Gradle and Maven with auto-import 38 | # When using Gradle or Maven with auto-import, you should exclude module files, 39 | # since they will be recreated, and may cause churn. Uncomment if using 40 | # auto-import. 41 | # .idea/artifacts 42 | # .idea/compiler.xml 43 | # .idea/jarRepositories.xml 44 | # .idea/modules.xml 45 | # .idea/*.iml 46 | # .idea/modules 47 | # *.iml 48 | # *.ipr 49 | 50 | # CMake 51 | cmake-build-*/ 52 | 53 | # Mongo Explorer plugin 54 | .idea/**/mongoSettings.xml 55 | 56 | # File-based project format 57 | *.iws 58 | 59 | # IntelliJ 60 | out/ 61 | 62 | # mpeltonen/sbt-idea plugin 63 | .idea_modules/ 64 | 65 | # JIRA plugin 66 | atlassian-ide-plugin.xml 67 | 68 | # Cursive Clojure plugin 69 | .idea/replstate.xml 70 | 71 | # Crashlytics plugin (for Android Studio and IntelliJ) 72 | com_crashlytics_export_strings.xml 73 | crashlytics.properties 74 | crashlytics-build.properties 75 | fabric.properties 76 | 77 | # Editor-based Rest Client 78 | .idea/httpRequests 79 | 80 | # Android studio 3.1+ serialized cache file 81 | .idea/caches/build_file_checksums.ser 82 | 83 | ### Linux template 84 | *~ 85 | 86 | # temporary files which can be created if a process still has a handle open of a deleted file 87 | .fuse_hidden* 88 | 89 | # KDE directory preferences 90 | .directory 91 | 92 | # Linux trash folder which might appear on any partition or disk 93 | .Trash-* 94 | 95 | # .nfs files are created when an open file is removed but is still being accessed 96 | .nfs* 97 | 98 | ### CMake template 99 | CMakeLists.txt.user 100 | CMakeCache.txt 101 | CMakeFiles 102 | CMakeScripts 103 | Testing 104 | Makefile 105 | cmake_install.cmake 106 | install_manifest.txt 107 | compile_commands.json 108 | CTestTestfile.cmake 109 | _deps 110 | 111 | ### Example user template template 112 | ### Example user template 113 | 114 | # IntelliJ project files 115 | .idea 116 | *.iml 117 | out 118 | gen 119 | ### Windows template 120 | # Windows thumbnail cache files 121 | Thumbs.db 122 | Thumbs.db:encryptable 123 | ehthumbs.db 124 | ehthumbs_vista.db 125 | 126 | # Dump file 127 | *.stackdump 128 | 129 | # Folder config file 130 | [Dd]esktop.ini 131 | 132 | # Recycle Bin used on file shares 133 | $RECYCLE.BIN/ 134 | 135 | # Windows Installer files 136 | *.cab 137 | *.msi 138 | *.msix 139 | *.msm 140 | *.msp 141 | 142 | # Windows shortcuts 143 | *.lnk 144 | 145 | ### macOS template 146 | # General 147 | .DS_Store 148 | .AppleDouble 149 | .LSOverride 150 | 151 | # Icon must end with two \r 152 | Icon 153 | 154 | # Thumbnails 155 | ._* 156 | 157 | # Files that might appear in the root of a volume 158 | .DocumentRevisions-V100 159 | .fseventsd 160 | .Spotlight-V100 161 | .TemporaryItems 162 | .Trashes 163 | .VolumeIcon.icns 164 | .com.apple.timemachine.donotpresent 165 | 166 | # Directories potentially created on remote AFP share 167 | .AppleDB 168 | .AppleDesktop 169 | Network Trash Folder 170 | Temporary Items 171 | .apdisk 172 | 173 | /lib/libfex/ 174 | build/ -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "lib/argparse"] 2 | path = lib/argparse 3 | url = https://github.com/p-ranav/argparse 4 | [submodule "lib/ftxui"] 5 | path = lib/ftxui 6 | url = https://github.com/ArthurSonzogni/FTXUI 7 | [submodule "lib/ColorCout"] 8 | path = lib/ColorCout 9 | url = https://github.com/YuzukiTsuru/ColorCout 10 | [submodule "lib/cpp-subprocess"] 11 | path = lib/cpp-subprocess 12 | url = https://github.com/arun11299/cpp-subprocess 13 | [submodule "lib/inicpp"] 14 | path = lib/inicpp 15 | url = https://github.com/SemaiCZE/inicpp 16 | -------------------------------------------------------------------------------- /CMake/cpack.cmake: -------------------------------------------------------------------------------- 1 | set(CPACK_GENERATOR "DEB" "TGZ" "STGZ" "ZIP") 2 | 3 | set(CPACK_PACKAGE_VENDOR "YuzukiTsuru") 4 | set(CPACK_PACKAGE_DESCRIPTION "Open Source Version of Allwinner PhoenixCard to Dump, Unpack, Flash Allwinner IMG Files") 5 | 6 | set(CPACK_DEBIAN_PACKAGE_NAME "OpenixCard") 7 | set(CPACK_DEBIAN_PACKAGE_MAINTAINER "YuzukiTsuru") 8 | set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "Open Source Version of Allwinner PhoenixCard to Dump, Unpack, Flash Allwinner IMG Files") 9 | set(CPACK_DEBIAN_PACKAGE_VERSION ${PACKAGE_VERSION}) 10 | set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) 11 | set(CPACK_DEBIAN_PACKAGE_DEPENDS "libconfuse-dev") 12 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11) 2 | 3 | # use git version as library version 4 | find_package(Git QUIET) 5 | if (Git_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git") 6 | execute_process( 7 | COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD 8 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 9 | OUTPUT_VARIABLE _git_version 10 | OUTPUT_STRIP_TRAILING_WHITESPACE 11 | ) 12 | execute_process( 13 | COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%h 14 | OUTPUT_VARIABLE PROJECT_GIT_HASH 15 | OUTPUT_STRIP_TRAILING_WHITESPACE 16 | ERROR_QUIET 17 | WORKING_DIRECTORY 18 | ${CMAKE_CURRENT_SOURCE_DIR} 19 | ) 20 | else () 21 | set(_git_version 0) 22 | set(PROJECT_GIT_HASH "") 23 | endif () 24 | 25 | project(OpenixCard LANGUAGES C CXX VERSION ${_git_version}) 26 | 27 | set(CMAKE_CXX_STANDARD 17) 28 | 29 | if (MSVC) 30 | message(ERROR "OpenixCard does not support MSVC") 31 | else () 32 | set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/dist") 33 | 34 | find_package(Threads REQUIRED) 35 | 36 | set(FTXUI_BUILD_DOCS OFF CACHE BOOL "" FORCE) 37 | set(FTXUI_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) 38 | set(FTXUI_ENABLE_INSTALL OFF CACHE BOOL "" FORCE) 39 | set(INICPP_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) 40 | set(INICPP_BUILD_TESTS OFF CACHE BOOL "" FORCE) 41 | 42 | add_subdirectory(lib/inicpp EXCLUDE_FROM_ALL) 43 | add_subdirectory(lib/argparse EXCLUDE_FROM_ALL) 44 | add_subdirectory(lib/ftxui EXCLUDE_FROM_ALL) 45 | 46 | configure_file( 47 | "${PROJECT_SOURCE_DIR}/config.h.in" 48 | "${PROJECT_BINARY_DIR}/config.h" 49 | ) 50 | 51 | include_directories( 52 | ${PROJECT_BINARY_DIR} 53 | src/OpenixIMG/include 54 | src/OpenixCard 55 | src/GenIMG 56 | src/OpenixCard/payloads 57 | src/GenIMG/genimage-src 58 | src/OpenixIMG/lib/rc6/src 59 | src/OpenixIMG/lib/twofish/src 60 | lib/ColorCout/includes 61 | lib/argparse/include 62 | lib/inicpp/include 63 | lib/cpp-subprocess 64 | lib/ftxui/include 65 | ) 66 | 67 | add_subdirectory(src) 68 | 69 | install( 70 | TARGETS OpenixCard 71 | EXPORT OpenixCard 72 | COMPONENT applications 73 | RUNTIME DESTINATION bin 74 | ) 75 | 76 | include(CMake/cpack.cmake) 77 | include(CPack) 78 | endif () 79 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | GloomyGhost@GloomyGhost.com. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenixCard 2 | 3 | Open Source Version of Allwinner PhoenixCard to Dump, Unpack, Flash Allwinner Linux IMG Files on Linux 4 | 5 | [![forthebadge](https://forthebadge.com/images/badges/made-with-c-plus-plus.svg)](https://forthebadge.com) 6 | [![forthebadge](https://forthebadge.com/images/badges/made-with-c.svg)](https://forthebadge.com) 7 | [![forthebadge](https://forthebadge.com/images/badges/powered-by-black-magic.svg)](https://forthebadge.com) 8 | [![forthebadge](https://forthebadge.com/images/badges/uses-git.svg)](https://forthebadge.com) 9 | 10 | [![CMake](https://github.com/YuzukiTsuru/OpenixCard/actions/workflows/cmake.yml/badge.svg)](https://github.com/YuzukiTsuru/OpenixCard/actions/workflows/cmake.yml) 11 | 12 | ## About Android IMG File Supporting 13 | > Android firmware will not support and will not adapt to support it in the future. Due to the large number of Android GKI, GMS, and GRF versions, it is impossible to cover all firmware versions, and Android firmware partitioning is very complex. There is no universal method to generate usable firmware, nor is there a fixed address to make it run. Even if it can be adapted, there will be situations where the functionality is unavailable like fastboot, GMS service and more. 14 | > Finally, Android firmware modifications are often used to crack and modify firmware, and this project does not support such behavior 15 | 16 | ## Usage 17 | 18 | ``` 19 | _____ _ _____ _ 20 | | |___ ___ ___|_|_ _| |___ ___ _| | 21 | | | | . | -_| | |_'_| --| .'| _| . | 22 | |_____| _|___|_|_|_|_,_|_____|__,|_| |___| 23 | |_| 24 | Copyright (c) 2022, YuzukiTsuru 25 | 26 | Usage: OpenixCard [options] input 27 | 28 | Positional arguments: 29 | input Input image file or directory path [required] 30 | 31 | Optional arguments: 32 | -h --help shows help message and exits [default: false] 33 | -v --version prints version information and exits [default: false] 34 | -u --unpack Unpack Allwinner Image to folder [default: false] 35 | -d --dump Convert Allwinner image to regular image [default: false] 36 | -c --cfg Get Allwinner image partition table cfg file (use together with unpack) [default: false] 37 | -p --pack pack dumped Allwinner image to regular image from folder (needs cfg file) [default: false] 38 | -s --size Get the accurate size of Allwinner image [default: false] 39 | 40 | eg.: 41 | OpenixCard -u - Unpack Allwinner image to target 42 | OpenixCard -uc - Unpack Allwinner image to target and generate Allwinner image partition table cfg 43 | OpenixCard -d - Convert Allwinner image to regular image 44 | OpenixCard -p - pack dumped Allwinner image to regular image from folder 45 | OpenixCard -s - Get the accurate size of Allwinner image 46 | ``` 47 | 48 | ## Download 49 | ### ArchLinux 50 | OpenixCard Now available at [AUR](https://aur.archlinux.org/packages/openixcard) [#3](https://github.com/YuzukiTsuru/OpenixCard/issues/3#issuecomment-1135317155) 51 | ``` 52 | yay -S openixcard 53 | ``` 54 | 55 | ### Other Linux 56 | You can find the new release file: 57 | https://github.com/YuzukiTsuru/OpenixCard/releases 58 | 59 | ## Build from source 60 | 61 | ``` 62 | # Download the source code 63 | git clone --recursive --depth 1 https://github.com/YuzukiTsuru/OpenixCard 64 | 65 | # Download the depends 66 | sudo apt install cmake build-essential automake autoconf libconfuse-dev pkg-config 67 | 68 | # Make build directory 69 | mkdir build 70 | cd build 71 | 72 | # Make 73 | cmake .. && make -j 74 | ``` 75 | 76 | > Note: Ubuntu 20.04 compilation will report an error, This is caused by the bug of ar, you can compile and install the new version. 77 | 78 | ``` 79 | sudo apt-get install texinfo 80 | 81 | wget https://ftp.gnu.org/gnu/binutils/binutils-2.38.tar.xz && \ 82 | tar xvf binutils-2.38.tar.xz && \ 83 | cd binutils-2.38 && \ 84 | ./configure --prefix=/usr/local && \ 85 | make 86 | 87 | sudo make install 88 | ``` 89 | 90 | ## LICENSE 91 | ``` 92 | GNU GENERAL PUBLIC LICENSE Version 2, June 1991 93 | 94 | Copyright (c) 2022, YuzukiTsuru 95 | 96 | This program is free software; you can redistribute it and/or modify 97 | it under the terms of the GNU General Public License version 2 as 98 | published by the Free Software Foundation. 99 | 100 | See README and LICENSE for more details. 101 | ``` 102 | -------------------------------------------------------------------------------- /config.h.in: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gloom on 2022/4/28. 3 | // 4 | 5 | #ifndef OPENIXCARD_CONFIG_H_IN_H 6 | #define OPENIXCARD_CONFIG_H_IN_H 7 | 8 | #define PROJECT_NAME "@PROJECT_NAME@" 9 | #define PROJECT_VER "@PROJECT_VERSION@" 10 | #define PROJECT_GIT_HASH "@PROJECT_GIT_HASH@" 11 | #define PROJECT_VER_MAJOR "@PROJECT_VERSION_MAJOR@" 12 | #define PROJECT_VER_MINOR "@PROJECT_VERSION_MINOR@" 13 | #define PTOJECT_VER_PATCH "@PROJECT_VERSION_PATCH@" 14 | 15 | #endif //OPENIXCARD_CONFIG_H_IN_H 16 | -------------------------------------------------------------------------------- /docs/img_decode/config.mk: -------------------------------------------------------------------------------- 1 | 2 | ## 3 | ## Makefile for Sunxi Secure Boot 4 | ## 5 | 6 | ######################################################################### 7 | # clean the slate ... 8 | PLATFORM_RELFLAGS = 9 | PLATFORM_CPPFLAGS = 10 | PLATFORM_LDFLAGS = 11 | 12 | ######################################################################### 13 | 14 | HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer \ 15 | $(HOSTCPPFLAGS) 16 | HOSTSTRIP = strip 17 | 18 | # 19 | # Include the make variables (CC, etc...) 20 | # 21 | 22 | CROSS_COMPILE ?= 23 | 24 | AS = $(CROSS_COMPILE)as 25 | LD = $(CROSS_COMPILE)ld 26 | CC = $(CROSS_COMPILE)gcc 27 | CPP = $(CC) -E 28 | AR = $(CROSS_COMPILE)ar 29 | NM = $(CROSS_COMPILE)nm 30 | LDR = $(CROSS_COMPILE)ldr 31 | STRIP = $(CROSS_COMPILE)strip 32 | OBJCOPY = $(CROSS_COMPILE)objcopy 33 | OBJDUMP = $(CROSS_COMPILE)objdump 34 | RANLIB = $(CROSS_COMPILE)RANLIB 35 | 36 | ifneq (,$(findstring s,$(MAKEFLAGS))) 37 | ARFLAGS = cr 38 | else 39 | ARFLAGS = crv 40 | endif 41 | 42 | CPPFLAGS := $(DBGFLAGS) $(OPTFLAGS) $(RELFLAGS) 43 | 44 | CPPFLAGS = 45 | 46 | CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes 47 | 48 | CFLAGS += $(call cc-option,-fno-stack-protector) 49 | # Some toolchains enable security related warning flags by default, 50 | # but they don't make much sense in the u-boot world, so disable them. 51 | 52 | INCLUDES = -I$(OBJTREE) \ 53 | -I$(OBJTREE)/include \ 54 | -I$(COMMONDIR)/include 55 | 56 | 57 | CPPFLAGS += $(INCLUDES) 58 | CFLAGS += $(INCLUDES) 59 | 60 | # $(CPPFLAGS) sets -g, which causes gcc to pass a suitable -g 61 | # option to the assembler. 62 | AFLAGS_DEBUG := 63 | 64 | 65 | AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CPPFLAGS) 66 | 67 | LDFLAGS += $(PLATFORM_LDFLAGS) 68 | LDFLAGS += $(INCLUDE) 69 | ######################################################################### 70 | 71 | export HOSTCC HOSTCFLAGS HOSTLDFLAGS PEDCFLAGS HOSTSTRIP CROSS_COMPILE \ 72 | AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP MAKE 73 | export CONFIG_SYS_TEXT_BASE PLATFORM_CPPFLAGS PLATFORM_RELFLAGS CPPFLAGS CFLAGS AFLAGS 74 | 75 | ######################################################################### 76 | 77 | # Allow boards to use custom optimize flags on a per dir/file basis 78 | BCURDIR = $(subst $(SRCTREE)/,,$(CURDIR:$(obj)%=%)) 79 | ALL_AFLAGS = $(AFLAGS) $(AFLAGS_$(BCURDIR)/$(@F)) $(AFLAGS_$(BCURDIR)) 80 | ALL_CFLAGS = $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) 81 | $(obj)%.s: %.S 82 | @$(CPP) $(ALL_AFLAGS) -o $@ $< 83 | @echo " CPP "$< ... 84 | $(obj)%.o: %.S 85 | @$(CC) $(ALL_AFLAGS) -o $@ $< -c 86 | @echo " CC "$< ... 87 | $(obj)%.o: %.c 88 | @$(CC) $(ALL_CFLAGS) -o $@ $< -c 89 | @echo " CC "$< ... 90 | $(obj)%.i: %.c 91 | @$(CPP) $(ALL_CFLAGS) -o $@ $< -c 92 | @echo " CPP "$< ... 93 | $(obj)%.s: %.c 94 | @$(CC) $(ALL_CFLAGS) -o $@ $< -c -S 95 | @echo " CPP "$< ... 96 | 97 | ######################################################################### 98 | 99 | # If the list of objects to link is empty, just create an empty built-in.o 100 | cmd_link_o_target = $(if $(strip $1),\ 101 | $(LD) $(LDFLAGS) -r -o $@ $1,\ 102 | rm -f $@; $(AR) rcs $@ ) 103 | 104 | ######################################################################### 105 | -------------------------------------------------------------------------------- /docs/img_decode/firmware/imagefile_new.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuzukiTsuru/OpenixCard/dc7efda6949659fa9f6d8008cffd5066b90b9160/docs/img_decode/firmware/imagefile_new.h -------------------------------------------------------------------------------- /docs/img_decode/firmware/imgdecode.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuzukiTsuru/OpenixCard/dc7efda6949659fa9f6d8008cffd5066b90b9160/docs/img_decode/firmware/imgdecode.c -------------------------------------------------------------------------------- /docs/img_decode/firmware/imgdecode.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuzukiTsuru/OpenixCard/dc7efda6949659fa9f6d8008cffd5066b90b9160/docs/img_decode/firmware/imgdecode.h -------------------------------------------------------------------------------- /docs/img_decode/main/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) Copyright 2016 3 | * Allwinner Technology Co., Ltd. 4 | * SPDX-License-Identifier: GPL-2.0+ 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include "firmware/imgdecode.h" 11 | 12 | 13 | #define PARSER_BUFFER_MAX (32 * 1024 * 1024) 14 | static void *img_hd = NULL; 15 | 16 | static int img_parser_file(void *img_hd, char *name, char *out_dir); 17 | static int img_list_files(void *img_hd); 18 | static int decode_all_item(void *img_hd, char *out_dir); 19 | 20 | static void usage(void) 21 | { 22 | printf("*************************start**************************\n"); 23 | printf("* *\n"); 24 | printf(" the usage of imgdecode:\n"); 25 | printf(" when the program can work well, it will make a directory named imgdecode to store the files.\n"); 26 | printf(" If the directory exists already, this step would be ignored\n"); 27 | printf(" imgdecode imgname decode all files in the firmware\n"); 28 | printf(" imadecode imgname -list list all the mainkey and subkey in the img\n"); 29 | printf(" imadecode imgname -name SUBKEY decode the files whose sub key = SUBKEY\n"); 30 | printf("* *\n"); 31 | printf("**************************end***************************\n"); 32 | 33 | return; 34 | } 35 | 36 | int main(int argc, char* argv[]) 37 | { 38 | char *keyname = NULL; 39 | int i, list_cmd = 0; 40 | char cmdline[1024]; 41 | char imgfullname[MAX_PATH] = ""; 42 | char tmpdirpath[MAX_PATH]; 43 | FILE *img_file; 44 | int decode_all = 0; 45 | 46 | if (argc == 2) { 47 | printf("findout all files\n"); 48 | decode_all = 1; 49 | } else if ((argc != 3) && (argc != 4)) { 50 | printf("Dragon Img Decode: not enough parameters\n"); 51 | usage(); 52 | 53 | return -1; 54 | } 55 | 56 | for (i = 2; i < argc; i += 2) { 57 | if (!strcmp(argv[i], "-name")) { 58 | keyname = argv[i + 1]; 59 | printf("findout all files whose main name is %s\n", keyname); 60 | } else if (!strcmp(argv[i], "-list")) { 61 | i--; 62 | printf("try to list all the subkey in the image\n"); 63 | printf("all the other command will be ingored\n"); 64 | list_cmd = 1; 65 | } else { 66 | printf("Dragon Img Decode: Unknown command\n"); 67 | 68 | usage(); 69 | return -1; 70 | } 71 | } 72 | 73 | GetFullPath(imgfullname, argv[1]); 74 | printf("imgpath=%s\n", imgfullname); 75 | 76 | img_file = fopen(imgfullname, "rb"); 77 | 78 | if (img_file == NULL) { 79 | printf("Dragon Img Decode: The file cant be open\n"); 80 | usage(); 81 | 82 | return -1; 83 | } 84 | 85 | fclose(img_file); 86 | 87 | img_hd = Img_Open(imgfullname); 88 | 89 | if (!img_hd) { 90 | printf("Dragon Img Decode: the iamge file is invalid\n"); 91 | 92 | return -1; 93 | } 94 | 95 | if (list_cmd) { 96 | img_list_files(img_hd); 97 | 98 | return 0; 99 | } 100 | 101 | memset(tmpdirpath, 0, MAX_PATH); 102 | GetFullPath(tmpdirpath, "imgout"); 103 | 104 | memset(cmdline, 0, 1024); 105 | sprintf(cmdline, "rm -rf %s", tmpdirpath); 106 | system(cmdline); 107 | 108 | memset(cmdline, 0, 1024); 109 | sprintf(cmdline, "mkdir -p %s", tmpdirpath); 110 | system(cmdline); 111 | 112 | if (decode_all) { 113 | decode_all_item(img_hd, tmpdirpath); 114 | } else { 115 | if (img_parser_file(img_hd, keyname, tmpdirpath)) { 116 | usage(); 117 | 118 | return -1; 119 | } 120 | } 121 | 122 | return 0; 123 | } 124 | 125 | static int img_parser_file(void *img_hd, char *name, char *out_dir) 126 | { 127 | char outfullpath[MAX_PATH]; 128 | FILE *dedicate_file = NULL; 129 | long long file_len, tmp_file_len; 130 | char *buffer; 131 | uint file_offset, read_len; 132 | void *item_hd; 133 | int ret = -1; 134 | 135 | item_hd = Img_OpenItem(img_hd, name); 136 | 137 | if (!item_hd) { 138 | printf("Dragon Img Decode: the wanted file is not exist\n"); 139 | 140 | return -1; 141 | } 142 | 143 | file_len = Img_GetItemSize(img_hd, item_hd); 144 | 145 | if (!file_len) { 146 | printf("Dragon Img Decode: the dedicate file length is 0\n"); 147 | 148 | goto __parser_img_out; 149 | } 150 | 151 | memset(outfullpath, 0, MAX_PATH); 152 | sprintf(outfullpath, "%s/%s.bin", out_dir, name); 153 | 154 | dedicate_file = fopen(outfullpath, "wb"); 155 | 156 | if (dedicate_file == NULL) { 157 | printf("Dragon Img Decode: unable to create the dedicate file\n"); 158 | 159 | goto __parser_img_out; 160 | } 161 | 162 | buffer = (char *)malloc(PARSER_BUFFER_MAX); 163 | 164 | if (buffer == NULL) { 165 | printf("Dragon Img Decode: unable to malloc buffer to store data\n"); 166 | 167 | goto __parser_img_out; 168 | } 169 | 170 | file_offset = 0; 171 | tmp_file_len = file_len; 172 | 173 | while (tmp_file_len >= PARSER_BUFFER_MAX) { 174 | read_len = Img_ReadItem_Continue(img_hd, item_hd, buffer, PARSER_BUFFER_MAX, file_offset); 175 | 176 | if (read_len != PARSER_BUFFER_MAX) { 177 | printf("Dragon Img Decode: read(step1) dedicate file err\n"); 178 | 179 | goto __parser_img_out; 180 | } 181 | 182 | fwrite(buffer, PARSER_BUFFER_MAX, 1, dedicate_file); 183 | file_offset += PARSER_BUFFER_MAX; 184 | tmp_file_len -= PARSER_BUFFER_MAX; 185 | } 186 | 187 | if (tmp_file_len) { 188 | read_len = Img_ReadItem_Continue(img_hd, item_hd, buffer, (uint)tmp_file_len, file_offset); 189 | 190 | if (read_len != tmp_file_len) { 191 | printf("Dragon Img Decode: read(step2) dedicate file err\n"); 192 | 193 | goto __parser_img_out; 194 | } 195 | 196 | fwrite(buffer, (uint)tmp_file_len, 1, dedicate_file); 197 | } 198 | 199 | printf("successfully writing the dedicate file %s\n", outfullpath); 200 | ret = 0; 201 | 202 | __parser_img_out: 203 | 204 | if (dedicate_file) { 205 | fclose(dedicate_file); 206 | } 207 | 208 | if (buffer) { 209 | free(buffer); 210 | } 211 | 212 | if (item_hd) { 213 | Img_CloseItem(img_hd, item_hd); 214 | } 215 | 216 | return ret; 217 | } 218 | 219 | static int decode_all_item(void *image_name, char *out_dir) 220 | { 221 | u8 *name; 222 | int index; 223 | 224 | index = 0; 225 | 226 | printf("Ready to decode All Item Name\n\n"); 227 | 228 | do { 229 | name = Img_GetItem_Subname(img_hd, index); 230 | 231 | if (name != NULL) { 232 | index ++; 233 | 234 | if (img_parser_file(image_name, name, out_dir)) { 235 | return -1; 236 | } 237 | } else { 238 | break; 239 | } 240 | } while (1); 241 | 242 | return 0; 243 | } 244 | 245 | static int img_list_files(void *img_hd) 246 | { 247 | u8 *name; 248 | int index; 249 | 250 | index = 0; 251 | 252 | printf("Ready to List All Item Name\n\n"); 253 | 254 | do { 255 | name = Img_GetItem_Subname(img_hd, index); 256 | 257 | if (name != NULL) { 258 | index ++; 259 | printf("Item %4d: %s\n", index, name); 260 | } else { 261 | break; 262 | } 263 | } while (1); 264 | 265 | return 0; 266 | } 267 | 268 | 269 | -------------------------------------------------------------------------------- /docs/img_decode/rules.mk: -------------------------------------------------------------------------------- 1 | # 2 | # (C) Copyright 2006 3 | # Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 | # 5 | # See file CREDITS for list of people who contributed to this 6 | # project. 7 | # 8 | # This program is free software; you can redistribute it and/or 9 | # modify it under the terms of the GNU General Public License as 10 | # published by the Free Software Foundation; either version 2 of 11 | # the License, or (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program; if not, write to the Free Software 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 | # MA 02111-1307 USA 22 | # 23 | 24 | ######################################################################### 25 | 26 | _depend: $(obj).depend 27 | 28 | $(obj).depend: $(src)Makefile $(TOPDIR)/config.mk $(SRCS) $(HOSTSRCS) 29 | @rm -f $@ 30 | @touch $@ 31 | @for f in $(SRCS); do \ 32 | g=`basename $$f | sed -e 's/\(.*\)\.[[:alnum:]_]/\1.o/'`; \ 33 | $(CC) -M $(CPPFLAGS) -MQ $(obj)$$g $$f >> $@ ; \ 34 | done 35 | @for f in $(HOSTSRCS); do \ 36 | g=`basename $$f | sed -e 's/\(.*\)\.[[:alnum:]_]/\1.o/'`; \ 37 | $(HOSTCC) -M $(HOSTCPPFLAGS) -MQ $(obj)$$g $$f >> $@ ; \ 38 | done 39 | 40 | $(HOSTOBJS): $(obj)%.o: %.c 41 | $(HOSTCC) $(HOSTCFLAGS) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c 42 | $(NOPEDOBJS): $(obj)%.o: %.c 43 | $(HOSTCC) $(HOSTCFLAGS_NOPED) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c 44 | 45 | ######################################################################### 46 | -------------------------------------------------------------------------------- /docs/pach.txt: -------------------------------------------------------------------------------- 1 | /home/yuzuki/WorkSpace/tina-d1-open/scripts/pack_img.sh -c sun20iw1p1 -p tina -b d1-lichee_rv -d uart0 -s none -m normal -w none -v none -i none -t /home/yuzuki/WorkSpace/tina-d1-open -------------------------------------------------------------------------------- /snap/snapcraft.yaml: -------------------------------------------------------------------------------- 1 | name: openixcard 2 | adopt-info: openixcard 3 | summary: Open Source Version of Allwinner PhoenixCard to Dump, Unpack, Flash Allwinner IMG Files on Linux. 4 | description: | 5 | OpenixCard is a Open Source Version of Allwinner PhoenixCard to Dump, Unpack, Flash Allwinner IMG Files on Linux, Support Linux images. 6 | grade: stable 7 | confinement: strict 8 | icon: "./favicon.png" 9 | apps: 10 | openixcard: 11 | command: openixcard 12 | base: core18 13 | parts: 14 | openixcard: 15 | plugin: cmake 16 | source: https://github.com/YuzukiTsuru/OpenixCard 17 | source-type: git 18 | source-depth: 1 19 | build-snaps: [cmake/latest/edge] 20 | build-packages: 21 | - pkg-config 22 | - libconfuse-dev 23 | - automake 24 | - autoconf 25 | - aclocal 26 | override-build: | 27 | /snap/bin/cmake \ 28 | -DCMAKE_INSTALL_PREFIX=$SNAPCRAFT_PART_INSTALL/usr/local \ 29 | -DCMAKE_BUILD_TYPE=Release \ 30 | $SNAPCRAFT_PART_SRC 31 | make -j 4 32 | make install 33 | snapcraftctl set-version "$(git rev-list --count HEAD)" 34 | passthrough: 35 | title: OpenixCard 36 | license: GPL -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(GenIMG) 2 | add_subdirectory(OpenixIMG) 3 | add_subdirectory(OpenixCard) 4 | 5 | # Main app 6 | add_executable(OpenixCard main.cpp) 7 | target_link_libraries(OpenixCard PRIVATE libOpenixCard OpenixIMG inicpp GenIMG) -------------------------------------------------------------------------------- /src/GenIMG/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Tool to build libgenimage 2 | 3 | file(GLOB GenIMGSource *.c) 4 | 5 | set(LIBGENIMAGE_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/GenIMG/genimage-src") 6 | set(LIBGENIMAGE_TARGET_BUILD_DIR "${CMAKE_BINARY_DIR}/thirdparty/genimage-src/") 7 | set(LIBGENIMAGE_TARGET_LIBRARY "${CMAKE_BINARY_DIR}/thirdparty/genimage-src/libgenimage.a") 8 | 9 | add_library(GenIMG ${GenIMGSource} GenIMG.cpp GenIMG.h GenimageWrapper.h) 10 | target_link_libraries(GenIMG PRIVATE ${LIBGENIMAGE_TARGET_LIBRARY} Threads::Threads confuse) 11 | 12 | file(MAKE_DIRECTORY ${LIBGENIMAGE_TARGET_BUILD_DIR}) 13 | 14 | add_custom_command( 15 | TARGET GenIMG 16 | PRE_LINK 17 | COMMAND cp -rf "${LIBGENIMAGE_SOURCE_DIR}/*" "${LIBGENIMAGE_TARGET_BUILD_DIR}" 18 | ) 19 | 20 | # autotools build 21 | add_custom_command( 22 | TARGET GenIMG 23 | PRE_LINK 24 | WORKING_DIRECTORY ${LIBGENIMAGE_TARGET_BUILD_DIR} 25 | COMMAND aclocal 26 | ) 27 | 28 | add_custom_command( 29 | TARGET GenIMG 30 | PRE_LINK 31 | WORKING_DIRECTORY ${LIBGENIMAGE_TARGET_BUILD_DIR} 32 | COMMAND autoconf -i && automake --add-missing 33 | ) 34 | 35 | add_custom_command( 36 | TARGET GenIMG 37 | PRE_LINK 38 | WORKING_DIRECTORY ${LIBGENIMAGE_TARGET_BUILD_DIR} 39 | COMMAND chmod a+x configure && ./configure && make 40 | ) 41 | -------------------------------------------------------------------------------- /src/GenIMG/GenIMG.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * GenIMG.cpp 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "GenIMG.h" 21 | #include "exception.h" 22 | 23 | extern "C" { 24 | #include "GenimageWrapper.h" 25 | } 26 | 27 | [[maybe_unused]] GenIMG::GenIMG(std::string config_path, std::string image_path, std::string output_path) 28 | : config_path(std::move(config_path)) 29 | , image_path(std::move(image_path)) 30 | , output_path(std::move(output_path)) 31 | { 32 | // generate blank.fex file for commented partition 33 | generate_blank_fex(); 34 | 35 | generate_tmp_dir(); 36 | // call genimage-src 37 | run_genimage(); 38 | } 39 | 40 | void GenIMG::generate_tmp_dir() 41 | { 42 | for (int i = 0; i < 2; ++i) 43 | temp_dir.emplace_back([]() -> std::string { 44 | auto dir_name_str = std::string(subprocess::check_output({ "mktemp", "-d" }).buf.data()); 45 | size_t start_pos = 0; 46 | while ((start_pos = dir_name_str.find('\n', start_pos)) != std::string::npos) { 47 | dir_name_str.replace(start_pos, 1, ""); 48 | start_pos += 1; 49 | } 50 | return dir_name_str; 51 | }()); 52 | } 53 | 54 | void GenIMG::run_genimage() 55 | { 56 | char arg0[] = "OpenixCard"; 57 | char arg1[] = "--config"; 58 | char arg2[] = "--rootpath"; 59 | char arg3[] = "--tmppath"; 60 | char arg4[] = "--inputpath"; 61 | char arg5[] = "--outputpath"; 62 | char* argv[] = { 63 | &arg0[0], 64 | &arg1[0], const_cast(this->config_path.c_str()), 65 | &arg2[0], const_cast(temp_dir[0].c_str()), 66 | &arg3[0], const_cast(temp_dir[1].c_str()), 67 | &arg4[0], const_cast(this->image_path.c_str()), 68 | &arg5[0], const_cast(this->output_path.c_str()), 69 | nullptr 70 | }; 71 | 72 | int argc = static_cast((sizeof(argv) / sizeof(argv[0]))) - 1; 73 | 74 | std::cout << cc::cyan; 75 | status = GenimageWrapper(argc, argv); 76 | status != 0 ? std::cout << cc::red : std::cout << cc::reset; 77 | std::cout << cc::reset; 78 | } 79 | 80 | [[maybe_unused]] void GenIMG::print() 81 | { 82 | std::cout << "\tconfig_path: " << this->config_path << std::endl; 83 | std::cout << "\timage_path: " << this->image_path << std::endl; 84 | std::cout << "\toutput_path: " << this->output_path << std::endl; 85 | std::cout << "\ttemp_dir[0]: " << this->temp_dir[0] << std::endl; 86 | std::cout << "\ttemp_dir[1]: " << this->temp_dir[0] << std::endl; 87 | } 88 | 89 | void GenIMG::generate_blank_fex() 90 | { 91 | std::ofstream out(this->image_path + "/blank.fex"); 92 | // File not open, throw error. 93 | if (!out.is_open()) { 94 | throw file_open_error(this->image_path + "/blank.fex"); 95 | } 96 | out << "blank.fex"; 97 | out.close(); 98 | } 99 | 100 | [[maybe_unused]] int GenIMG::get_status() const 101 | { 102 | return this->status; 103 | } 104 | 105 | void GenIMG::re_run_genimage(std::string _config_path, std::string _image_path, std::string _output_path) 106 | { 107 | config_path = std::move(_config_path); 108 | image_path = std::move(_image_path); 109 | output_path = std::move(_output_path); 110 | run_genimage(); 111 | } 112 | -------------------------------------------------------------------------------- /src/GenIMG/GenIMG.h: -------------------------------------------------------------------------------- 1 | /* 2 | * GenIMG.h 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #ifndef OPENIXCARD_GENIMG_H 13 | #define OPENIXCARD_GENIMG_H 14 | 15 | #include 16 | 17 | class GenIMG { 18 | public: 19 | [[maybe_unused]] GenIMG(std::string config_path, std::string image_path, std::string output_path); 20 | 21 | [[maybe_unused]] void print(); 22 | 23 | [[maybe_unused]] [[nodiscard]] int get_status() const; 24 | 25 | void re_run_genimage(std::string _config_path, std::string _image_path, std::string _output_path); 26 | 27 | private: 28 | std::string config_path; 29 | std::string name; 30 | std::string image_path; 31 | std::string output_path; 32 | std::vector temp_dir = std::vector{}; 33 | 34 | int status = 0; 35 | 36 | private: 37 | void generate_tmp_dir(); 38 | 39 | // subprocess runner for genimage-src 40 | void run_genimage(); 41 | 42 | void generate_blank_fex(); 43 | }; 44 | 45 | 46 | #endif //OPENIXCARD_GENIMG_H 47 | -------------------------------------------------------------------------------- /src/GenIMG/GenimageWrapper.h: -------------------------------------------------------------------------------- 1 | /* 2 | * GenimageWrapper.c 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | 13 | #ifndef OPENIXCARD_GENIMAGEWRAPPER_H 14 | #define OPENIXCARD_GENIMAGEWRAPPER_H 15 | 16 | int GenimageWrapper(int argc, char *argv[]); 17 | 18 | #endif //OPENIXCARD_GENIMAGEWRAPPER_H 19 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/Makefile.am: -------------------------------------------------------------------------------- 1 | if BUILD_SILENTLY 2 | AM_MAKEFLAGS = --no-print-directory 3 | endif 4 | 5 | EXTRA_DIST = \ 6 | README.rst \ 7 | test.config \ 8 | flash.conf 9 | 10 | ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS} 11 | 12 | AM_CPPFLAGS = \ 13 | -include $(top_builddir)/config.h 14 | 15 | noinst_LIBRARIES = libgenimage.a 16 | libgenimage_a_SOURCES = \ 17 | config.c \ 18 | util.c \ 19 | crc32.c \ 20 | image-android-sparse.c \ 21 | image-cpio.c \ 22 | image-cramfs.c \ 23 | image-ext2.c \ 24 | image-f2fs.c \ 25 | image-file.c \ 26 | image-fip.c \ 27 | image-fit.c \ 28 | image-flash.c \ 29 | image-hd.c \ 30 | image-iso.c \ 31 | image-jffs2.c \ 32 | image-qemu.c \ 33 | image-rauc.c \ 34 | image-squashfs.c \ 35 | image-tar.c \ 36 | image-ubi.c \ 37 | image-ubifs.c \ 38 | image-vfat.c 39 | 40 | libgenimage_a_CFLAGS = \ 41 | $(AM_CFLAGS) \ 42 | $(CONFUSE_CFLAGS) 43 | 44 | libgenimage_a_LIBADD = \ 45 | $(CONFUSE_LIBS) 46 | 47 | noinst_HEADERS = \ 48 | genimage.h \ 49 | list.h 50 | 51 | # when "make clean" runs 52 | CLEANFILES = \ 53 | test-results/*.test*.counts 54 | 55 | # when "make distclean" runs 56 | DISTCLEAN = \ 57 | Makefile 58 | 59 | # when "make maintainer-clean" runs 60 | MAINTAINERCLEANFILES = \ 61 | configure \ 62 | autoscan.log \ 63 | config.h.in~ \ 64 | config.h.in \ 65 | configure.scan \ 66 | configure.ac~ \ 67 | aclocal.m4 \ 68 | Makefile.in \ 69 | build-aux/depcomp \ 70 | build-aux/install-sh \ 71 | build-aux/missing \ 72 | $(DIST_ARCHIVES) 73 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/build-aux/compile: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # Wrapper for compilers which do not understand '-c -o'. 3 | 4 | scriptversion=2018-03-07.03; # UTC 5 | 6 | # Copyright (C) 1999-2020 Free Software Foundation, Inc. 7 | # Written by Tom Tromey . 8 | # 9 | # This program is free software; you can redistribute it and/or modify 10 | # it under the terms of the GNU General Public License as published by 11 | # the Free Software Foundation; either version 2, or (at your option) 12 | # any later version. 13 | # 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU General Public License 20 | # along with this program. If not, see . 21 | 22 | # As a special exception to the GNU General Public License, if you 23 | # distribute this file as part of a program that contains a 24 | # configuration script generated by Autoconf, you may include it under 25 | # the same distribution terms that you use for the rest of that program. 26 | 27 | # This file is maintained in Automake, please report 28 | # bugs to or send patches to 29 | # . 30 | 31 | nl=' 32 | ' 33 | 34 | # We need space, tab and new line, in precisely that order. Quoting is 35 | # there to prevent tools from complaining about whitespace usage. 36 | IFS=" "" $nl" 37 | 38 | file_conv= 39 | 40 | # func_file_conv build_file lazy 41 | # Convert a $build file to $host form and store it in $file 42 | # Currently only supports Windows hosts. If the determined conversion 43 | # type is listed in (the comma separated) LAZY, no conversion will 44 | # take place. 45 | func_file_conv () 46 | { 47 | file=$1 48 | case $file in 49 | / | /[!/]*) # absolute file, and not a UNC file 50 | if test -z "$file_conv"; then 51 | # lazily determine how to convert abs files 52 | case `uname -s` in 53 | MINGW*) 54 | file_conv=mingw 55 | ;; 56 | CYGWIN* | MSYS*) 57 | file_conv=cygwin 58 | ;; 59 | *) 60 | file_conv=wine 61 | ;; 62 | esac 63 | fi 64 | case $file_conv/,$2, in 65 | *,$file_conv,*) 66 | ;; 67 | mingw/*) 68 | file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'` 69 | ;; 70 | cygwin/* | msys/*) 71 | file=`cygpath -m "$file" || echo "$file"` 72 | ;; 73 | wine/*) 74 | file=`winepath -w "$file" || echo "$file"` 75 | ;; 76 | esac 77 | ;; 78 | esac 79 | } 80 | 81 | # func_cl_dashL linkdir 82 | # Make cl look for libraries in LINKDIR 83 | func_cl_dashL () 84 | { 85 | func_file_conv "$1" 86 | if test -z "$lib_path"; then 87 | lib_path=$file 88 | else 89 | lib_path="$lib_path;$file" 90 | fi 91 | linker_opts="$linker_opts -LIBPATH:$file" 92 | } 93 | 94 | # func_cl_dashl library 95 | # Do a library search-path lookup for cl 96 | func_cl_dashl () 97 | { 98 | lib=$1 99 | found=no 100 | save_IFS=$IFS 101 | IFS=';' 102 | for dir in $lib_path $LIB 103 | do 104 | IFS=$save_IFS 105 | if $shared && test -f "$dir/$lib.dll.lib"; then 106 | found=yes 107 | lib=$dir/$lib.dll.lib 108 | break 109 | fi 110 | if test -f "$dir/$lib.lib"; then 111 | found=yes 112 | lib=$dir/$lib.lib 113 | break 114 | fi 115 | if test -f "$dir/lib$lib.a"; then 116 | found=yes 117 | lib=$dir/lib$lib.a 118 | break 119 | fi 120 | done 121 | IFS=$save_IFS 122 | 123 | if test "$found" != yes; then 124 | lib=$lib.lib 125 | fi 126 | } 127 | 128 | # func_cl_wrapper cl arg... 129 | # Adjust compile command to suit cl 130 | func_cl_wrapper () 131 | { 132 | # Assume a capable shell 133 | lib_path= 134 | shared=: 135 | linker_opts= 136 | for arg 137 | do 138 | if test -n "$eat"; then 139 | eat= 140 | else 141 | case $1 in 142 | -o) 143 | # configure might choose to run compile as 'compile cc -o foo foo.c'. 144 | eat=1 145 | case $2 in 146 | *.o | *.[oO][bB][jJ]) 147 | func_file_conv "$2" 148 | set x "$@" -Fo"$file" 149 | shift 150 | ;; 151 | *) 152 | func_file_conv "$2" 153 | set x "$@" -Fe"$file" 154 | shift 155 | ;; 156 | esac 157 | ;; 158 | -I) 159 | eat=1 160 | func_file_conv "$2" mingw 161 | set x "$@" -I"$file" 162 | shift 163 | ;; 164 | -I*) 165 | func_file_conv "${1#-I}" mingw 166 | set x "$@" -I"$file" 167 | shift 168 | ;; 169 | -l) 170 | eat=1 171 | func_cl_dashl "$2" 172 | set x "$@" "$lib" 173 | shift 174 | ;; 175 | -l*) 176 | func_cl_dashl "${1#-l}" 177 | set x "$@" "$lib" 178 | shift 179 | ;; 180 | -L) 181 | eat=1 182 | func_cl_dashL "$2" 183 | ;; 184 | -L*) 185 | func_cl_dashL "${1#-L}" 186 | ;; 187 | -static) 188 | shared=false 189 | ;; 190 | -Wl,*) 191 | arg=${1#-Wl,} 192 | save_ifs="$IFS"; IFS=',' 193 | for flag in $arg; do 194 | IFS="$save_ifs" 195 | linker_opts="$linker_opts $flag" 196 | done 197 | IFS="$save_ifs" 198 | ;; 199 | -Xlinker) 200 | eat=1 201 | linker_opts="$linker_opts $2" 202 | ;; 203 | -*) 204 | set x "$@" "$1" 205 | shift 206 | ;; 207 | *.cc | *.CC | *.cxx | *.CXX | *.[cC]++) 208 | func_file_conv "$1" 209 | set x "$@" -Tp"$file" 210 | shift 211 | ;; 212 | *.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO]) 213 | func_file_conv "$1" mingw 214 | set x "$@" "$file" 215 | shift 216 | ;; 217 | *) 218 | set x "$@" "$1" 219 | shift 220 | ;; 221 | esac 222 | fi 223 | shift 224 | done 225 | if test -n "$linker_opts"; then 226 | linker_opts="-link$linker_opts" 227 | fi 228 | exec "$@" $linker_opts 229 | exit 1 230 | } 231 | 232 | eat= 233 | 234 | case $1 in 235 | '') 236 | echo "$0: No command. Try '$0 --help' for more information." 1>&2 237 | exit 1; 238 | ;; 239 | -h | --h*) 240 | cat <<\EOF 241 | Usage: compile [--help] [--version] PROGRAM [ARGS] 242 | 243 | Wrapper for compilers which do not understand '-c -o'. 244 | Remove '-o dest.o' from ARGS, run PROGRAM with the remaining 245 | arguments, and rename the output as expected. 246 | 247 | If you are trying to build a whole package this is not the 248 | right script to run: please start by reading the file 'INSTALL'. 249 | 250 | Report bugs to . 251 | EOF 252 | exit $? 253 | ;; 254 | -v | --v*) 255 | echo "compile $scriptversion" 256 | exit $? 257 | ;; 258 | cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \ 259 | icl | *[/\\]icl | icl.exe | *[/\\]icl.exe ) 260 | func_cl_wrapper "$@" # Doesn't return... 261 | ;; 262 | esac 263 | 264 | ofile= 265 | cfile= 266 | 267 | for arg 268 | do 269 | if test -n "$eat"; then 270 | eat= 271 | else 272 | case $1 in 273 | -o) 274 | # configure might choose to run compile as 'compile cc -o foo foo.c'. 275 | # So we strip '-o arg' only if arg is an object. 276 | eat=1 277 | case $2 in 278 | *.o | *.obj) 279 | ofile=$2 280 | ;; 281 | *) 282 | set x "$@" -o "$2" 283 | shift 284 | ;; 285 | esac 286 | ;; 287 | *.c) 288 | cfile=$1 289 | set x "$@" "$1" 290 | shift 291 | ;; 292 | *) 293 | set x "$@" "$1" 294 | shift 295 | ;; 296 | esac 297 | fi 298 | shift 299 | done 300 | 301 | if test -z "$ofile" || test -z "$cfile"; then 302 | # If no '-o' option was seen then we might have been invoked from a 303 | # pattern rule where we don't need one. That is ok -- this is a 304 | # normal compilation that the losing compiler can handle. If no 305 | # '.c' file was seen then we are probably linking. That is also 306 | # ok. 307 | exec "$@" 308 | fi 309 | 310 | # Name of file we expect compiler to create. 311 | cofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'` 312 | 313 | # Create the lock directory. 314 | # Note: use '[/\\:.-]' here to ensure that we don't use the same name 315 | # that we are using for the .o file. Also, base the name on the expected 316 | # object file name, since that is what matters with a parallel build. 317 | lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d 318 | while true; do 319 | if mkdir "$lockdir" >/dev/null 2>&1; then 320 | break 321 | fi 322 | sleep 1 323 | done 324 | # FIXME: race condition here if user kills between mkdir and trap. 325 | trap "rmdir '$lockdir'; exit 1" 1 2 15 326 | 327 | # Run the compile. 328 | "$@" 329 | ret=$? 330 | 331 | if test -f "$cofile"; then 332 | test "$cofile" = "$ofile" || mv "$cofile" "$ofile" 333 | elif test -f "${cofile}bj"; then 334 | test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile" 335 | fi 336 | 337 | rmdir "$lockdir" 338 | exit $ret 339 | 340 | # Local Variables: 341 | # mode: shell-script 342 | # sh-indentation: 2 343 | # eval: (add-hook 'before-save-hook 'time-stamp) 344 | # time-stamp-start: "scriptversion=" 345 | # time-stamp-format: "%:y-%02m-%02d.%02H" 346 | # time-stamp-time-zone: "UTC0" 347 | # time-stamp-end: "; # UTC" 348 | # End: 349 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/build-aux/missing: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # Common wrapper for a few potentially missing GNU programs. 3 | 4 | scriptversion=2018-03-07.03; # UTC 5 | 6 | # Copyright (C) 1996-2020 Free Software Foundation, Inc. 7 | # Originally written by Fran,cois Pinard , 1996. 8 | 9 | # This program is free software; you can redistribute it and/or modify 10 | # it under the terms of the GNU General Public License as published by 11 | # the Free Software Foundation; either version 2, or (at your option) 12 | # any later version. 13 | 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License for more details. 18 | 19 | # You should have received a copy of the GNU General Public License 20 | # along with this program. If not, see . 21 | 22 | # As a special exception to the GNU General Public License, if you 23 | # distribute this file as part of a program that contains a 24 | # configuration script generated by Autoconf, you may include it under 25 | # the same distribution terms that you use for the rest of that program. 26 | 27 | if test $# -eq 0; then 28 | echo 1>&2 "Try '$0 --help' for more information" 29 | exit 1 30 | fi 31 | 32 | case $1 in 33 | 34 | --is-lightweight) 35 | # Used by our autoconf macros to check whether the available missing 36 | # script is modern enough. 37 | exit 0 38 | ;; 39 | 40 | --run) 41 | # Back-compat with the calling convention used by older automake. 42 | shift 43 | ;; 44 | 45 | -h|--h|--he|--hel|--help) 46 | echo "\ 47 | $0 [OPTION]... PROGRAM [ARGUMENT]... 48 | 49 | Run 'PROGRAM [ARGUMENT]...', returning a proper advice when this fails due 50 | to PROGRAM being missing or too old. 51 | 52 | Options: 53 | -h, --help display this help and exit 54 | -v, --version output version information and exit 55 | 56 | Supported PROGRAM values: 57 | aclocal autoconf autoheader autom4te automake makeinfo 58 | bison yacc flex lex help2man 59 | 60 | Version suffixes to PROGRAM as well as the prefixes 'gnu-', 'gnu', and 61 | 'g' are ignored when checking the name. 62 | 63 | Send bug reports to ." 64 | exit $? 65 | ;; 66 | 67 | -v|--v|--ve|--ver|--vers|--versi|--versio|--version) 68 | echo "missing $scriptversion (GNU Automake)" 69 | exit $? 70 | ;; 71 | 72 | -*) 73 | echo 1>&2 "$0: unknown '$1' option" 74 | echo 1>&2 "Try '$0 --help' for more information" 75 | exit 1 76 | ;; 77 | 78 | esac 79 | 80 | # Run the given program, remember its exit status. 81 | "$@"; st=$? 82 | 83 | # If it succeeded, we are done. 84 | test $st -eq 0 && exit 0 85 | 86 | # Also exit now if we it failed (or wasn't found), and '--version' was 87 | # passed; such an option is passed most likely to detect whether the 88 | # program is present and works. 89 | case $2 in --version|--help) exit $st;; esac 90 | 91 | # Exit code 63 means version mismatch. This often happens when the user 92 | # tries to use an ancient version of a tool on a file that requires a 93 | # minimum version. 94 | if test $st -eq 63; then 95 | msg="probably too old" 96 | elif test $st -eq 127; then 97 | # Program was missing. 98 | msg="missing on your system" 99 | else 100 | # Program was found and executed, but failed. Give up. 101 | exit $st 102 | fi 103 | 104 | perl_URL=https://www.perl.org/ 105 | flex_URL=https://github.com/westes/flex 106 | gnu_software_URL=https://www.gnu.org/software 107 | 108 | program_details () 109 | { 110 | case $1 in 111 | aclocal|automake) 112 | echo "The '$1' program is part of the GNU Automake package:" 113 | echo "<$gnu_software_URL/automake>" 114 | echo "It also requires GNU Autoconf, GNU m4 and Perl in order to run:" 115 | echo "<$gnu_software_URL/autoconf>" 116 | echo "<$gnu_software_URL/m4/>" 117 | echo "<$perl_URL>" 118 | ;; 119 | autoconf|autom4te|autoheader) 120 | echo "The '$1' program is part of the GNU Autoconf package:" 121 | echo "<$gnu_software_URL/autoconf/>" 122 | echo "It also requires GNU m4 and Perl in order to run:" 123 | echo "<$gnu_software_URL/m4/>" 124 | echo "<$perl_URL>" 125 | ;; 126 | esac 127 | } 128 | 129 | give_advice () 130 | { 131 | # Normalize program name to check for. 132 | normalized_program=`echo "$1" | sed ' 133 | s/^gnu-//; t 134 | s/^gnu//; t 135 | s/^g//; t'` 136 | 137 | printf '%s\n' "'$1' is $msg." 138 | 139 | configure_deps="'configure.ac' or m4 files included by 'configure.ac'" 140 | case $normalized_program in 141 | autoconf*) 142 | echo "You should only need it if you modified 'configure.ac'," 143 | echo "or m4 files included by it." 144 | program_details 'autoconf' 145 | ;; 146 | autoheader*) 147 | echo "You should only need it if you modified 'acconfig.h' or" 148 | echo "$configure_deps." 149 | program_details 'autoheader' 150 | ;; 151 | automake*) 152 | echo "You should only need it if you modified 'Makefile.am' or" 153 | echo "$configure_deps." 154 | program_details 'automake' 155 | ;; 156 | aclocal*) 157 | echo "You should only need it if you modified 'acinclude.m4' or" 158 | echo "$configure_deps." 159 | program_details 'aclocal' 160 | ;; 161 | autom4te*) 162 | echo "You might have modified some maintainer files that require" 163 | echo "the 'autom4te' program to be rebuilt." 164 | program_details 'autom4te' 165 | ;; 166 | bison*|yacc*) 167 | echo "You should only need it if you modified a '.y' file." 168 | echo "You may want to install the GNU Bison package:" 169 | echo "<$gnu_software_URL/bison/>" 170 | ;; 171 | lex*|flex*) 172 | echo "You should only need it if you modified a '.l' file." 173 | echo "You may want to install the Fast Lexical Analyzer package:" 174 | echo "<$flex_URL>" 175 | ;; 176 | help2man*) 177 | echo "You should only need it if you modified a dependency" \ 178 | "of a man page." 179 | echo "You may want to install the GNU Help2man package:" 180 | echo "<$gnu_software_URL/help2man/>" 181 | ;; 182 | makeinfo*) 183 | echo "You should only need it if you modified a '.texi' file, or" 184 | echo "any other file indirectly affecting the aspect of the manual." 185 | echo "You might want to install the Texinfo package:" 186 | echo "<$gnu_software_URL/texinfo/>" 187 | echo "The spurious makeinfo call might also be the consequence of" 188 | echo "using a buggy 'make' (AIX, DU, IRIX), in which case you might" 189 | echo "want to install GNU make:" 190 | echo "<$gnu_software_URL/make/>" 191 | ;; 192 | *) 193 | echo "You might have modified some files without having the proper" 194 | echo "tools for further handling them. Check the 'README' file, it" 195 | echo "often tells you about the needed prerequisites for installing" 196 | echo "this package. You may also peek at any GNU archive site, in" 197 | echo "case some other package contains this missing '$1' program." 198 | ;; 199 | esac 200 | } 201 | 202 | give_advice "$1" | sed -e '1s/^/WARNING: /' \ 203 | -e '2,$s/^/ /' >&2 204 | 205 | # Propagate the correct exit status (expected to be 127 for a program 206 | # not found, 63 for a program that failed due to version mismatch). 207 | exit $st 208 | 209 | # Local variables: 210 | # eval: (add-hook 'before-save-hook 'time-stamp) 211 | # time-stamp-start: "scriptversion=" 212 | # time-stamp-format: "%:y-%02m-%02d.%02H" 213 | # time-stamp-time-zone: "UTC0" 214 | # time-stamp-end: "; # UTC" 215 | # End: 216 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/config.h.in: -------------------------------------------------------------------------------- 1 | /* config.h.in. Generated from configure.ac by autoheader. */ 2 | 3 | /* hide internal library symbols */ 4 | #undef DSO_HIDDEN 5 | 6 | /* Debug messages. */ 7 | #undef ENABLE_DEBUG 8 | 9 | /* Define to 1 if you have the header file. */ 10 | #undef HAVE_INTTYPES_H 11 | 12 | /* Define to 1 if your system has a GNU libc compatible `malloc' function, and 13 | to 0 otherwise. */ 14 | #undef HAVE_MALLOC 15 | 16 | /* Define to 1 if you have the header file. */ 17 | #undef HAVE_MEMORY_H 18 | 19 | /* Define to 1 if you have the `memset' function. */ 20 | #undef HAVE_MEMSET 21 | 22 | /* Define if cfg_add_searchpath() is available */ 23 | #undef HAVE_SEARCHPATH 24 | 25 | /* Define to 1 if you have the `setenv' function. */ 26 | #undef HAVE_SETENV 27 | 28 | /* Define to 1 if you have the header file. */ 29 | #undef HAVE_STDINT_H 30 | 31 | /* Define to 1 if you have the header file. */ 32 | #undef HAVE_STDLIB_H 33 | 34 | /* Define to 1 if you have the `strcasecmp' function. */ 35 | #undef HAVE_STRCASECMP 36 | 37 | /* Define to 1 if you have the `strdup' function. */ 38 | #undef HAVE_STRDUP 39 | 40 | /* Define to 1 if you have the `strerror' function. */ 41 | #undef HAVE_STRERROR 42 | 43 | /* Define to 1 if you have the header file. */ 44 | #undef HAVE_STRINGS_H 45 | 46 | /* Define to 1 if you have the header file. */ 47 | #undef HAVE_STRING_H 48 | 49 | /* Define to 1 if you have the `strstr' function. */ 50 | #undef HAVE_STRSTR 51 | 52 | /* Define to 1 if you have the `strtoull' function. */ 53 | #undef HAVE_STRTOULL 54 | 55 | /* Define to 1 if you have the header file. */ 56 | #undef HAVE_SYS_STAT_H 57 | 58 | /* Define to 1 if you have the header file. */ 59 | #undef HAVE_SYS_TYPES_H 60 | 61 | /* Define to 1 if you have the header file. */ 62 | #undef HAVE_UNISTD_H 63 | 64 | /* Name of package */ 65 | #undef PACKAGE 66 | 67 | /* Define to the address where bug reports for this package should be sent. */ 68 | #undef PACKAGE_BUGREPORT 69 | 70 | /* Define to the full name of this package. */ 71 | #undef PACKAGE_NAME 72 | 73 | /* Define to the full name and version of this package. */ 74 | #undef PACKAGE_STRING 75 | 76 | /* Define to the one symbol short name of this package. */ 77 | #undef PACKAGE_TARNAME 78 | 79 | /* Define to the home page for this package. */ 80 | #undef PACKAGE_URL 81 | 82 | /* Define to the version of this package. */ 83 | #undef PACKAGE_VERSION 84 | 85 | /* Define to 1 if you have the ANSI C header files. */ 86 | #undef STDC_HEADERS 87 | 88 | /* Enable extensions on AIX 3, Interix. */ 89 | #ifndef _ALL_SOURCE 90 | # undef _ALL_SOURCE 91 | #endif 92 | /* Enable GNU extensions on systems that have them. */ 93 | #ifndef _GNU_SOURCE 94 | # undef _GNU_SOURCE 95 | #endif 96 | /* Enable threading extensions on Solaris. */ 97 | #ifndef _POSIX_PTHREAD_SEMANTICS 98 | # undef _POSIX_PTHREAD_SEMANTICS 99 | #endif 100 | /* Enable extensions on HP NonStop. */ 101 | #ifndef _TANDEM_SOURCE 102 | # undef _TANDEM_SOURCE 103 | #endif 104 | /* Enable general extensions on Solaris. */ 105 | #ifndef __EXTENSIONS__ 106 | # undef __EXTENSIONS__ 107 | #endif 108 | 109 | 110 | /* Version number of package */ 111 | #undef VERSION 112 | 113 | /* Enable large inode numbers on Mac OS X 10.5. */ 114 | #ifndef _DARWIN_USE_64_BIT_INODE 115 | # define _DARWIN_USE_64_BIT_INODE 1 116 | #endif 117 | 118 | /* Number of bits in a file offset, on hosts where this is settable. */ 119 | #undef _FILE_OFFSET_BITS 120 | 121 | /* Define for large files, on AIX-style hosts. */ 122 | #undef _LARGE_FILES 123 | 124 | /* Define to 1 if on MINIX. */ 125 | #undef _MINIX 126 | 127 | /* Define to 2 if the system does not provide POSIX.1 features except with 128 | this defined. */ 129 | #undef _POSIX_1_SOURCE 130 | 131 | /* Define to 1 if you need to in order for `stat' and other things to work. */ 132 | #undef _POSIX_SOURCE 133 | 134 | /* Define for Solaris 2.5.1 so the uint32_t typedef from , 135 | , or is not used. If the typedef were allowed, the 136 | #define below would cause a syntax error. */ 137 | #undef _UINT32_T 138 | 139 | /* Define to `__inline__' or `__inline' if that's what the C compiler 140 | calls it, or to nothing if 'inline' is not supported under any name. */ 141 | #ifndef __cplusplus 142 | #undef inline 143 | #endif 144 | 145 | /* Define to rpl_malloc if the replacement function should be used. */ 146 | #undef malloc 147 | 148 | /* Define to `unsigned int' if does not define. */ 149 | #undef size_t 150 | 151 | /* Define to the type of an unsigned integer type of width exactly 32 bits if 152 | such a type exists and the standard includes do not define it. */ 153 | #undef uint32_t 154 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/configure.ac: -------------------------------------------------------------------------------- 1 | AC_PREREQ(2.60) 2 | AC_INIT([genimage], 3 | [16], 4 | [oss-tools@pengutronix.de], 5 | [genimage], 6 | [http://www.pengutronix.de/genimage/]) 7 | AC_CONFIG_SRCDIR([genimage.c]) 8 | AC_CONFIG_AUX_DIR([build-aux]) 9 | AM_INIT_AUTOMAKE([foreign 1.11 -Wall -Wno-portability silent-rules tar-pax no-dist-gzip dist-xz subdir-objects]) 10 | AC_PROG_CC_STDC 11 | AC_PROG_RANLIB 12 | 13 | # If possible, enable extensions to Posix 14 | AC_USE_SYSTEM_EXTENSIONS 15 | AC_SYS_LARGEFILE 16 | 17 | # where to put extension macros 18 | AC_CONFIG_MACRO_DIR([m4]) 19 | 20 | # default is less output while building the package 21 | AM_SILENT_RULES([yes]) 22 | 23 | # change if 'usr/local' as the default install path isn't a good choice 24 | #AC_PREFIX_DEFAULT([/usr/local]) 25 | 26 | AC_CHECK_FUNCS([memset setenv strdup strcasecmp strerror strstr strtoull]) 27 | 28 | AC_C_INLINE 29 | AC_FUNC_ERROR_AT_LINE 30 | AC_FUNC_MALLOC 31 | AC_TYPE_SIZE_T 32 | AC_TYPE_UINT32_T 33 | 34 | # ----------- query user's settings ---------------------- 35 | AC_MSG_CHECKING([whether to enable debugging]) 36 | AC_ARG_ENABLE([debug], 37 | AS_HELP_STRING([--enable-debug], 38 | [enable debug messages @<:@default=disabled@:>@]), 39 | [], 40 | [enable_debug=no]) 41 | AC_MSG_RESULT([${enable_debug}]) 42 | 43 | # should the executable export all symbols? 44 | AC_MSG_CHECKING([whether to hide internal symbols]) 45 | AC_ARG_ENABLE([hide], 46 | [AS_HELP_STRING([--disable-hide], 47 | [do not hide all internal symbols @<:@default=enabled@:>@])], 48 | [], 49 | [enable_hide=yes]) 50 | # for debugging purposes we must disable the hiding feature 51 | AS_IF([test "x${enable_debug}" = "xyes"], 52 | [AC_MSG_RESULT([no (due to debug enabled)]) 53 | enable_hide=no], 54 | [AC_MSG_RESULT([${enable_hide}])]) 55 | 56 | # ----------- autodetect some settings ------------------- 57 | 58 | # add as much warnings and features as possible, but check what the compiler 59 | # is able to understand and use it only if possible 60 | 61 | CC_CHECK_CFLAGS_SILENT([-pipe],[AM_CFLAGS="${AM_CFLAGS} -pipe"]) 62 | CC_CHECK_CFLAGS_SILENT([-Wall],[AM_CFLAGS="${AM_CFLAGS} -Wall"]) 63 | CC_CHECK_CFLAGS_SILENT([-Wextra],[AM_CFLAGS="${AM_CFLAGS} -Wextra"]) 64 | CC_CHECK_CFLAGS_SILENT([-Wmissing-declarations],[AM_CFLAGS="${AM_CFLAGS} -Wmissing-declarations"]) 65 | CC_CHECK_CFLAGS_SILENT([-Wmissing-prototypes],[AM_CFLAGS="${AM_CFLAGS} -Wmissing-prototypes"]) 66 | CC_CHECK_CFLAGS_SILENT([-Wnested-externs],[AM_CFLAGS="${AM_CFLAGS} -Wnested-externs"]) 67 | CC_CHECK_CFLAGS_SILENT([-Wpointer-arith],[AM_CFLAGS="${AM_CFLAGS} -Wpointer-arith"]) 68 | CC_CHECK_CFLAGS_SILENT([-Wsign-compare],[AM_CFLAGS="${AM_CFLAGS} -Wsign-compare"]) 69 | CC_CHECK_CFLAGS_SILENT([-Wchar-subscripts],[AM_CFLAGS="${AM_CFLAGS} -Wchar-subscripts"]) 70 | CC_CHECK_CFLAGS_SILENT([-Wstrict-prototypes],[AM_CFLAGS="${AM_CFLAGS} -Wstrict-prototypes"]) 71 | CC_CHECK_CFLAGS_SILENT([-Wshadow],[AM_CFLAGS="${AM_CFLAGS} -Wshadow"]) 72 | CC_CHECK_CFLAGS_SILENT([-Wformat-security],[AM_CFLAGS="${AM_CFLAGS} -Wformat-security"]) 73 | CC_CHECK_CFLAGS_SILENT([-Wtype-limits],[AM_CFLAGS="${AM_CFLAGS} -Wtype-limits"]) 74 | CC_CHECK_CFLAGS_SILENT([-Wunused-parameter],[AM_CFLAGS="${AM_CFLAGS} -Wno-unused-parameter"]) 75 | CC_CHECK_CFLAGS_SILENT([-ffunction-sections],[AM_CFLAGS="${AM_CFLAGS} -ffunction-sections"]) 76 | CC_CHECK_CFLAGS_SILENT([-fdata-sections],[AM_CFLAGS="${AM_CFLAGS} -fdata-sections"]) 77 | 78 | # Add only those libraries which are really used 79 | CC_CHECK_LDFLAGS([-Wl,--as-needed], [AM_LDFLAGS="${AM_LDFLAGS} -Wl,--as-needed"],[]) 80 | CC_CHECK_LDFLAGS([-Wl,--gc-sections], [AM_LDFLAGS="${AM_LDFLAGS} -Wl,--gc-sections"],[]) 81 | 82 | PKG_CHECK_MODULES(CONFUSE, libconfuse >= 2.8, 83 | [AC_DEFINE([HAVE_SEARCHPATH], [1], [Define if cfg_add_searchpath() is available])], 84 | [PKG_CHECK_MODULES(CONFUSE, libconfuse)]) 85 | 86 | # ------- use all the settings ---------------------- 87 | 88 | AS_IF([test "x$enable_debug" = "xyes"], [ 89 | AC_DEFINE(ENABLE_DEBUG, [1], [Debug messages.]) 90 | ]) 91 | 92 | # Enable "-fvisibility=hidden" only if the used gcc supports it 93 | AS_IF([test "${enable_hide}" = "yes"], 94 | [AC_MSG_CHECKING([whether the compiler supports -fvisibility=hidden]) 95 | CC_CHECK_CFLAGS_SILENT([-fvisibility=hidden], [], [enable_hide=no]) 96 | # still enabled? 97 | if test "x${enable_hide}" = "xyes"; then 98 | AC_DEFINE(DSO_HIDDEN, 1, [hide internal library symbols]) 99 | AM_CFLAGS="${AM_CFLAGS} -fvisibility=hidden" 100 | fi 101 | 102 | AC_MSG_RESULT([${enable_hide}])]) 103 | 104 | AC_SUBST(AM_CFLAGS) 105 | 106 | # be very silent on request 107 | AM_CONDITIONAL(BUILD_SILENTLY, test "x$AM_DEFAULT_VERBOSITY" = x0) 108 | 109 | AC_CONFIG_HEADERS(config.h) 110 | AC_CONFIG_FILES([ 111 | Makefile 112 | ]) 113 | 114 | AC_REQUIRE_AUX_FILE([tap-driver.sh]) 115 | 116 | AC_OUTPUT 117 | AC_MSG_RESULT([ 118 | $PACKAGE $VERSION 119 | ===== 120 | 121 | prefix: ${prefix} 122 | 123 | compiler: ${CC} 124 | cflags: ${CFLAGS} ${AM_CFLAGS} 125 | ldflags: ${LDFLAGS} ${AM_LDFLAGS} 126 | 127 | debug: ${enable_debug} 128 | hide symbols: ${enable_hide} 129 | libconfuse: ${CONFUSE_LIBS} 130 | ]) 131 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/crc32.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "genimage.h" 4 | 5 | static const uint32_t crc32_tab[] = { 6 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 7 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 8 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 9 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 10 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 11 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 12 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 13 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 14 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 15 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 16 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 17 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 18 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 19 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 20 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 21 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 22 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 23 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 24 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 25 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 26 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 27 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 28 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 29 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 30 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 31 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 32 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 33 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 34 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 35 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 36 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 37 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 38 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 39 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 40 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 41 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 42 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 43 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 44 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 45 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 46 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 47 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 48 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 49 | }; 50 | 51 | uint32_t crc32_next(const void *data, size_t len, uint32_t last_crc) 52 | { 53 | uint32_t crc = ~last_crc; 54 | const char *p = data; 55 | 56 | while(len--) 57 | crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8); 58 | 59 | return ~crc; 60 | } 61 | 62 | uint32_t crc32(const void *data, size_t len) 63 | { 64 | return crc32_next(data, len, 0); 65 | } 66 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/flash.conf: -------------------------------------------------------------------------------- 1 | flash nand-64M-512 { 2 | pebsize = 16384 3 | lebsize = 15360 4 | numpebs = 4096 5 | minimum-io-unit-size = 512 6 | vid-header-offset = 512 7 | sub-page-size = 512 8 | } 9 | 10 | flash nor-64M-128k { 11 | pebsize = 131072 12 | lebsize = 130944 13 | numpebs = 256 14 | minimum-io-unit-size = 1 15 | vid-header-offset = 64 16 | sub-page-size = 1 17 | } 18 | 19 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/genimage.h: -------------------------------------------------------------------------------- 1 | #ifndef __PTX_IMAGE_H 2 | #define __PTX_IMAGE_H 3 | 4 | #include 5 | #include 6 | #include "list.h" 7 | 8 | struct image_handler; 9 | 10 | struct image *image_get(const char *filename); 11 | 12 | int systemp(struct image *image, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); 13 | void error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2))); 14 | void info(const char *fmt, ...) __attribute__ ((format(printf, 1, 2))); 15 | void debug(const char *fmt, ...) __attribute__ ((format(printf, 1, 2))); 16 | void image_error(struct image *image, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); 17 | void image_info(struct image *image, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); 18 | void image_debug(struct image *image, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); 19 | void xasprintf(char **strp, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); 20 | void xstrcatf(char **strp, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); 21 | 22 | void disable_rootpath(void); 23 | const char *imagepath(void); 24 | const char *inputpath(void); 25 | const char *rootpath(void); 26 | const char *tmppath(void); 27 | const char *mountpath(const struct image *); 28 | struct flash_type; 29 | 30 | struct mountpoint { 31 | char *path; 32 | struct list_head list; 33 | char *mountpath; 34 | }; 35 | 36 | struct partition { 37 | unsigned long long offset; 38 | unsigned long long size; 39 | unsigned long long align; 40 | unsigned char partition_type; 41 | cfg_bool_t bootable; 42 | cfg_bool_t extended; 43 | cfg_bool_t read_only; 44 | cfg_bool_t hidden; 45 | cfg_bool_t no_automount; 46 | const char *image; 47 | struct list_head list; 48 | int autoresize; 49 | int in_partition_table; 50 | const char *name; 51 | const char *partition_type_uuid; 52 | const char *partition_uuid; 53 | cfg_t *cfg; 54 | }; 55 | 56 | struct image { 57 | const char *name; 58 | const char *file; 59 | unsigned long long size; 60 | struct extent *holes; 61 | int n_holes; 62 | cfg_bool_t size_is_percent; 63 | const char *mountpoint; 64 | const char *srcpath; 65 | cfg_bool_t empty; 66 | cfg_bool_t temporary; 67 | const char *exec_pre; 68 | const char *exec_post; 69 | unsigned char partition_type; 70 | void *handler_priv; 71 | struct image_handler *handler; 72 | struct list_head list; 73 | int done; 74 | struct flash_type *flash_type; 75 | cfg_t *imagesec; 76 | struct list_head partitions; 77 | struct mountpoint *mp; 78 | char *outfile; 79 | int seen; 80 | off_t last_offset; 81 | }; 82 | 83 | struct image_handler { 84 | char *type; 85 | cfg_bool_t no_rootpath; 86 | int (*parse)(struct image *i, cfg_t *cfg); 87 | int (*setup)(struct image *i, cfg_t *cfg); 88 | int (*generate)(struct image *i); 89 | cfg_opt_t *opts; 90 | }; 91 | 92 | struct flash_type { 93 | const char *name; 94 | int pebsize; 95 | int lebsize; 96 | int numpebs; 97 | int minimum_io_unit_size; 98 | int vid_header_offset; 99 | int sub_page_size; 100 | struct list_head list; 101 | }; 102 | 103 | struct flash_type *flash_type_get(const char *name); 104 | 105 | extern struct image_handler android_sparse_handler; 106 | extern struct image_handler cpio_handler; 107 | extern struct image_handler cramfs_handler; 108 | extern struct image_handler ext2_handler; 109 | extern struct image_handler ext3_handler; 110 | extern struct image_handler ext4_handler; 111 | extern struct image_handler f2fs_handler; 112 | extern struct image_handler file_handler; 113 | extern struct image_handler flash_handler; 114 | extern struct image_handler hdimage_handler; 115 | extern struct image_handler iso_handler; 116 | extern struct image_handler jffs2_handler; 117 | extern struct image_handler qemu_handler; 118 | extern struct image_handler rauc_handler; 119 | extern struct image_handler squashfs_handler; 120 | extern struct image_handler tar_handler; 121 | extern struct image_handler ubi_handler; 122 | extern struct image_handler ubifs_handler; 123 | extern struct image_handler vfat_handler; 124 | extern struct image_handler fit_handler; 125 | extern struct image_handler fip_handler; 126 | 127 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 128 | 129 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 130 | 131 | /** 132 | * container_of - cast a member of a structure out to the containing structure 133 | * @ptr: the pointer to the member. 134 | * @type: the type of the container struct this is embedded in. 135 | * @member: the name of the member within the struct. 136 | * 137 | */ 138 | #define container_of(ptr, type, member) ({ \ 139 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 140 | (type *)( (char *)__mptr - offsetof(type,member) );}) 141 | 142 | void *xzalloc(size_t n); 143 | void *xrealloc(void *ptr, size_t size); 144 | unsigned long long strtoul_suffix(const char *str, char **endp, 145 | cfg_bool_t *percent); 146 | 147 | int init_config(void); 148 | cfg_opt_t *get_confuse_opts(void); 149 | const char *get_opt(const char *name); 150 | int set_config_opts(int argc, char *argv[], cfg_t *cfg); 151 | 152 | static inline size_t min(size_t a, size_t b) 153 | { 154 | return a < b ? a : b; 155 | } 156 | 157 | enum pad_mode { 158 | MODE_APPEND, 159 | MODE_OVERWRITE, 160 | }; 161 | 162 | struct extent { 163 | unsigned long long start, end; 164 | }; 165 | 166 | int open_file(struct image *image, const char *filename, int extra_flags); 167 | int map_file_extents(struct image *image, const char *filename, int fd, 168 | size_t size, struct extent **extents, size_t *extent_count); 169 | int is_block_device(const char *filename); 170 | int block_device_size(struct image *image, const char *blkdev, 171 | unsigned long long *size); 172 | int prepare_image(struct image *image, unsigned long long size); 173 | int insert_image(struct image *image, struct image *sub, 174 | unsigned long long size, unsigned long long offset, 175 | unsigned char byte); 176 | int insert_data(struct image *image, const void *data, const char *outfile, 177 | size_t size, long offset); 178 | int extend_file(struct image *image, size_t size); 179 | int reload_partitions(struct image *image); 180 | int parse_holes(struct image *image, cfg_t *cfg); 181 | 182 | unsigned long long cfg_getint_suffix(cfg_t *sec, const char *name); 183 | unsigned long long cfg_getint_suffix_percent(cfg_t *sec, const char *name, 184 | cfg_bool_t *percent); 185 | 186 | static inline const char *imageoutfile(const struct image *image) 187 | { 188 | return image->outfile; 189 | } 190 | 191 | char *sanitize_path(const char *path); 192 | 193 | int uuid_validate(const char *str); 194 | void uuid_parse(const char *str, unsigned char *uuid); 195 | char *uuid_random(void); 196 | 197 | unsigned long long image_dir_size(struct image *image); 198 | 199 | uint32_t crc32(const void *data, size_t len); 200 | uint32_t crc32_next(const void *data, size_t len, uint32_t last_crc); 201 | 202 | #define ct_assert(e) _Static_assert(e, #e) 203 | 204 | #endif /* __PTX_IMAGE_H */ 205 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-cpio.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Sascha Hauer , Pengutronix 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int cpio_generate(struct image *image) 26 | { 27 | int ret; 28 | char *format = cfg_getstr(image->imagesec, "format"); 29 | char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 30 | char *comp = cfg_getstr(image->imagesec, "compress"); 31 | 32 | ret = systemp(image, "(cd '%s' && find . | %s -H '%s' %s -o %s %s) > '%s'", 33 | mountpath(image), 34 | get_opt("cpio"), 35 | format, extraargs, comp[0] != '\0' ? "|" : "", comp, 36 | imageoutfile(image)); 37 | 38 | return ret; 39 | } 40 | 41 | static cfg_opt_t cpio_opts[] = { 42 | CFG_STR("format", "newc", CFGF_NONE), 43 | CFG_STR("extraargs", "", CFGF_NONE), 44 | CFG_STR("compress", "", CFGF_NONE), 45 | CFG_END() 46 | }; 47 | 48 | struct image_handler cpio_handler = { 49 | .type = "cpio", 50 | .generate = cpio_generate, 51 | .opts = cpio_opts, 52 | }; 53 | 54 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-cramfs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Julien Viard de Galbert 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int cram_generate(struct image *image) 26 | { 27 | char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 28 | 29 | return systemp(image, "%s%s%s %s '%s' '%s'", 30 | get_opt("mkcramfs"), 31 | image->name ? " -n " : "", 32 | image->name ? image->name : "", /* name */ 33 | extraargs, 34 | mountpath(image), /* source dir */ 35 | imageoutfile(image)); /* destination file */ 36 | } 37 | 38 | static cfg_opt_t cram_opts[] = { 39 | CFG_STR("extraargs", "", CFGF_NONE), 40 | CFG_END() 41 | }; 42 | 43 | struct image_handler cramfs_handler = { 44 | .type = "cramfs", 45 | .generate = cram_generate, 46 | .opts = cram_opts, 47 | }; 48 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-ext2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Sascha Hauer , Pengutronix 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "genimage.h" 26 | 27 | struct ext { 28 | int use_mke2fs; 29 | const char *features; 30 | char *usage_type_args; 31 | char *conf_env; 32 | char *size_features; 33 | }; 34 | 35 | static int ext2_generate_genext2fs(struct image *image) 36 | { 37 | int ret; 38 | struct ext *ext = image->handler_priv; 39 | const char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 40 | const char *label = cfg_getstr(image->imagesec, "label"); 41 | 42 | ret = systemp(image, "%s %s%s%s --size-in-blocks=%lld -i 16384 '%s' %s", 43 | get_opt("genext2fs"), 44 | image->empty ? "" : "-d '", 45 | image->empty ? "" : mountpath(image), 46 | image->empty ? "" : "'", 47 | image->size / 1024, imageoutfile(image), extraargs); 48 | 49 | if (ret) 50 | return ret; 51 | 52 | if (ext->features && ext->features[0] != '\0') { 53 | ret = systemp(image, "%s -O '%s' '%s'", get_opt("tune2fs"), 54 | ext->features, imageoutfile(image)); 55 | if (ret) 56 | return ret; 57 | } 58 | if (label && label[0] != '\0') { 59 | ret = systemp(image, "%s -L '%s' '%s'", get_opt("tune2fs"), 60 | label, imageoutfile(image)); 61 | if (ret) 62 | return ret; 63 | } 64 | return 0; 65 | } 66 | 67 | static int ext2_generate_mke2fs(struct image *image) 68 | { 69 | struct ext *ext = image->handler_priv; 70 | const char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 71 | const char *label = cfg_getstr(image->imagesec, "label"); 72 | const char *root_owner = cfg_getstr(image->imagesec, "root-owner"); 73 | const char *options = "lazy_itable_init=0,lazy_journal_init=0"; 74 | const char *features = ext->features; 75 | int ret; 76 | 77 | if (features && features[0] == '\0') 78 | features = NULL; 79 | if (label && label[0] == '\0') 80 | label = NULL; 81 | 82 | ret = prepare_image(image, image->size); 83 | if (ret < 0) 84 | return ret; 85 | 86 | return systemp(image, "%s%s -t %s%s -I 256 -E 'root_owner=%s,%s'%s %s%s%s %s %s%s %s%s%s '%s' %lldk", 87 | ext->conf_env, get_opt("mke2fs"), image->handler->type, 88 | ext->usage_type_args, root_owner, options, ext->size_features, 89 | image->empty ? "" : "-d '", 90 | image->empty ? "" : mountpath(image), 91 | image->empty ? "" : "'", 92 | extraargs, label ? "-L " : "", label ? label : "", 93 | features ? "-O '" : "", 94 | features ? features : "", 95 | features ? "'" : "", 96 | imageoutfile(image), image->size / 1024); 97 | } 98 | 99 | static int ext2_generate(struct image *image) 100 | { 101 | struct ext *ext = image->handler_priv; 102 | const char *fs_timestamp = cfg_getstr(image->imagesec, "fs-timestamp"); 103 | int ret; 104 | 105 | if (ext->use_mke2fs) 106 | ret = ext2_generate_mke2fs(image); 107 | else 108 | ret = ext2_generate_genext2fs(image); 109 | 110 | if (ret) 111 | return ret; 112 | 113 | ret = systemp(image, "%s -pvfD '%s'", get_opt("e2fsck"), 114 | imageoutfile(image)); 115 | 116 | /* e2fsck return 1 when the filesystem was successfully modified */ 117 | if (ret > 2) 118 | return ret; 119 | 120 | if (fs_timestamp) { 121 | ret = systemp(image, "echo '" 122 | "set_current_time %s\n" 123 | "set_super_value mkfs_time %s\n" 124 | "set_super_value lastcheck %s\n" 125 | "set_super_value mtime 00000000' | %s -w '%s'", 126 | fs_timestamp, fs_timestamp, fs_timestamp, 127 | get_opt("debugfs"), imageoutfile(image)); 128 | if (ret) 129 | return ret; 130 | } 131 | return 0; 132 | } 133 | 134 | static int ext2_setup(struct image *image, cfg_t *cfg) 135 | { 136 | struct ext *ext = xzalloc(sizeof(*ext)); 137 | const char *conf = cfg_getstr(image->imagesec, "mke2fs-conf"); 138 | const char *usage_type = cfg_getstr(image->imagesec, "usage-type"); 139 | 140 | if (!conf) { 141 | conf = cfg_getstr(image->imagesec, "mke2fs_conf"); 142 | if (conf) 143 | image_info(image, "option 'mke2fs_conf' is deprecated, use mke2fs-conf instead.\n"); 144 | } 145 | 146 | if (!image->size) { 147 | image_error(image, "no size given or must not be zero\n"); 148 | return -EINVAL; 149 | } 150 | 151 | ext->use_mke2fs = cfg_getbool(cfg, "use-mke2fs"); 152 | 153 | ext->features = cfg_getstr(image->imagesec, "features"); 154 | if (!ext->features) { 155 | if (!ext->use_mke2fs) { 156 | if (!strcmp(image->handler->type, "ext3")) 157 | ext->features = "has_journal"; 158 | else if (!strcmp(image->handler->type, "ext4")) 159 | ext->features = "extents,uninit_bg,dir_index,has_journal"; 160 | } 161 | } 162 | 163 | if (ext->use_mke2fs) { 164 | int is_large = image->size >= 4ll * 1024 * 1024 * 1024; 165 | int is_huge = image->size >= 2048ll * 1024 * 1024 * 1024; 166 | struct stat s; 167 | int ret; 168 | 169 | if (conf) { 170 | /* mke2fs ignores a missing config file, so make sure it exists. */ 171 | ret = stat(conf, &s); 172 | if (ret) { 173 | image_error(image, "mke2fs.conf(%s) does not exist: %s\n", 174 | conf, strerror(errno)); 175 | return -errno; 176 | } 177 | xasprintf(&ext->conf_env,"MKE2FS_CONFIG=\"%s\" ", conf); 178 | } else 179 | ext->conf_env = ""; 180 | 181 | if (usage_type) 182 | xasprintf(&ext->usage_type_args, " -T '%s'", usage_type); 183 | else 184 | ext->usage_type_args = ""; 185 | 186 | xasprintf(&ext->size_features, "%s%s", 187 | is_large ? "" : " -O '^large_file'", 188 | is_huge ? "" : " -O '^huge_file'"); 189 | } 190 | else { 191 | if (conf) { 192 | image_error(image, "'mke2fs.conf' is only used for 'mke2fs'\n"); 193 | return -EINVAL; 194 | } 195 | if (usage_type) { 196 | image_error(image, "'usage_type' is only used for 'mke2fs'\n"); 197 | return -EINVAL; 198 | } 199 | } 200 | 201 | image->handler_priv = ext; 202 | 203 | return 0; 204 | } 205 | 206 | static cfg_opt_t ext_opts[] = { 207 | CFG_STR("root-owner", "0:0", CFGF_NONE), 208 | CFG_STR("extraargs", "", CFGF_NONE), 209 | CFG_STR("features", NULL, CFGF_NONE), 210 | CFG_STR("label", NULL, CFGF_NONE), 211 | CFG_STR("fs-timestamp", NULL, CFGF_NONE), 212 | CFG_BOOL("use-mke2fs", cfg_false, CFGF_NONE), 213 | CFG_STR("usage-type", NULL, CFGF_NONE), 214 | CFG_STR("mke2fs-conf", NULL, CFGF_NONE), 215 | CFG_STR("mke2fs_conf", NULL, CFGF_NONE), 216 | CFG_END() 217 | }; 218 | 219 | struct image_handler ext2_handler = { 220 | .type = "ext2", 221 | .generate = ext2_generate, 222 | .setup = ext2_setup, 223 | .opts = ext_opts, 224 | }; 225 | 226 | struct image_handler ext3_handler = { 227 | .type = "ext3", 228 | .generate = ext2_generate, 229 | .setup = ext2_setup, 230 | .opts = ext_opts, 231 | }; 232 | 233 | struct image_handler ext4_handler = { 234 | .type = "ext4", 235 | .generate = ext2_generate, 236 | .setup = ext2_setup, 237 | .opts = ext_opts, 238 | }; 239 | 240 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-f2fs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Tomas Mudrunka 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int f2fs_generate(struct image *image) 26 | { 27 | int ret; 28 | 29 | char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 30 | char *label = cfg_getstr(image->imagesec, "label"); 31 | 32 | extraargs = cfg_getstr(image->imagesec, "extraargs"); 33 | 34 | ret = prepare_image(image, image->size); 35 | if(ret) 36 | return ret; 37 | 38 | ret = systemp(image, "%s %s %s%s%s %s '%s'", 39 | get_opt("mkfsf2fs"), 40 | label ? "-l" : "", 41 | label ? "'" : "", 42 | label ? label : "", 43 | label ? "'" : "", 44 | extraargs, 45 | imageoutfile(image)); 46 | 47 | if(ret || image->empty) 48 | return ret; 49 | 50 | ret = systemp(image, "%s -f '%s' '%s'", 51 | get_opt("sloadf2fs"), 52 | mountpath(image), 53 | imageoutfile(image)); 54 | 55 | return ret; 56 | } 57 | 58 | static cfg_opt_t f2fs_opts[] = { 59 | CFG_STR("extraargs", "", CFGF_NONE), 60 | CFG_STR("label", NULL, CFGF_NONE), 61 | CFG_END() 62 | }; 63 | 64 | struct image_handler f2fs_handler = { 65 | .type = "f2fs", 66 | .generate = f2fs_generate, 67 | .opts = f2fs_opts, 68 | }; 69 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Sascha Hauer , Pengutronix 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "genimage.h" 25 | 26 | struct file { 27 | char *name; 28 | char *infile; 29 | cfg_bool_t copy; 30 | }; 31 | 32 | static int file_generate(struct image *image) 33 | { 34 | struct file *f = image->handler_priv; 35 | int ret; 36 | 37 | if (!f->copy) 38 | return 0; 39 | 40 | if (!strcmp(f->infile, imageoutfile(image))) 41 | return 0; 42 | 43 | ret = systemp(image, "cp '%s' '%s'", f->infile, imageoutfile(image)); 44 | 45 | return ret; 46 | } 47 | 48 | static int file_setup(struct image *image, cfg_t *cfg) 49 | { 50 | struct file *f = xzalloc(sizeof(*f)); 51 | struct stat s; 52 | int ret; 53 | 54 | if (cfg) 55 | f->name = cfg_getstr(cfg, "name"); 56 | if (!f->name) 57 | f->name = strdup(image->file); 58 | 59 | if (f->name[0] == '/') 60 | f->infile = strdup(f->name); 61 | else 62 | xasprintf(&f->infile, "%s/%s", inputpath(), f->name); 63 | 64 | ret = stat(f->infile, &s); 65 | if (ret) { 66 | ret = -errno; 67 | image_error(image, "stat(%s) failed: %s\n", f->infile, 68 | strerror(errno)); 69 | return ret; 70 | } 71 | if (!image->size) 72 | image->size = s.st_size; 73 | 74 | if (cfg) 75 | f->copy = cfg_getbool(cfg, "copy"); 76 | else 77 | f->copy = cfg_false; 78 | 79 | if (!f->copy) { 80 | free(image->outfile); 81 | image->outfile = strdup(f->infile); 82 | } 83 | ret = parse_holes(image, cfg); 84 | if (ret) 85 | return ret; 86 | 87 | image->handler_priv = f; 88 | 89 | return 0; 90 | } 91 | 92 | static int file_parse(struct image *image, cfg_t *cfg) 93 | { 94 | /* File type images are used for custom types so assume that the 95 | * rootpath is need when a pre/post command is defined */ 96 | if (!image->exec_pre && !image->exec_post) 97 | image->empty = cfg_true; 98 | 99 | return 0; 100 | } 101 | 102 | static cfg_opt_t file_opts[] = { 103 | CFG_STR("name", NULL, CFGF_NONE), 104 | CFG_BOOL("copy", cfg_true, CFGF_NONE), 105 | CFG_STR_LIST("holes", NULL, CFGF_NONE), 106 | CFG_END() 107 | }; 108 | 109 | struct image_handler file_handler = { 110 | .type = "file", 111 | .generate = file_generate, 112 | .setup = file_setup, 113 | .parse = file_parse, 114 | .opts = file_opts, 115 | }; 116 | 117 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-fip.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Ahmad Fatoum 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int fip_generate(struct image *image) 26 | { 27 | struct partition *part; 28 | char *args = strdup(""); 29 | const char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 30 | int ret; 31 | 32 | list_for_each_entry(part, &image->partitions, list) { 33 | struct image *child = image_get(part->image); 34 | char *oldargs; 35 | 36 | oldargs = args; 37 | xasprintf(&args, "%s --%s '%s'", args, part->name, imageoutfile(child)); 38 | free(oldargs); 39 | } 40 | 41 | ret = systemp(image, "%s create %s %s '%s'", get_opt("fiptool"), 42 | args, extraargs, imageoutfile(image)); 43 | 44 | free(args); 45 | 46 | return ret; 47 | } 48 | 49 | static void fip_add_part(struct image *image, 50 | const char *name, const char *path) 51 | { 52 | struct partition *part; 53 | 54 | part = xzalloc(sizeof *part); 55 | part->image = path; 56 | part->name = name; 57 | list_add_tail(&part->list, &image->partitions); 58 | } 59 | 60 | static cfg_opt_t fip_opts[] = { 61 | CFG_STR("extraargs", "", CFGF_NONE), 62 | CFG_STR_LIST("tos-fw", NULL, CFGF_NONE), /* Secure Payload BL32 (Trusted OS, Extra1, Extra 2) */ 63 | /* CFGF_NODEFAULT marks options passed as-is */ 64 | CFG_STR("scp-fwu-cfg", NULL, CFGF_NODEFAULT), /* SCP Firmware Updater Configuration FWU SCP_BL2U */ 65 | CFG_STR("ap-fwu-cfg", NULL, CFGF_NODEFAULT), /* AP Firmware Updater Configuration BL2U */ 66 | CFG_STR("fwu", NULL, CFGF_NODEFAULT), /* Firmware Updater NS_BL2U */ 67 | CFG_STR("fwu-cert", NULL, CFGF_NODEFAULT), /* Non-Trusted Firmware Updater certificate */ 68 | CFG_STR("tb-fw", NULL, CFGF_NODEFAULT), /* Trusted Boot Firmware BL2 */ 69 | CFG_STR("scp-fw", NULL, CFGF_NODEFAULT), /* SCP Firmware SCP_BL2 */ 70 | CFG_STR("soc-fw", NULL, CFGF_NODEFAULT), /* EL3 Runtime Firmware BL31 */ 71 | CFG_STR("nt-fw", NULL, CFGF_NODEFAULT), /* Non-Trusted Firmware BL33 */ 72 | CFG_STR("fw-config", NULL, CFGF_NODEFAULT), /* FW_CONFIG */ 73 | CFG_STR("hw-config", NULL, CFGF_NODEFAULT), /* HW_CONFIG */ 74 | CFG_STR("tb-fw-config", NULL, CFGF_NODEFAULT), /* TB_FW_CONFIG */ 75 | CFG_STR("soc-fw-config", NULL, CFGF_NODEFAULT), /* SOC_FW_CONFIG */ 76 | CFG_STR("tos-fw-config", NULL, CFGF_NODEFAULT), /* TOS_FW_CONFIG */ 77 | CFG_STR("nt-fw-config", NULL, CFGF_NODEFAULT), /* NT_FW_CONFIG */ 78 | 79 | CFG_STR("rot-cert", NULL, CFGF_NODEFAULT), /* Root Of Trust key certificate */ 80 | 81 | CFG_STR("trusted-key-cert", NULL, CFGF_NODEFAULT), /* Trusted key certificate */ 82 | CFG_STR("scp-fw-key-cert", NULL, CFGF_NODEFAULT), /* SCP Firmware key certificate */ 83 | CFG_STR("soc-fw-key-cert", NULL, CFGF_NODEFAULT), /* SoC Firmware key certificate */ 84 | CFG_STR("tos-fw-key-cert", NULL, CFGF_NODEFAULT), /* Trusted OS Firmware key certificate */ 85 | CFG_STR("nt-fw-key-cert", NULL, CFGF_NODEFAULT), /* Non-Trusted Firmware key certificate */ 86 | 87 | CFG_STR("tb-fw-cert", NULL, CFGF_NODEFAULT), /* Trusted Boot Firmware BL2 certificate */ 88 | CFG_STR("scp-fw-cert", NULL, CFGF_NODEFAULT), /* SCP Firmware content certificate */ 89 | CFG_STR("soc-fw-cert", NULL, CFGF_NODEFAULT), /* SoC Firmware content certificate */ 90 | CFG_STR("tos-fw-cert", NULL, CFGF_NODEFAULT), /* Trusted OS Firmware content certificate */ 91 | CFG_STR("nt-fw-cert", NULL, CFGF_NODEFAULT), /* Non-Trusted Firmware content certificate */ 92 | 93 | CFG_STR("sip-sp-cert", NULL, CFGF_NODEFAULT), /* SiP owned Secure Partition content certificate */ 94 | CFG_STR("plat-sp-cert", NULL, CFGF_NODEFAULT), /* Platform owned Secure Partition content certificate */ 95 | 96 | CFG_END() 97 | }; 98 | 99 | static const char *tos_fw[] = { "tos-fw", "tos-fw-extra1", "tos-fw-extra2" }; 100 | 101 | static int fip_parse(struct image *image, cfg_t *cfg) 102 | { 103 | unsigned int i, num_tos_fw; 104 | cfg_opt_t *opt; 105 | 106 | num_tos_fw = cfg_size(cfg, "tos-fw"); 107 | if (num_tos_fw > ARRAY_SIZE(tos_fw)) { 108 | image_error(image, "%u tos-fw binaries given, but maximum is %zu\n", 109 | num_tos_fw, ARRAY_SIZE(tos_fw)); 110 | return -EINVAL; 111 | } 112 | 113 | for (i = 0; i < num_tos_fw; i++) 114 | fip_add_part(image, tos_fw[i], cfg_getnstr(cfg, "tos-fw", i)); 115 | 116 | for (opt = fip_opts; opt->type; opt++) { 117 | const char *file; 118 | 119 | if (opt->flags != CFGF_NODEFAULT) 120 | continue; 121 | 122 | file = cfg_getstr(cfg, opt->name); 123 | if (file) 124 | fip_add_part(image, opt->name, file); 125 | } 126 | 127 | return 0; 128 | } 129 | 130 | struct image_handler fip_handler = { 131 | .type = "fip", 132 | .no_rootpath = cfg_true, 133 | .generate = fip_generate, 134 | .parse = fip_parse, 135 | .opts = fip_opts, 136 | }; 137 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-fit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Sascha Hauer 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "genimage.h" 28 | 29 | static struct partition *partition_by_name(struct image *image, const char *name) 30 | { 31 | struct partition *part; 32 | 33 | list_for_each_entry(part, &image->partitions, list) 34 | if (!strcmp(part->name, name)) 35 | return part; 36 | return NULL; 37 | } 38 | 39 | static int fit_generate(struct image *image) 40 | { 41 | int ret; 42 | struct partition *part, *its; 43 | char *itspath; 44 | int itsfd; 45 | char *keydir = cfg_getstr(image->imagesec, "keydir"); 46 | char *keyopt = NULL; 47 | 48 | its = partition_by_name(image, "its"); 49 | if (!its) 50 | return -EINVAL; 51 | 52 | struct image *itsimg = image_get(its->image); 53 | 54 | xasprintf(&itspath, "%s/fit.its", tmppath()); 55 | 56 | /* Copy input its file to temporary path. Use 'cat' to ignore permissions */ 57 | ret = systemp(image, "cat '%s' > '%s'", imageoutfile(itsimg), itspath); 58 | if (ret) 59 | return ret; 60 | 61 | itsfd = open(itspath, O_WRONLY | O_APPEND); 62 | if (itsfd < 0) { 63 | printf("Cannot open %s: %s\n", itspath, strerror(errno)); 64 | return -errno; 65 | } 66 | 67 | dprintf(itsfd, "\n"); 68 | 69 | /* Add /incbin/ to each /images// node */ 70 | list_for_each_entry(part, &image->partitions, list) { 71 | struct image *child = image_get(part->image); 72 | const char *file = imageoutfile(child); 73 | const char *target = part->name; 74 | 75 | if (part == its) 76 | continue; 77 | 78 | dprintf(itsfd, "/ { images { %s { data = /incbin/(\"%s\"); };};};\n", target, file); 79 | } 80 | 81 | close(itsfd); 82 | 83 | if (keydir && *keydir) { 84 | if (*keydir != '/') { 85 | image_error(image, "'keydir' must be an absolute path\n"); 86 | return -EINVAL; 87 | } 88 | xasprintf(&keyopt, "-k '%s'", keydir); 89 | } 90 | 91 | ret = systemp(image, "%s -r %s -f '%s' '%s'", 92 | get_opt("mkimage"), keyopt ? keyopt : "", itspath, imageoutfile(image)); 93 | 94 | if (ret) 95 | image_error(image, "Failed to create FIT image\n"); 96 | 97 | return ret; 98 | } 99 | 100 | static int fit_parse(struct image *image, cfg_t *cfg) 101 | { 102 | struct partition *part; 103 | char *its = cfg_getstr(image->imagesec, "its"); 104 | 105 | part = xzalloc(sizeof *part); 106 | part->name = "its"; 107 | part->image = its; 108 | list_add_tail(&part->list, &image->partitions); 109 | 110 | return 0; 111 | } 112 | 113 | static cfg_opt_t fit_opts[] = { 114 | CFG_STR("keydir", "", CFGF_NONE), 115 | CFG_STR("its", "", CFGF_NONE), 116 | CFG_END() 117 | }; 118 | 119 | struct image_handler fit_handler = { 120 | .type = "fit", 121 | .no_rootpath = cfg_true, 122 | .generate = fit_generate, 123 | .parse = fit_parse, 124 | .opts = fit_opts, 125 | }; 126 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-flash.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Sascha Hauer , Pengutronix 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "list.h" 27 | #include "genimage.h" 28 | 29 | struct flash_image { 30 | }; 31 | 32 | static int flash_generate(struct image *image) 33 | { 34 | struct partition *part; 35 | unsigned long long end = 0; 36 | int ret; 37 | 38 | ret = prepare_image(image, image->size); 39 | if (ret < 0) 40 | return ret; 41 | 42 | list_for_each_entry(part, &image->partitions, list) { 43 | struct image *child = NULL; 44 | 45 | image_info(image, "writing image partition '%s' (0x%llx@0x%llx)\n", 46 | part->name, part->size, part->offset); 47 | 48 | if (part->offset > end) { 49 | ret = insert_image(image, NULL, part->offset - end, end, 0xFF); 50 | if (ret) { 51 | image_error(image, "failed to pad image to size %lld\n", 52 | part->offset); 53 | return ret; 54 | } 55 | } 56 | 57 | if (part->image) 58 | child = image_get(part->image); 59 | 60 | ret = insert_image(image, child, part->size, part->offset, 0xFF); 61 | if (ret) { 62 | image_error(image, "failed to write image partition '%s'\n", 63 | part->name); 64 | return ret; 65 | } 66 | end = part->offset + part->size; 67 | } 68 | 69 | return 0; 70 | } 71 | 72 | static int flash_setup(struct image *image, cfg_t *cfg) 73 | { 74 | struct flash_image *f = xzalloc(sizeof(*f)); 75 | struct partition *part; 76 | int last = 0; 77 | unsigned long long partsize = 0, flashsize; 78 | 79 | image->handler_priv = f; 80 | 81 | if (!image->flash_type) { 82 | image_error(image, "no flash type given\n"); 83 | return -EINVAL; 84 | } 85 | 86 | flashsize = (unsigned long long)image->flash_type->pebsize * image->flash_type->numpebs; 87 | 88 | list_for_each_entry(part, &image->partitions, list) { 89 | if (last) { 90 | image_error(image, "only last partition may have size 0\n"); 91 | return -EINVAL; 92 | } 93 | 94 | if (!part->size) { 95 | last = 1; 96 | if (partsize > flashsize) 97 | goto err_exceed; 98 | part->size = flashsize - partsize; 99 | } 100 | if (part->size % image->flash_type->pebsize) { 101 | image_error(image, "part %s size (%lld) must be a " 102 | "multiple of erase block size (%i bytes)\n", 103 | part->name, part->size, image->flash_type->pebsize); 104 | return -EINVAL; 105 | } 106 | if (part->offset % image->flash_type->pebsize) { 107 | image_error(image, "part %s offset (%lld) must be a" 108 | "multiple of erase block size (%i bytes)\n", 109 | part->name, part->offset, image->flash_type->pebsize); 110 | return -EINVAL; 111 | } 112 | if (part->offset) { 113 | if (partsize > part->offset) { 114 | image_error(image, "part %s overlaps with previous partition\n", 115 | part->name); 116 | return -EINVAL; 117 | } 118 | } else { 119 | part->offset = partsize; 120 | } 121 | if (part->image) { 122 | struct image *child = image_get(part->image); 123 | if (!child) { 124 | image_error(image, "could not find %s\n", 125 | part->image); 126 | return -EINVAL; 127 | } 128 | if (child->size > part->size) { 129 | image_error(image, "part %s size (%lld) too small for %s (%lld)\n", 130 | part->name, part->size, child->file, child->size); 131 | return -EINVAL; 132 | } 133 | } 134 | 135 | partsize = part->offset + part->size; 136 | } 137 | 138 | if (partsize > flashsize) { 139 | err_exceed: 140 | image_error(image, "size of partitions (%lld) exceeds flash size (%lld)\n", 141 | partsize, flashsize); 142 | return -EINVAL; 143 | } 144 | if (!image->size) 145 | image->size = partsize; 146 | 147 | return 0; 148 | } 149 | 150 | static cfg_opt_t flash_opts[] = { 151 | CFG_END() 152 | }; 153 | 154 | struct image_handler flash_handler = { 155 | .type = "flash", 156 | .no_rootpath = cfg_true, 157 | .generate = flash_generate, 158 | .setup = flash_setup, 159 | .opts = flash_opts, 160 | }; 161 | 162 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-iso.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 Michael Olbrich 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int iso_generate(struct image *image) 26 | { 27 | int ret; 28 | char *boot; 29 | char *boot_image = cfg_getstr(image->imagesec, "boot-image"); 30 | char *bootargs = cfg_getstr(image->imagesec, "bootargs"); 31 | char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 32 | char *input_charset = cfg_getstr(image->imagesec, "input-charset"); 33 | char *volume_id = cfg_getstr(image->imagesec, "volume-id"); 34 | 35 | if (boot_image) 36 | xasprintf(&boot, "-b '%s' %s", boot_image, bootargs); 37 | else 38 | boot = ""; 39 | 40 | ret = systemp(image, "%s -input-charset %s -R -hide-rr-moved %s -V '%s' %s -o '%s' '%s'", 41 | get_opt("genisoimage"), 42 | input_charset, 43 | boot, 44 | volume_id, 45 | extraargs, 46 | imageoutfile(image), 47 | mountpath(image)); 48 | return ret; 49 | } 50 | 51 | static cfg_opt_t iso_opts[] = { 52 | CFG_STR("boot-image", NULL, CFGF_NONE), 53 | CFG_STR("bootargs", "-no-emul-boot -boot-load-size 4 -boot-info-table -c boot.cat -hide boot.cat", CFGF_NONE), 54 | CFG_STR("extraargs", "", CFGF_NONE), 55 | CFG_STR("input-charset", "default", CFGF_NONE), 56 | CFG_STR("volume-id", "", CFGF_NONE), 57 | CFG_END() 58 | }; 59 | 60 | struct image_handler iso_handler = { 61 | .type = "iso", 62 | .generate = iso_generate, 63 | .opts = iso_opts, 64 | }; 65 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-jffs2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Sascha Hauer , Pengutronix 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int jffs2_generate(struct image *image) 26 | { 27 | int ret; 28 | char *extraargs; 29 | 30 | extraargs = cfg_getstr(image->imagesec, "extraargs"); 31 | 32 | ret = systemp(image, "%s --eraseblock=%d %s%s%s -o '%s' %s", 33 | get_opt("mkfsjffs2"), 34 | image->flash_type->pebsize, 35 | image->empty ? "" : "-d '", 36 | image->empty ? "" : mountpath(image), 37 | image->empty ? "" : "'", 38 | imageoutfile(image), extraargs); 39 | 40 | return ret; 41 | } 42 | 43 | static int jffs2_setup(struct image *image, cfg_t *cfg) 44 | { 45 | if (!image->flash_type) { 46 | image_error(image, "no flash type given\n"); 47 | return -EINVAL; 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | static cfg_opt_t jffs2_opts[] = { 54 | CFG_STR("extraargs", "", CFGF_NONE), 55 | CFG_END() 56 | }; 57 | 58 | struct image_handler jffs2_handler = { 59 | .type = "jffs2", 60 | .generate = jffs2_generate, 61 | .setup = jffs2_setup, 62 | .opts = jffs2_opts, 63 | }; 64 | 65 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-qemu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Alexandre Fournier , Kiplink 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | struct qemu { 26 | const char *format; 27 | const char *extraargs; 28 | }; 29 | 30 | static int qemu_generate(struct image *image) 31 | { 32 | struct partition *part; 33 | struct qemu *qemu = image->handler_priv; 34 | char *partitions = NULL; 35 | int ret; 36 | 37 | list_for_each_entry(part, &image->partitions, list) { 38 | struct image *child; 39 | const char *infile; 40 | 41 | if (!part->image) { 42 | image_debug(image, "skipping partition %s\n", 43 | part->name); 44 | continue; 45 | } 46 | 47 | image_info(image, "adding partition %s from %s ...\n", 48 | part->name, part->image); 49 | 50 | child = image_get(part->image); 51 | infile = imageoutfile(child); 52 | 53 | if (!partitions) 54 | xasprintf(&partitions, "'%s'", infile); 55 | else 56 | xasprintf(&partitions, "%s '%s'", partitions, infile); 57 | } 58 | 59 | ret = systemp(image, "qemu-img convert %s -O %s %s '%s'", 60 | qemu->extraargs, 61 | qemu->format, 62 | partitions, 63 | imageoutfile(image)); 64 | 65 | return ret; 66 | } 67 | 68 | static int qemu_setup(struct image *image, cfg_t *cfg) 69 | { 70 | struct qemu *qemu = xzalloc(sizeof(*qemu)); 71 | struct partition *part; 72 | int partitions_count = 0; 73 | 74 | list_for_each_entry(part, &image->partitions, list) { 75 | if (part->image) 76 | partitions_count++; 77 | } 78 | 79 | if (partitions_count == 0) { 80 | image_error(image, "no partition given\n"); 81 | return -EINVAL; 82 | } 83 | 84 | qemu->format = cfg_getstr(cfg, "format"); 85 | qemu->extraargs = cfg_getstr(cfg, "extraargs"); 86 | 87 | image->handler_priv = qemu; 88 | 89 | return 0; 90 | } 91 | 92 | static cfg_opt_t qemu_opts[] = { 93 | CFG_STR("format", "qcow2", CFGF_NONE), 94 | CFG_STR("extraargs", "", CFGF_NONE), 95 | CFG_END() 96 | }; 97 | 98 | struct image_handler qemu_handler = { 99 | .type = "qemu", 100 | .no_rootpath = cfg_true, 101 | .generate = qemu_generate, 102 | .setup = qemu_setup, 103 | .opts = qemu_opts, 104 | }; 105 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-rauc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Michael Olbrich 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | #define RAUC_CONTENT 0 26 | #define RAUC_KEY 1 27 | #define RAUC_CERT 2 28 | #define RAUC_KEYRING 3 29 | #define RAUC_INTERMEDIATE 4 30 | 31 | static const char *pkcs11_prefix = "pkcs11:"; 32 | 33 | static int rauc_generate(struct image *image) 34 | { 35 | int ret; 36 | struct partition *part; 37 | const char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 38 | const char *manifest = cfg_getstr(image->imagesec, "manifest"); 39 | const char *cert = cfg_getstr(image->imagesec, "cert"); 40 | const char *key = cfg_getstr(image->imagesec, "key"); 41 | const char *keyring = cfg_getstr(image->imagesec, "keyring"); 42 | char *keyringarg = NULL; 43 | char *manifest_file = NULL; 44 | char *tmpdir = NULL; 45 | char *intermediatearg = NULL; 46 | unsigned int i; 47 | 48 | image_debug(image, "manifest = '%s'\n", manifest); 49 | 50 | xasprintf(&tmpdir, "%s/rauc-%s", tmppath(), sanitize_path(image->file)); 51 | ret = systemp(image, "mkdir -p '%s'", tmpdir); 52 | if (ret) 53 | goto out; 54 | 55 | xasprintf(&manifest_file, "%s/manifest.raucm", tmpdir); 56 | ret = insert_data(image, manifest, manifest_file, strlen(manifest), 0); 57 | if (ret) 58 | goto out; 59 | 60 | for (i = 0; i < cfg_size(image->imagesec, "intermediate"); i++) { 61 | const char *uri; 62 | 63 | uri = cfg_getnstr(image->imagesec, "intermediate", i); 64 | if (!strncmp(pkcs11_prefix, uri, strlen(pkcs11_prefix))) 65 | xstrcatf(&intermediatearg, " --intermediate='%s'", uri); 66 | } 67 | list_for_each_entry(part, &image->partitions, list) { 68 | struct image *child = image_get(part->image); 69 | const char *file = imageoutfile(child); 70 | const char *target = part->name; 71 | char *path, *tmp; 72 | 73 | if (part->partition_type == RAUC_CERT) 74 | cert = file; 75 | 76 | if (part->partition_type == RAUC_KEY) 77 | key = file; 78 | 79 | if (part->partition_type == RAUC_KEYRING) 80 | keyring = file; 81 | 82 | if (part->partition_type == RAUC_INTERMEDIATE) 83 | xstrcatf(&intermediatearg, " --intermediate='%s'", file); 84 | 85 | if (part->partition_type != RAUC_CONTENT) 86 | continue; 87 | 88 | if (!target) { 89 | /* use basename from source as target name */ 90 | tmp = strrchr(child->file, '/'); 91 | if (tmp) 92 | target = tmp + 1; 93 | else 94 | target = child->file; 95 | } 96 | 97 | /* create parent directories if target needs it */ 98 | path = strdupa(target); 99 | tmp = strrchr(path, '/'); 100 | if (tmp) { 101 | *tmp = '\0'; 102 | ret = systemp(image, "mkdir -p '%s/%s'", 103 | tmpdir, path); 104 | if (ret) 105 | goto out; 106 | } 107 | 108 | image_info(image, "adding file '%s' as '%s' ...\n", 109 | child->file, target); 110 | ret = systemp(image, "cp --remove-destination '%s' '%s/%s'", 111 | file, tmpdir, target); 112 | if (ret) 113 | goto out; 114 | } 115 | 116 | if (keyring) 117 | xasprintf(&keyringarg, "--keyring='%s'", keyring); 118 | 119 | systemp(image, "rm -f '%s'", imageoutfile(image)); 120 | 121 | ret = systemp(image, "%s bundle '%s' --cert='%s' --key='%s' %s %s %s '%s'", 122 | get_opt("rauc"), tmpdir, cert, key, 123 | (keyringarg ? keyringarg : ""), 124 | (intermediatearg ? intermediatearg : ""), 125 | extraargs, imageoutfile(image)); 126 | 127 | out: 128 | free(keyringarg); 129 | free(tmpdir); 130 | free(manifest_file); 131 | free(intermediatearg); 132 | 133 | return ret; 134 | } 135 | 136 | static int rauc_parse(struct image *image, cfg_t *cfg) 137 | { 138 | unsigned int i; 139 | unsigned int num_files; 140 | struct partition *part; 141 | char *part_image_key; 142 | char *part_image_cert; 143 | char *part_image_keyring; 144 | 145 | part_image_key = cfg_getstr(image->imagesec, "key"); 146 | if (!part_image_key) { 147 | image_error(image, "Mandatory 'key' option is missing!\n"); 148 | return -EINVAL; 149 | } 150 | if (strncmp(pkcs11_prefix, part_image_key, strlen(pkcs11_prefix))) { 151 | part = xzalloc(sizeof *part); 152 | part->image = part_image_key; 153 | part->partition_type = RAUC_KEY; 154 | list_add_tail(&part->list, &image->partitions); 155 | } 156 | 157 | part_image_cert = cfg_getstr(image->imagesec, "cert"); 158 | if (!part_image_cert) { 159 | image_error(image, "Mandatory 'cert' option is missing!\n"); 160 | return -EINVAL; 161 | } 162 | if (strncmp(pkcs11_prefix, part_image_cert, strlen(pkcs11_prefix))) { 163 | part = xzalloc(sizeof *part); 164 | part->image = part_image_cert; 165 | part->partition_type = RAUC_CERT; 166 | list_add_tail(&part->list, &image->partitions); 167 | } 168 | 169 | part_image_keyring = cfg_getstr(image->imagesec, "keyring"); 170 | if (part_image_keyring) { 171 | part = xzalloc(sizeof *part); 172 | part->image = part_image_keyring; 173 | part->partition_type = RAUC_KEYRING; 174 | list_add_tail(&part->list, &image->partitions); 175 | } 176 | 177 | for (i = 0; i < cfg_size(cfg, "intermediate"); i++) { 178 | char *part_image_intermediate; 179 | 180 | part_image_intermediate = cfg_getnstr(cfg, "intermediate", i); 181 | if (strncmp(pkcs11_prefix, part_image_intermediate, 182 | strlen(pkcs11_prefix))) { 183 | part = xzalloc(sizeof *part); 184 | part->image = part_image_intermediate; 185 | part->partition_type = RAUC_INTERMEDIATE; 186 | list_add_tail(&part->list, &image->partitions); 187 | } 188 | } 189 | 190 | num_files = cfg_size(cfg, "file"); 191 | for (i = 0; i < num_files; i++) { 192 | cfg_t *filesec = cfg_getnsec(cfg, "file", i); 193 | part = xzalloc(sizeof *part); 194 | part->name = cfg_title(filesec); 195 | part->image = cfg_getstr(filesec, "image"); 196 | part->partition_type = RAUC_CONTENT; 197 | list_add_tail(&part->list, &image->partitions); 198 | } 199 | 200 | for(i = 0; i < cfg_size(cfg, "files"); i++) { 201 | part = xzalloc(sizeof *part); 202 | part->image = cfg_getnstr(cfg, "files", i); 203 | part->partition_type = RAUC_CONTENT; 204 | list_add_tail(&part->list, &image->partitions); 205 | } 206 | 207 | return 0; 208 | } 209 | 210 | static int rauc_setup(struct image *image, cfg_t *cfg) 211 | { 212 | char *manifest = cfg_getstr(image->imagesec, "manifest"); 213 | if (!manifest) { 214 | image_error(image, "Mandatory 'manifest' option is missing!\n"); 215 | return -EINVAL; 216 | } 217 | return 0; 218 | } 219 | 220 | static cfg_opt_t file_opts[] = { 221 | CFG_STR("image", NULL, CFGF_NONE), 222 | CFG_END() 223 | }; 224 | 225 | static cfg_opt_t rauc_opts[] = { 226 | CFG_STR("extraargs", "", CFGF_NONE), 227 | CFG_STR_LIST("files", NULL, CFGF_NONE), 228 | CFG_SEC("file", file_opts, CFGF_MULTI | CFGF_TITLE), 229 | CFG_STR("key", NULL, CFGF_NONE), 230 | CFG_STR("cert", NULL, CFGF_NONE), 231 | CFG_STR("keyring", NULL, CFGF_NONE), 232 | CFG_STR_LIST("intermediate", 0, CFGF_NONE), 233 | CFG_STR("manifest", NULL, CFGF_NONE), 234 | CFG_END() 235 | }; 236 | 237 | struct image_handler rauc_handler = { 238 | .type = "rauc", 239 | .no_rootpath = cfg_true, 240 | .generate = rauc_generate, 241 | .parse = rauc_parse, 242 | .setup = rauc_setup, 243 | .opts = rauc_opts, 244 | }; 245 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-squashfs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Juergen Beisert 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "genimage.h" 27 | 28 | static int squash_generate(struct image *image) 29 | { 30 | char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 31 | char compression[128]; 32 | char *comp_setup = cfg_getstr(image->imagesec, "compression"); 33 | unsigned block_size = cfg_getint_suffix(image->imagesec, "block-size"); 34 | unsigned long long file_size; 35 | struct stat sb; 36 | int ret; 37 | 38 | /* 39 | * 'mksquashfs' currently defaults to 'gzip' compression. Provide a shortcut 40 | * to be able to disable all kind of compression and force the current 41 | * default behaviour for the future. Disabling compression is very useful 42 | * to handle binary diffs. 43 | */ 44 | if (!strcasecmp(comp_setup, "none")) 45 | strncpy(compression, "-comp gzip -noInodeCompression -noDataCompression -noFragmentCompression -noXattrCompression", sizeof(compression)); 46 | else 47 | snprintf(compression, sizeof(compression), "-comp %s", comp_setup); 48 | 49 | ret = systemp(image, "%s '%s' '%s' -b %u -noappend %s %s", 50 | get_opt("mksquashfs"), 51 | mountpath(image), /* source dir */ 52 | imageoutfile(image), /* destination file */ 53 | block_size, compression, extraargs); 54 | if (ret) 55 | return ret; 56 | ret = stat(imageoutfile(image), &sb); 57 | if (ret) { 58 | ret = -errno; 59 | image_error(image, "stat(%s) failed: %s\n", imageoutfile(image), strerror(errno)); 60 | return ret; 61 | } 62 | file_size = sb.st_size; 63 | 64 | if (image->size && file_size > image->size) { 65 | image_error(image, "generated image %s is larger than given image size (%llu v %llu)\n", 66 | imageoutfile(image), file_size, image->size); 67 | return -E2BIG; 68 | } 69 | 70 | image_debug(image, "setting image size to %llu bytes\n", file_size); 71 | image->size = file_size; 72 | 73 | return 0; 74 | } 75 | 76 | /** 77 | * 'compression' can be 'gzip' (the current default), 'lzo', 'xz' or 'none' 78 | * @note 'none' is a special keyword to add the parameters '-noInodeCompression -noDataCompression -noFragmentCompression -noXattrCompression' 79 | */ 80 | static cfg_opt_t squash_opts[] = { 81 | CFG_STR("extraargs", "", CFGF_NONE), 82 | CFG_STR("compression", "gzip", CFGF_NONE), 83 | CFG_STR("block-size", "4096", CFGF_NONE), 84 | CFG_END() 85 | }; 86 | 87 | struct image_handler squashfs_handler = { 88 | .type = "squashfs", 89 | .generate = squash_generate, 90 | .opts = squash_opts, 91 | }; 92 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-tar.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Sascha Hauer , Pengutronix 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int tar_generate(struct image *image) 26 | { 27 | int ret; 28 | char *comp = "a"; 29 | 30 | if (strstr(image->file, ".tar.gz") || strstr(image->file, "tgz")) 31 | comp = "z"; 32 | if (strstr(image->file, ".tar.bz2")) 33 | comp = "j"; 34 | 35 | ret = systemp(image, "%s c%s -f '%s' -C '%s' .", 36 | get_opt("tar"), 37 | comp, 38 | imageoutfile(image), mountpath(image)); 39 | 40 | return ret; 41 | } 42 | 43 | static cfg_opt_t tar_opts[] = { 44 | CFG_END() 45 | }; 46 | 47 | struct image_handler tar_handler = { 48 | .type = "tar", 49 | .generate = tar_generate, 50 | .opts = tar_opts, 51 | }; 52 | 53 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-ubi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Sascha Hauer , Pengutronix 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | struct ubi { 26 | }; 27 | 28 | static int ubi_generate(struct image *image) 29 | { 30 | int ret; 31 | FILE *fini; 32 | char *tempfile; 33 | int i = 0; 34 | struct partition *part; 35 | char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 36 | 37 | xasprintf(&tempfile, "%s/ubi.ini", tmppath()); 38 | if (!tempfile) 39 | return -ENOMEM; 40 | 41 | fini = fopen(tempfile, "w"); 42 | if (!fini) { 43 | ret = -errno; 44 | image_error(image, "creating temp file failed: %s\n", strerror(errno)); 45 | goto err_free; 46 | } 47 | 48 | list_for_each_entry(part, &image->partitions, list) { 49 | struct image *child = NULL; 50 | unsigned long long size = part->size; 51 | if (part->image) 52 | child = image_get(part->image); 53 | if (!size) { 54 | if (!child) { 55 | image_error(image, "could not find %s\n", part->image); 56 | fclose(fini); 57 | ret = -EINVAL; 58 | goto err_free; 59 | } 60 | size = child->size; 61 | } 62 | 63 | fprintf(fini, "[%s]\n", part->name); 64 | fprintf(fini, "mode=ubi\n"); 65 | if (child) 66 | fprintf(fini, "image=%s\n", imageoutfile(child)); 67 | fprintf(fini, "vol_id=%d\n", i); 68 | fprintf(fini, "vol_size=%lld\n", size); 69 | fprintf(fini, "vol_type=%s\n", part->read_only ? "static" : "dynamic"); 70 | fprintf(fini, "vol_name=%s\n", part->name); 71 | if (part->autoresize) 72 | fprintf(fini, "vol_flags=autoresize\n"); 73 | fprintf(fini, "vol_alignment=1\n"); 74 | i++; 75 | } 76 | 77 | fclose(fini); 78 | 79 | ret = systemp(image, "%s -s %d -O %d -p %d -m %d -o '%s' '%s' %s", 80 | get_opt("ubinize"), 81 | image->flash_type->sub_page_size, 82 | image->flash_type->vid_header_offset, 83 | image->flash_type->pebsize, 84 | image->flash_type->minimum_io_unit_size, 85 | imageoutfile(image), 86 | tempfile, 87 | extraargs); 88 | 89 | err_free: 90 | free(tempfile); 91 | 92 | return ret; 93 | } 94 | 95 | static int ubi_setup(struct image *image, cfg_t *cfg) 96 | { 97 | struct ubi *ubi = xzalloc(sizeof(*ubi)); 98 | int autoresize = 0; 99 | struct partition *part; 100 | 101 | if (!image->flash_type) { 102 | image_error(image, "no flash type given\n"); 103 | return -EINVAL; 104 | } 105 | 106 | image->handler_priv = ubi; 107 | 108 | list_for_each_entry(part, &image->partitions, list) 109 | autoresize += part->autoresize; 110 | 111 | if (autoresize > 1) { 112 | image_error(image, "more than one volume has the autoresize flag set\n"); 113 | return -EINVAL; 114 | } 115 | 116 | return 0; 117 | } 118 | 119 | static cfg_opt_t ubi_opts[] = { 120 | CFG_STR("extraargs", "", CFGF_NONE), 121 | CFG_END() 122 | }; 123 | 124 | struct image_handler ubi_handler = { 125 | .type = "ubi", 126 | .no_rootpath = cfg_true, 127 | .generate = ubi_generate, 128 | .setup = ubi_setup, 129 | .opts = ubi_opts, 130 | }; 131 | 132 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-ubifs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Sascha Hauer , Pengutronix 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int ubifs_generate(struct image *image) 26 | { 27 | int max_leb_cnt; 28 | int ret; 29 | char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 30 | unsigned long long max_size = cfg_getint_suffix(image->imagesec, "max-size"); 31 | 32 | if (max_size) 33 | max_leb_cnt = max_size / image->flash_type->lebsize; 34 | else 35 | max_leb_cnt = image->size / image->flash_type->lebsize; 36 | 37 | ret = systemp(image, "%s %s%s%s -e %d -m %d -c %d -o '%s' %s", 38 | get_opt("mkfsubifs"), 39 | image->empty ? "" : "-d '", 40 | image->empty ? "" : mountpath(image), 41 | image->empty ? "" : "'", 42 | image->flash_type->lebsize, 43 | image->flash_type->minimum_io_unit_size, 44 | max_leb_cnt, 45 | imageoutfile(image), 46 | extraargs); 47 | 48 | return ret; 49 | } 50 | 51 | static int ubifs_setup(struct image *image, cfg_t *cfg) 52 | { 53 | if (!image->flash_type) { 54 | image_error(image, "no flash type given\n"); 55 | return -EINVAL; 56 | } 57 | if (image->flash_type->lebsize <= 0) { 58 | image_error(image, "invalid lebsize (%d) in %s\n", 59 | image->flash_type->lebsize, image->flash_type->name); 60 | return -EINVAL; 61 | } 62 | 63 | return 0; 64 | } 65 | 66 | static cfg_opt_t ubifs_opts[] = { 67 | CFG_STR("extraargs", "", CFGF_NONE), 68 | CFG_STR("max-size", NULL, CFGF_NONE), 69 | CFG_END() 70 | }; 71 | 72 | struct image_handler ubifs_handler = { 73 | .type = "ubifs", 74 | .generate = ubifs_generate, 75 | .setup = ubifs_setup, 76 | .opts = ubifs_opts, 77 | }; 78 | 79 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/image-vfat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 Michael Olbrich 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 6 | * as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "genimage.h" 24 | 25 | static int vfat_generate(struct image *image) 26 | { 27 | int ret; 28 | struct partition *part; 29 | char *extraargs = cfg_getstr(image->imagesec, "extraargs"); 30 | char *label = cfg_getstr(image->imagesec, "label"); 31 | 32 | if (label && label[0] != '\0') 33 | xasprintf(&label, "-n '%s'", label); 34 | else 35 | label = ""; 36 | 37 | ret = prepare_image(image, image->size); 38 | if (ret) 39 | return ret; 40 | 41 | ret = systemp(image, "%s %s %s '%s'", get_opt("mkdosfs"), 42 | extraargs, label, imageoutfile(image)); 43 | if (ret) 44 | return ret; 45 | 46 | list_for_each_entry(part, &image->partitions, list) { 47 | struct image *child = image_get(part->image); 48 | const char *file = imageoutfile(child); 49 | const char *target = part->name; 50 | char *path = strdupa(target); 51 | char *next = path; 52 | 53 | while ((next = strchr(next, '/')) != NULL) { 54 | *next = '\0'; 55 | /* ignore the error: mdd fails if the target exists. */ 56 | systemp(image, "MTOOLS_SKIP_CHECK=1 %s -DsS -i %s '::%s'", 57 | get_opt("mmd"), imageoutfile(image), path); 58 | *next = '/'; 59 | ++next; 60 | } 61 | 62 | image_info(image, "adding file '%s' as '%s' ...\n", 63 | child->file, *target ? target : child->file); 64 | ret = systemp(image, "MTOOLS_SKIP_CHECK=1 %s -sp -i '%s' '%s' '::%s'", 65 | get_opt("mcopy"), imageoutfile(image), 66 | file, target); 67 | if (ret) 68 | return ret; 69 | } 70 | if (!list_empty(&image->partitions)) 71 | return 0; 72 | 73 | if (!image->empty) 74 | ret = systemp(image, "MTOOLS_SKIP_CHECK=1 %s -sp -i '%s' '%s'/* ::", 75 | get_opt("mcopy"), imageoutfile(image), mountpath(image)); 76 | return ret; 77 | } 78 | 79 | static int vfat_setup(struct image *image, cfg_t *cfg) 80 | { 81 | char *label = cfg_getstr(image->imagesec, "label"); 82 | 83 | if (!image->size) { 84 | image_error(image, "no size given or must not be zero\n"); 85 | return -EINVAL; 86 | } 87 | 88 | if (label && strlen(label) > 11) { 89 | image_error(image, "vfat volume name cannot be longer than 11 characters\n"); 90 | return -EINVAL; 91 | } 92 | 93 | return 0; 94 | } 95 | 96 | static int vfat_parse(struct image *image, cfg_t *cfg) 97 | { 98 | unsigned int i; 99 | unsigned int num_files; 100 | struct partition *part; 101 | 102 | num_files = cfg_size(cfg, "file"); 103 | for (i = 0; i < num_files; i++) { 104 | cfg_t *filesec = cfg_getnsec(cfg, "file", i); 105 | part = xzalloc(sizeof *part); 106 | part->name = cfg_title(filesec); 107 | part->image = cfg_getstr(filesec, "image"); 108 | list_add_tail(&part->list, &image->partitions); 109 | } 110 | 111 | for(i = 0; i < cfg_size(cfg, "files"); i++) { 112 | part = xzalloc(sizeof *part); 113 | part->image = cfg_getnstr(cfg, "files", i); 114 | part->name = ""; 115 | list_add_tail(&part->list, &image->partitions); 116 | } 117 | 118 | return 0; 119 | } 120 | 121 | static cfg_opt_t file_opts[] = { 122 | CFG_STR("image", NULL, CFGF_NONE), 123 | CFG_END() 124 | }; 125 | 126 | static cfg_opt_t vfat_opts[] = { 127 | CFG_STR("extraargs", "", CFGF_NONE), 128 | CFG_STR("label", "", CFGF_NONE), 129 | CFG_STR_LIST("files", NULL, CFGF_NONE), 130 | CFG_SEC("file", file_opts, CFGF_MULTI | CFGF_TITLE), 131 | CFG_END() 132 | }; 133 | 134 | struct image_handler vfat_handler = { 135 | .type = "vfat", 136 | .generate = vfat_generate, 137 | .setup = vfat_setup, 138 | .parse = vfat_parse, 139 | .opts = vfat_opts, 140 | }; 141 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/m4/attributes.m4: -------------------------------------------------------------------------------- 1 | dnl Macros to check the presence of generic (non-typed) symbols. 2 | dnl Copyright (c) 2006-2008 Diego Pettenò 3 | dnl Copyright (c) 2006-2008 xine project 4 | dnl 5 | dnl This program is free software; you can redistribute it and/or modify 6 | dnl it under the terms of the GNU General Public License as published by 7 | dnl the Free Software Foundation; either version 2, or (at your option) 8 | dnl any later version. 9 | dnl 10 | dnl This program is distributed in the hope that it will be useful, 11 | dnl but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | dnl GNU General Public License for more details. 14 | dnl 15 | dnl You should have received a copy of the GNU General Public License 16 | dnl along with this program; if not, write to the Free Software 17 | dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 18 | dnl 02110-1301, USA. 19 | dnl 20 | dnl As a special exception, the copyright owners of the 21 | dnl macro gives unlimited permission to copy, distribute and modify the 22 | dnl configure scripts that are the output of Autoconf when processing the 23 | dnl Macro. You need not follow the terms of the GNU General Public 24 | dnl License when using or distributing such scripts, even though portions 25 | dnl of the text of the Macro appear in them. The GNU General Public 26 | dnl License (GPL) does govern all other use of the material that 27 | dnl constitutes the Autoconf Macro. 28 | dnl 29 | dnl This special exception to the GPL applies to versions of the 30 | dnl Autoconf Macro released by this project. When you make and 31 | dnl distribute a modified version of the Autoconf Macro, you may extend 32 | dnl this special exception to the GPL to apply to your modified version as 33 | dnl well. 34 | 35 | dnl Check if the flag is supported by compiler 36 | dnl CC_CHECK_CFLAGS_SILENT([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND]) 37 | 38 | AC_DEFUN([CC_CHECK_CFLAGS_SILENT], [ 39 | AC_CACHE_VAL(AS_TR_SH([cc_cv_cflags_$1]), 40 | [ac_save_CFLAGS="$CFLAGS" 41 | CFLAGS="$CFLAGS $1" 42 | AC_COMPILE_IFELSE([AC_LANG_SOURCE([int a;])], 43 | [eval "AS_TR_SH([cc_cv_cflags_$1])='yes'"], 44 | [eval "AS_TR_SH([cc_cv_cflags_$1])='no'"]) 45 | CFLAGS="$ac_save_CFLAGS" 46 | ]) 47 | 48 | AS_IF([eval test x$]AS_TR_SH([cc_cv_cflags_$1])[ = xyes], 49 | [$2], [$3]) 50 | ]) 51 | 52 | dnl Check if the flag is supported by compiler (cacheable) 53 | dnl CC_CHECK_CFLAGS([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND]) 54 | 55 | AC_DEFUN([CC_CHECK_CFLAGS], [ 56 | AC_CACHE_CHECK([if $CC supports $1 flag], 57 | AS_TR_SH([cc_cv_cflags_$1]), 58 | CC_CHECK_CFLAGS_SILENT([$1]) dnl Don't execute actions here! 59 | ) 60 | 61 | AS_IF([eval test x$]AS_TR_SH([cc_cv_cflags_$1])[ = xyes], 62 | [$2], [$3]) 63 | ]) 64 | 65 | dnl CC_CHECK_CFLAG_APPEND(FLAG, [action-if-found], [action-if-not-found]) 66 | dnl Check for CFLAG and appends them to CFLAGS if supported 67 | AC_DEFUN([CC_CHECK_CFLAG_APPEND], [ 68 | AC_CACHE_CHECK([if $CC supports $1 flag], 69 | AS_TR_SH([cc_cv_cflags_$1]), 70 | CC_CHECK_CFLAGS_SILENT([$1]) dnl Don't execute actions here! 71 | ) 72 | 73 | AS_IF([eval test x$]AS_TR_SH([cc_cv_cflags_$1])[ = xyes], 74 | [CFLAGS="$CFLAGS $1"; DEBUG_CFLAGS="$DEBUG_CFLAGS $1"; $2], [$3]) 75 | ]) 76 | 77 | dnl CC_CHECK_CFLAGS_APPEND([FLAG1 FLAG2], [action-if-found], [action-if-not]) 78 | AC_DEFUN([CC_CHECK_CFLAGS_APPEND], [ 79 | for flag in $1; do 80 | CC_CHECK_CFLAG_APPEND($flag, [$2], [$3]) 81 | done 82 | ]) 83 | 84 | dnl Check if the flag is supported by linker (cacheable) 85 | dnl CC_CHECK_LDFLAGS([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND]) 86 | 87 | AC_DEFUN([CC_CHECK_LDFLAGS], [ 88 | AC_CACHE_CHECK([if $CC supports $1 flag], 89 | AS_TR_SH([cc_cv_ldflags_$1]), 90 | [ac_save_LDFLAGS="$LDFLAGS" 91 | LDFLAGS="$LDFLAGS $1" 92 | AC_LINK_IFELSE([int main() { return 1; }], 93 | [eval "AS_TR_SH([cc_cv_ldflags_$1])='yes'"], 94 | [eval "AS_TR_SH([cc_cv_ldflags_$1])="]) 95 | LDFLAGS="$ac_save_LDFLAGS" 96 | ]) 97 | 98 | AS_IF([eval test x$]AS_TR_SH([cc_cv_ldflags_$1])[ = xyes], 99 | [$2], [$3]) 100 | ]) 101 | 102 | dnl define the LDFLAGS_NOUNDEFINED variable with the correct value for 103 | dnl the current linker to avoid undefined references in a shared object. 104 | AC_DEFUN([CC_NOUNDEFINED], [ 105 | dnl We check $host for which systems to enable this for. 106 | AC_REQUIRE([AC_CANONICAL_HOST]) 107 | 108 | case $host in 109 | dnl FreeBSD (et al.) does not complete linking for shared objects when pthreads 110 | dnl are requested, as different implementations are present; to avoid problems 111 | dnl use -Wl,-z,defs only for those platform not behaving this way. 112 | *-freebsd* | *-openbsd*) ;; 113 | *) 114 | dnl First of all check for the --no-undefined variant of GNU ld. This allows 115 | dnl for a much more readable commandline, so that people can understand what 116 | dnl it does without going to look for what the heck -z defs does. 117 | for possible_flags in "-Wl,--no-undefined" "-Wl,-z,defs"; do 118 | CC_CHECK_LDFLAGS([$possible_flags], [LDFLAGS_NOUNDEFINED="$possible_flags"]) 119 | break 120 | done 121 | ;; 122 | esac 123 | 124 | AC_SUBST([LDFLAGS_NOUNDEFINED]) 125 | ]) 126 | 127 | dnl Check for a -Werror flag or equivalent. -Werror is the GCC 128 | dnl and ICC flag that tells the compiler to treat all the warnings 129 | dnl as fatal. We usually need this option to make sure that some 130 | dnl constructs (like attributes) are not simply ignored. 131 | dnl 132 | dnl Other compilers don't support -Werror per se, but they support 133 | dnl an equivalent flag: 134 | dnl - Sun Studio compiler supports -errwarn=%all 135 | AC_DEFUN([CC_CHECK_WERROR], [ 136 | AC_CACHE_CHECK( 137 | [for $CC way to treat warnings as errors], 138 | [cc_cv_werror], 139 | [CC_CHECK_CFLAGS_SILENT([-Werror], [cc_cv_werror=-Werror], 140 | [CC_CHECK_CFLAGS_SILENT([-errwarn=%all], [cc_cv_werror=-errwarn=%all])]) 141 | ]) 142 | ]) 143 | 144 | AC_DEFUN([CC_CHECK_ATTRIBUTE], [ 145 | AC_REQUIRE([CC_CHECK_WERROR]) 146 | AC_CACHE_CHECK([if $CC supports __attribute__(( ifelse([$2], , [$1], [$2]) ))], 147 | AS_TR_SH([cc_cv_attribute_$1]), 148 | [ac_save_CFLAGS="$CFLAGS" 149 | CFLAGS="$CFLAGS $cc_cv_werror" 150 | AC_COMPILE_IFELSE([AC_LANG_SOURCE([$3])], 151 | [eval "AS_TR_SH([cc_cv_attribute_$1])='yes'"], 152 | [eval "AS_TR_SH([cc_cv_attribute_$1])='no'"]) 153 | CFLAGS="$ac_save_CFLAGS" 154 | ]) 155 | 156 | AS_IF([eval test x$]AS_TR_SH([cc_cv_attribute_$1])[ = xyes], 157 | [AC_DEFINE( 158 | AS_TR_CPP([SUPPORT_ATTRIBUTE_$1]), 1, 159 | [Define this if the compiler supports __attribute__(( ifelse([$2], , [$1], [$2]) ))] 160 | ) 161 | $4], 162 | [$5]) 163 | ]) 164 | 165 | AC_DEFUN([CC_ATTRIBUTE_CONSTRUCTOR], [ 166 | CC_CHECK_ATTRIBUTE( 167 | [constructor],, 168 | [void __attribute__((constructor)) ctor() { int a; }], 169 | [$1], [$2]) 170 | ]) 171 | 172 | AC_DEFUN([CC_ATTRIBUTE_FORMAT], [ 173 | CC_CHECK_ATTRIBUTE( 174 | [format], [format(printf, n, n)], 175 | [void __attribute__((format(printf, 1, 2))) printflike(const char *fmt, ...) { fmt = (void *)0; }], 176 | [$1], [$2]) 177 | ]) 178 | 179 | AC_DEFUN([CC_ATTRIBUTE_FORMAT_ARG], [ 180 | CC_CHECK_ATTRIBUTE( 181 | [format_arg], [format_arg(printf)], 182 | [char *__attribute__((format_arg(1))) gettextlike(const char *fmt) { fmt = (void *)0; }], 183 | [$1], [$2]) 184 | ]) 185 | 186 | AC_DEFUN([CC_ATTRIBUTE_VISIBILITY], [ 187 | CC_CHECK_ATTRIBUTE( 188 | [visibility_$1], [visibility("$1")], 189 | [void __attribute__((visibility("$1"))) $1_function() { }], 190 | [$2], [$3]) 191 | ]) 192 | 193 | AC_DEFUN([CC_ATTRIBUTE_NONNULL], [ 194 | CC_CHECK_ATTRIBUTE( 195 | [nonnull], [nonnull()], 196 | [void __attribute__((nonnull())) some_function(void *foo, void *bar) { foo = (void*)0; bar = (void*)0; }], 197 | [$1], [$2]) 198 | ]) 199 | 200 | AC_DEFUN([CC_ATTRIBUTE_UNUSED], [ 201 | CC_CHECK_ATTRIBUTE( 202 | [unused], , 203 | [void some_function(void *foo, __attribute__((unused)) void *bar);], 204 | [$1], [$2]) 205 | ]) 206 | 207 | AC_DEFUN([CC_ATTRIBUTE_SENTINEL], [ 208 | CC_CHECK_ATTRIBUTE( 209 | [sentinel], , 210 | [void some_function(void *foo, ...) __attribute__((sentinel));], 211 | [$1], [$2]) 212 | ]) 213 | 214 | AC_DEFUN([CC_ATTRIBUTE_DEPRECATED], [ 215 | CC_CHECK_ATTRIBUTE( 216 | [deprecated], , 217 | [void some_function(void *foo, ...) __attribute__((deprecated));], 218 | [$1], [$2]) 219 | ]) 220 | 221 | AC_DEFUN([CC_ATTRIBUTE_ALIAS], [ 222 | CC_CHECK_ATTRIBUTE( 223 | [alias], [weak, alias], 224 | [void other_function(void *foo) { } 225 | void some_function(void *foo) __attribute__((weak, alias("other_function")));], 226 | [$1], [$2]) 227 | ]) 228 | 229 | AC_DEFUN([CC_ATTRIBUTE_MALLOC], [ 230 | CC_CHECK_ATTRIBUTE( 231 | [malloc], , 232 | [void * __attribute__((malloc)) my_alloc(int n);], 233 | [$1], [$2]) 234 | ]) 235 | 236 | AC_DEFUN([CC_ATTRIBUTE_PACKED], [ 237 | CC_CHECK_ATTRIBUTE( 238 | [packed], , 239 | [struct astructure { char a; int b; long c; void *d; } __attribute__((packed));], 240 | [$1], [$2]) 241 | ]) 242 | 243 | AC_DEFUN([CC_ATTRIBUTE_CONST], [ 244 | CC_CHECK_ATTRIBUTE( 245 | [const], , 246 | [int __attribute__((const)) twopow(int n) { return 1 << n; } ], 247 | [$1], [$2]) 248 | ]) 249 | 250 | AC_DEFUN([CC_FLAG_VISIBILITY], [ 251 | AC_REQUIRE([CC_CHECK_WERROR]) 252 | AC_CACHE_CHECK([if $CC supports -fvisibility=hidden], 253 | [cc_cv_flag_visibility], 254 | [cc_flag_visibility_save_CFLAGS="$CFLAGS" 255 | CFLAGS="$CFLAGS $cc_cv_werror" 256 | CC_CHECK_CFLAGS_SILENT([-fvisibility=hidden], 257 | cc_cv_flag_visibility='yes', 258 | cc_cv_flag_visibility='no') 259 | CFLAGS="$cc_flag_visibility_save_CFLAGS"]) 260 | 261 | AS_IF([test "x$cc_cv_flag_visibility" = "xyes"], 262 | [AC_DEFINE([SUPPORT_FLAG_VISIBILITY], 1, 263 | [Define this if the compiler supports the -fvisibility flag]) 264 | $1], 265 | [$2]) 266 | ]) 267 | 268 | AC_DEFUN([CC_FUNC_EXPECT], [ 269 | AC_REQUIRE([CC_CHECK_WERROR]) 270 | AC_CACHE_CHECK([if compiler has __builtin_expect function], 271 | [cc_cv_func_expect], 272 | [ac_save_CFLAGS="$CFLAGS" 273 | CFLAGS="$CFLAGS $cc_cv_werror" 274 | AC_COMPILE_IFELSE([AC_LANG_SOURCE( 275 | [int some_function() { 276 | int a = 3; 277 | return (int)__builtin_expect(a, 3); 278 | }])], 279 | [cc_cv_func_expect=yes], 280 | [cc_cv_func_expect=no]) 281 | CFLAGS="$ac_save_CFLAGS" 282 | ]) 283 | 284 | AS_IF([test "x$cc_cv_func_expect" = "xyes"], 285 | [AC_DEFINE([SUPPORT__BUILTIN_EXPECT], 1, 286 | [Define this if the compiler supports __builtin_expect() function]) 287 | $1], 288 | [$2]) 289 | ]) 290 | 291 | AC_DEFUN([CC_ATTRIBUTE_ALIGNED], [ 292 | AC_REQUIRE([CC_CHECK_WERROR]) 293 | AC_CACHE_CHECK([highest __attribute__ ((aligned ())) supported], 294 | [cc_cv_attribute_aligned], 295 | [ac_save_CFLAGS="$CFLAGS" 296 | CFLAGS="$CFLAGS $cc_cv_werror" 297 | for cc_attribute_align_try in 64 32 16 8 4 2; do 298 | AC_COMPILE_IFELSE([AC_LANG_SOURCE([ 299 | int main() { 300 | static char c __attribute__ ((aligned($cc_attribute_align_try))) = 0; 301 | return c; 302 | }])], [cc_cv_attribute_aligned=$cc_attribute_align_try; break]) 303 | done 304 | CFLAGS="$ac_save_CFLAGS" 305 | ]) 306 | 307 | if test "x$cc_cv_attribute_aligned" != "x"; then 308 | AC_DEFINE_UNQUOTED([ATTRIBUTE_ALIGNED_MAX], [$cc_cv_attribute_aligned], 309 | [Define the highest alignment supported]) 310 | fi 311 | ]) 312 | -------------------------------------------------------------------------------- /src/GenIMG/genimage-src/test.config: -------------------------------------------------------------------------------- 1 | 2 | include("flash.conf") 3 | 4 | #----------------------------------- 5 | 6 | image data.tgz { 7 | tar {} 8 | mountpoint = "/data" 9 | } 10 | 11 | image nand-pcm038.img { 12 | flash { 13 | } 14 | flashtype = "nand-64M-512" 15 | partition barebox { 16 | image = "barebox-pcm038.bin" 17 | size = 512K 18 | } 19 | partition bareboxenv { 20 | image = "bareboxenv-pcm038.bin" 21 | size = 512K 22 | } 23 | partition kernel { 24 | image = "kernel-imx.bin" 25 | size = 4M 26 | } 27 | partition root { 28 | image = "root-nand.ubi" 29 | size = 20M 30 | } 31 | partition data { 32 | image = "data-nand.ubi" 33 | size = 0 34 | } 35 | } 36 | 37 | image root-nor-32M-64k.jffs2 { 38 | name = "root" 39 | flashtype = "nor-64M-128k" 40 | jffs2 {} 41 | size = 24M 42 | mountpoint = "/" 43 | } 44 | 45 | image data-nor-32M-64k.jffs2 { 46 | name = "data" 47 | flashtype = "nor-64M-128k" 48 | size = 0 49 | jffs2 { 50 | extraargs = "-l" 51 | } 52 | mountpoint = "/data" 53 | } 54 | 55 | image nand-pcm037.img { 56 | flash { 57 | } 58 | flashtype = "nand-64M-512" 59 | partition barebox { 60 | image = "barebox-pcm037.bin" 61 | size = 512K 62 | } 63 | partition bareboxenv { 64 | image = "bareboxenv-pcm037.bin" 65 | size = 512K 66 | } 67 | partition kernel { 68 | image = "kernel-imx.bin" 69 | size = 4M 70 | } 71 | partition root { 72 | image = "root-nand.ubi" 73 | size = 20M 74 | } 75 | partition data { 76 | image = "data-nand.ubi" 77 | size = 0 78 | } 79 | } 80 | 81 | image data-nand.ubi { 82 | ubi {} 83 | partition data { 84 | autoresize = true 85 | image = "data-nand.ubifs" 86 | } 87 | partition root { 88 | image = "data-nand.ubifs" 89 | } 90 | } 91 | 92 | image data-nand.ubifs { 93 | ubifs {} 94 | name = "data" 95 | size = 128M 96 | mountpoint = "/data" 97 | } 98 | 99 | image barebox-pcm038.bin { 100 | name = "barebox" 101 | file {} 102 | } 103 | 104 | image bareboxenv-pcm038.bin { 105 | name = "bareboxenv" 106 | file {} 107 | } 108 | 109 | image barebox-pcm037.bin { 110 | name = "barebox" 111 | file {} 112 | } 113 | 114 | image bareboxenv-pcm037.bin { 115 | name = "bareboxenv" 116 | file {} 117 | } 118 | 119 | image kernel-imx.bin { 120 | name = "kernel" 121 | file { 122 | name = "zImage-linux-2.6.39-imx" 123 | } 124 | } 125 | 126 | image root-nand.ubi { 127 | name = "root" 128 | ubi {} 129 | 130 | partition root { 131 | image = "root-nand.ubifs" 132 | } 133 | } 134 | 135 | image root-nand.ubifs { 136 | name = "root" 137 | size = 128M 138 | ubifs {} 139 | mountpoint = "/" 140 | } 141 | 142 | image hdimg.img { 143 | hdimage {} 144 | 145 | partition root { 146 | offset = 2M 147 | size = 128M 148 | partition-type = 0x78 149 | image = "root.ext2" 150 | } 151 | partition data { 152 | size = 20M 153 | partition-type = 0x1a 154 | image = "data.ext2" 155 | } 156 | 157 | size = 2G 158 | } 159 | 160 | image root.ext2 { 161 | ext2 {} 162 | size = 128M 163 | mountpoint = "/" 164 | } 165 | 166 | image data.ext2 { 167 | ext2 {} 168 | size = 20M 169 | mountpoint = "/data" 170 | } 171 | 172 | config { 173 | outputpath = images 174 | inputpath = input 175 | rootpath = root 176 | tmppath = tmp 177 | } 178 | 179 | -------------------------------------------------------------------------------- /src/OpenixCard/AW_IMG_PARA.h: -------------------------------------------------------------------------------- 1 | /* 2 | * AW_IMG_PARA.h 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #ifndef OPENIXCARD_AW_IMG_PARA_H 13 | #define OPENIXCARD_AW_IMG_PARA_H 14 | 15 | #include 16 | #include 17 | 18 | class AW_IMG_PARA { 19 | public: 20 | std::string image_name = {}; 21 | std::string partition_table_fex = "sys_partition.fex"; 22 | std::string partition_table_fex_path = "sys_partition.fex"; 23 | std::string partition_table_cfg = "sys_partition.cfg"; 24 | }; 25 | 26 | #endif //OPENIXCARD_AW_IMG_PARA_H 27 | -------------------------------------------------------------------------------- /src/OpenixCard/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file(GLOB libOpenixCardSource *.cpp) 2 | file(GLOB libOpenixCardPayloads payloads/*.cpp) 3 | 4 | add_library(libOpenixCard ${libOpenixCardSource} ${libOpenixCardPayloads}) 5 | target_link_libraries(libOpenixCard PRIVATE OpenixIMG inicpp GenIMG) -------------------------------------------------------------------------------- /src/OpenixCard/FEX2CFG.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * FEX2CFG.cpp 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | 19 | #include "FEX2CFG.h" 20 | #include "LOG.h" 21 | #include "exception.h" 22 | #include "payloads/chip.h" 23 | 24 | FEX2CFG::FEX2CFG(const std::string &dump_path) { 25 | // parse basic files 26 | awImgPara.partition_table_fex_path = dump_path + '/' + awImgPara.partition_table_fex; 27 | awImgPara.image_name = dump_path.substr(dump_path.find_last_of('/') + 1, dump_path.length() - dump_path.find_last_of('/') + 1); 28 | awImgPara.image_name = awImgPara.image_name.substr(0, awImgPara.image_name.find('.')); 29 | awImgPara.partition_table_fex = awImgPara.image_name.substr(0, awImgPara.image_name.rfind('.')) + ".fex"; 30 | awImgPara.partition_table_cfg = awImgPara.image_name.substr(0, awImgPara.image_name.rfind('.')) + ".cfg"; 31 | 32 | // Parse File 33 | open_file(awImgPara.partition_table_fex_path); 34 | classify_fex(); 35 | 36 | parse_fex(); 37 | gen_cfg(); 38 | } 39 | 40 | std::string FEX2CFG::save_file(const std::string &file_path) { 41 | auto path = file_path + "/" + awImgPara.partition_table_cfg; 42 | std::ofstream out(path); 43 | // File not open, throw error. 44 | if (!out.is_open()) { 45 | throw file_open_error(path); 46 | } 47 | out << awImgCfg; 48 | out.close(); 49 | return path; 50 | } 51 | 52 | [[maybe_unused]] void FEX2CFG::save_file() { 53 | std::ofstream out(awImgPara.image_name + ".cfg"); 54 | // File not open, throw error. 55 | if (!out.is_open()) { 56 | throw file_open_error(awImgPara.image_name + ".cfg"); 57 | } 58 | out << awImgCfg; 59 | out.close(); 60 | } 61 | 62 | void FEX2CFG::open_file(const std::string &file_path) { 63 | std::ifstream in; 64 | in.open(file_path, std::ios::in | std::ios::out | std::ios::binary); 65 | // File not open, throw error. 66 | if (!in.is_open()) { 67 | throw file_open_error(file_path); 68 | } 69 | awImgFex = std::string((std::istreambuf_iterator(in)), (std::istreambuf_iterator())); 70 | in.close(); 71 | } 72 | 73 | /* 74 | * The partition table of Allwinner's IMAGEWTY is very special. 75 | * It is parsed based on INI but its Section is repeated, which will cause the parsing to fail。 76 | * so use this function to organize 77 | */ 78 | 79 | void FEX2CFG::classify_fex() { 80 | int occ = 0; 81 | std::string::size_type pos = 0; 82 | std::istringstream _temp_aw_img_fex(awImgFex); 83 | std::string _temp = {}; 84 | std::string _temp_str = {}; 85 | 86 | // clean the comment message 87 | while(std::getline(_temp_aw_img_fex, _temp_str)){ 88 | if (_temp_str.substr(0, 1) != ";"){ 89 | _temp += _temp_str + "\n"; 90 | } 91 | } 92 | 93 | _temp = _temp.substr(_temp.find("[partition_start]")); 94 | 95 | while ((pos = _temp.find("[partition]", pos)) != std::string::npos) { 96 | ++occ; 97 | pos += std::string("[partition]").length(); 98 | } 99 | 100 | std::string _section, _less_out = _temp; 101 | for (int i = 0; i < occ; ++i) { 102 | _section = _less_out.substr(_less_out.rfind("[partition]") + std::string("[partition]").length()); 103 | _less_out = _less_out.substr(0, _less_out.rfind("[partition]")); 104 | awImgFexClassed.insert(0, "[partition" + std::to_string(occ - i) + "]" + _section); 105 | } 106 | } 107 | 108 | void FEX2CFG::parse_fex() { 109 | // Quick Fix for #26 110 | try { 111 | fex_classed = inicpp::parser::load(awImgFexClassed); 112 | } catch(const inicpp::ambiguity_exception &e) { 113 | LOG::ERROR(std::string("Partition table error, bad format. ") + std::string(e.what())); 114 | LOG::ERROR(std::string("Your Partition table: ")); 115 | std::cout << awImgFexClassed << std::endl; 116 | LOG::ERROR(std::string("Please fix in `sys_partition.fex` and re-pack with Allwinner BSP")); 117 | std::exit(-1); 118 | } 119 | } 120 | 121 | [[maybe_unused]] std::string FEX2CFG::get_image_name() const { 122 | return awImgPara.image_name; 123 | } 124 | 125 | [[maybe_unused]] std::string FEX2CFG::get_cfg() { 126 | return awImgCfg; 127 | } 128 | 129 | uint FEX2CFG::get_image_real_size(bool print) { 130 | get_partition_real_size(); 131 | uint total_size = 0; 132 | for (auto &size: partition_size_list) { 133 | total_size += size; 134 | } 135 | if (print) { 136 | LOG::DATA("Partition Table: "); 137 | print_partition_table(); 138 | } 139 | return total_size + linux_common_fex_compensate(); 140 | } 141 | 142 | void FEX2CFG::gen_cfg() { 143 | // Generate Prefix 144 | awImgCfg += "image "; 145 | awImgCfg += awImgPara.image_name; 146 | awImgCfg += ".img {\n"; 147 | 148 | // For Debug 149 | print_partition_table(); 150 | 151 | // Generate file from FEX 152 | awImgCfg += gen_linux_cfg_from_fex_map(fex_classed, type); 153 | 154 | awImgCfg += "}"; 155 | } 156 | 157 | [[maybe_unused]] void FEX2CFG::print_partition_table() { 158 | std::cout << cc::green; 159 | for (auto §: fex_classed) { 160 | std::cout << std::left << std::setw(13) << " Partition: '"; 161 | // Iterate through options in a section 162 | for (auto &opt: sect) { 163 | if (opt.get_name() == "name") { 164 | auto name = opt.get(); 165 | std::cout << std::left << std::setw(18) << name + "'"; 166 | if (name == "UDISK") { 167 | std::cout << "Remaining space."; 168 | } 169 | } else if (opt.get_name() == "size") { 170 | std::cout << std::left << std::setw(9) << static_cast(opt.get()) / 2 / 0x300 << "MB - " 171 | << std::left << std::setw(7) << opt.get() / 2 << "KB"; 172 | } 173 | } 174 | std::cout << std::endl; 175 | } 176 | std::cout << cc::reset; 177 | } 178 | 179 | void FEX2CFG::get_partition_real_size() { 180 | for (auto §: fex_classed) { 181 | for (auto &opt: sect) { 182 | if (opt.get_name() == "size") { 183 | partition_size_list.emplace_back(opt.get() / 2); 184 | } 185 | } 186 | } 187 | } 188 | 189 | void FEX2CFG::regenerate_cfg_file(partition_table_type _type) { 190 | awImgCfg = ""; 191 | this->type = _type; 192 | gen_cfg(); 193 | } 194 | 195 | -------------------------------------------------------------------------------- /src/OpenixCard/FEX2CFG.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FEX2CFG.h 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #ifndef OPENIXCARD_FEX2CFG_H 13 | #define OPENIXCARD_FEX2CFG_H 14 | 15 | #include 16 | 17 | #include 18 | 19 | #include "AW_IMG_PARA.h" 20 | #include "payloads/chip.h" 21 | 22 | class FEX2CFG { 23 | public: 24 | explicit FEX2CFG(const std::string &dump_path); 25 | 26 | // save the configuration to the dump file path 27 | std::string save_file(const std::string &file_path); 28 | 29 | //save the configuration to local path 30 | [[maybe_unused]] void save_file(); 31 | 32 | // load the configuration from the dump file 33 | [[maybe_unused]] std::string get_cfg(); 34 | 35 | // get image name 36 | [[maybe_unused]] [[nodiscard]] std::string get_image_name() const; 37 | 38 | // get image real size 39 | [[maybe_unused]] uint get_image_real_size(bool print); 40 | 41 | // Print out the partition table 42 | [[maybe_unused]] void print_partition_table(); 43 | 44 | // regenerate cfg file 45 | void regenerate_cfg_file(partition_table_type _type); 46 | 47 | private: 48 | AW_IMG_PARA awImgPara; 49 | inicpp::config fex_classed; 50 | std::vector partition_size_list; 51 | std::string awImgFex = {}; 52 | std::string awImgCfg = {}; 53 | std::string awImgFexClassed = {}; 54 | partition_table_type type = partition_table_type::gpt; 55 | 56 | void open_file(const std::string &file_path); 57 | 58 | void classify_fex(); 59 | 60 | void parse_fex(); 61 | 62 | void gen_cfg(); 63 | 64 | void get_partition_real_size(); 65 | }; 66 | 67 | 68 | #endif //OPENIXCARD_FEX2CFG_H 69 | -------------------------------------------------------------------------------- /src/OpenixCard/LOG.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * LOG.cpp 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #include 13 | 14 | #include "LOG.h" 15 | 16 | void LOG::DATA(const std::string &msg) { 17 | std::cout << cc::green << msg << cc::reset << std::endl; 18 | } 19 | 20 | void LOG::INFO(const std::string &msg) { 21 | std::cout << cc::cyan << "[OpenixCard INFO] " << msg << cc::reset << std::endl; 22 | } 23 | 24 | [[maybe_unused]] void LOG::DEBUG(const std::string &msg) { 25 | std::cout << cc::white << "[OpenixCard DEBUG] " << msg << cc::reset << std::endl; 26 | } 27 | 28 | void LOG::WARNING(const std::string &msg) { 29 | std::cout << cc::yellow << "[OpenixCard WARNING] " << msg << cc::reset << std::endl; 30 | } 31 | 32 | void LOG::ERROR(const std::string &msg) { 33 | std::cout << cc::red << "[OpenixCard ERROR] " << msg << cc::reset << std::endl; 34 | } 35 | -------------------------------------------------------------------------------- /src/OpenixCard/LOG.h: -------------------------------------------------------------------------------- 1 | /* 2 | * LOG.h 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #ifndef OPENIXCARD_LOG_H 13 | #define OPENIXCARD_LOG_H 14 | 15 | #include 16 | 17 | class LOG { 18 | public: 19 | [[maybe_unused]] static void DATA(const std::string &msg); 20 | 21 | [[maybe_unused]] static void INFO(const std::string &msg); 22 | 23 | [[maybe_unused]] static void DEBUG(const std::string &msg); 24 | 25 | [[maybe_unused]] static void WARNING(const std::string &msg); 26 | 27 | [[maybe_unused]] static void ERROR(const std::string &msg); 28 | }; 29 | 30 | 31 | #endif //OPENIXCARD_LOG_H 32 | -------------------------------------------------------------------------------- /src/OpenixCard/OpenixCard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * OpenixCard.cpp 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include "LOG.h" 17 | #include "exception.h" 18 | #include "config.h" 19 | #include "FEX2CFG.h" 20 | #include "GenIMG.h" 21 | 22 | extern "C" { 23 | #include "OpenixIMG.h" 24 | } 25 | 26 | #include "OpenixCard.h" 27 | 28 | OpenixCard::OpenixCard(int argc, char **argv) { 29 | argparse::ArgumentParser parser("OpenixCard", []() { 30 | if (std::string(PROJECT_GIT_HASH).empty()) 31 | return PROJECT_VER; 32 | else 33 | return PROJECT_GIT_HASH; 34 | }()); 35 | show_logo(); 36 | 37 | parser.add_argument("-u", "--unpack") 38 | .help("Unpack Allwinner Image to folder") 39 | .default_value(false) 40 | .implicit_value(true); 41 | parser.add_argument("-d", "--dump") 42 | .help("Convert Allwinner image to regular image") 43 | .default_value(false) 44 | .implicit_value(true); 45 | parser.add_argument("-c", "--cfg") 46 | .help("Get Allwinner image partition table cfg file (use together with unpack)") 47 | .default_value(false) 48 | .implicit_value(true); 49 | parser.add_argument("-p", "--pack") 50 | .help("pack dumped Allwinner image to regular image from folder (needs cfg file)") 51 | .default_value(false) 52 | .implicit_value(true); 53 | parser.add_argument("-s", "--size") 54 | .help("Get the accurate size of Allwinner image") 55 | .default_value(false) 56 | .implicit_value(true); 57 | parser.add_argument("input") 58 | .help("Input image file or directory path") 59 | .required() 60 | .remaining(); 61 | parser.add_epilog( 62 | "\r\neg.:\r\nOpenixCard -u - Unpack Allwinner image to target" 63 | "\r\nOpenixCard -uc - Unpack Allwinner image to target and generate Allwinner image partition table cfg" 64 | "\r\nOpenixCard -d - Convert Allwinner image to regular image" 65 | "\r\nOpenixCard -p - pack dumped Allwinner image to regular image from folder" 66 | "\r\nOpenixCard -s - Get the accurate size of Allwinner image)" 67 | "\r\n"); 68 | 69 | if (argc < 2) { 70 | std::cout << parser; // show help 71 | return; 72 | } 73 | 74 | try { 75 | // parser args 76 | parser.parse_args(argc, argv); 77 | } 78 | catch (const std::runtime_error &err) { 79 | std::cout << parser; // show help 80 | throw operator_error(err.what()); 81 | } 82 | 83 | try { 84 | input_file_vector = parser.get>("input"); 85 | } catch (const std::logic_error &err) { 86 | std::cout << parser; // show help 87 | throw no_file_provide_error(); 88 | } 89 | 90 | input_file = input_file_vector[0]; 91 | 92 | // if input file path is absolute path, convert to relative path, #1 93 | std::filesystem::path input_path(input_file); 94 | 95 | is_absolute = input_path.is_absolute(); 96 | temp_file_path = input_file + ".dump"; 97 | output_file_path = temp_file_path + ".out"; 98 | 99 | // Basic Operator 100 | mode = [&]() { 101 | if (parser.get("pack")) { 102 | return OpenixCardOperator::PACK; 103 | } else if (parser.get("unpack")) { 104 | if (parser.get("cfg")) { 105 | return OpenixCardOperator::UNPACKCFG; 106 | } 107 | return OpenixCardOperator::UNPACK; 108 | } else if (parser.get("dump")) { 109 | return OpenixCardOperator::DUMP; 110 | } else if (parser.get("size")) { 111 | return OpenixCardOperator::SIZE; 112 | } else { 113 | return OpenixCardOperator::NONE; 114 | } 115 | }(); 116 | 117 | if (mode == OpenixCardOperator::NONE) { 118 | std::cout << parser; 119 | // Break here. 120 | throw operator_missing_error(); 121 | } 122 | 123 | 124 | if (mode == OpenixCardOperator::DUMP) { 125 | unpack_target_image(); 126 | LOG::INFO("Convert Done! Parsing the partition tables..."); 127 | dump_and_clean(); 128 | } else if (mode == OpenixCardOperator::UNPACK || mode == OpenixCardOperator::UNPACKCFG) { 129 | unpack_target_image(); 130 | LOG::INFO("Unpack Done! Your image file is at " + temp_file_path); 131 | if (mode == OpenixCardOperator::UNPACKCFG) { 132 | save_cfg_file(); 133 | } 134 | } else if (mode == OpenixCardOperator::PACK) { 135 | pack(); 136 | } 137 | 138 | if (parser.get("size")) { 139 | get_real_size(); 140 | } 141 | } 142 | 143 | void OpenixCard::show_logo() { 144 | std::cout << cc::green << 145 | " _____ _ _____ _ \n" 146 | "| |___ ___ ___|_|_ _| |___ ___ _| |\n" 147 | "| | | . | -_| | |_'_| --| .'| _| . |\n" 148 | "|_____| _|___|_|_|_|_,_|_____|__,|_| |___|\n" 149 | " |_| Version: " << PROJECT_GIT_HASH << " Commit: " << PROJECT_VER 150 | << cc::magenta << 151 | "\nCopyright (c) 2022, YuzukiTsuru \n" 152 | << cc::reset << std::endl; 153 | } 154 | 155 | void OpenixCard::pack() { 156 | LOG::INFO("Generating target image..."); 157 | 158 | std::string target_cfg_path = {}; 159 | 160 | auto a = std::filesystem::directory_iterator(input_file); 161 | temp_file_path = input_file; // for potential size query 162 | 163 | for (const auto &entry: std::filesystem::directory_iterator(input_file)) { 164 | if (entry.path().extension() == ".cfg") { 165 | if (entry.path().filename() != "image.cfg") { 166 | target_cfg_path = entry.path().string(); 167 | } 168 | } 169 | } 170 | 171 | if (target_cfg_path.empty()) { 172 | LOG::ERROR("Can't find target image partition table cfg file in target folder"); 173 | return; 174 | } 175 | 176 | GenIMG gen_img(target_cfg_path, input_file, input_file); 177 | 178 | // check gen_img-src result 179 | if (gen_img.get_status() == -EINVAL) { 180 | LOG::ERROR("Generate image failed! Check your cfg file in: " + target_cfg_path); 181 | std::exit(1); 182 | } else if (gen_img.get_status() != 0) { 183 | LOG::ERROR("Generate image failed!"); 184 | std::exit(1); 185 | } 186 | 187 | LOG::INFO("Generate Done! Your image file is at " + input_file + " Cleaning up..."); 188 | } 189 | 190 | void OpenixCard::unpack_target_image() { 191 | // dump the packed image 192 | LOG::INFO("Converting input file: " + input_file); 193 | check_file(input_file); 194 | std::filesystem::create_directories(temp_file_path); 195 | crypto_init(); 196 | std::cout << cc::cyan; 197 | auto unpack_img_ret = unpack_image(input_file.c_str(), temp_file_path.c_str(), is_absolute); 198 | std::cout << cc::reset; 199 | 200 | switch (unpack_img_ret) { 201 | case 2: 202 | throw file_open_error(input_file); 203 | case 3: 204 | throw file_size_error(input_file); 205 | case 4: 206 | throw std::runtime_error("Unable to allocate memory for image: " + input_file); 207 | case 5: 208 | throw file_format_error(input_file); 209 | default: 210 | break; 211 | } 212 | } 213 | 214 | void OpenixCard::dump_and_clean() { 215 | FEX2CFG fex2Cfg(temp_file_path); 216 | auto target_cfg_path = fex2Cfg.save_file(temp_file_path); 217 | auto image_name = fex2Cfg.get_image_name(); 218 | // generate the image 219 | LOG::INFO("Parse Done! Generating target image..."); 220 | 221 | GenIMG genimage(target_cfg_path, temp_file_path, output_file_path); 222 | 223 | // check genimage-src result 224 | if (genimage.get_status() != 0) { 225 | LOG::ERROR("Generate image failed!"); 226 | std::exit(1); 227 | } 228 | 229 | LOG::INFO("Generate Done! Your image file is at " + output_file_path); 230 | LOG::INFO("Cleaning up..."); 231 | } 232 | 233 | void OpenixCard::save_cfg_file() { 234 | FEX2CFG fex2Cfg(temp_file_path); 235 | auto target_cfg_path = fex2Cfg.save_file(temp_file_path); 236 | LOG::INFO("Parse Done! Your cfg file is at " + target_cfg_path); 237 | } 238 | 239 | void OpenixCard::check_file(const std::string &file_path) { 240 | if (!std::filesystem::exists(file_path)) { 241 | throw file_open_error(file_path); 242 | } 243 | } 244 | 245 | void OpenixCard::get_real_size() { 246 | LOG::INFO("Getting accurate size of Allwinner img..."); 247 | FEX2CFG fex2Cfg(temp_file_path); 248 | auto real_size = fex2Cfg.get_image_real_size(true); 249 | LOG::DATA("The accurate size of image: " + std::to_string(real_size / 1024) + "MB, " + std::to_string(real_size) + "KB"); 250 | std::filesystem::remove_all(temp_file_path); 251 | } 252 | 253 | -------------------------------------------------------------------------------- /src/OpenixCard/OpenixCard.h: -------------------------------------------------------------------------------- 1 | /* 2 | * OpenixCard.h 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #ifndef OPENIXCARD_OPENIXCARD_H 13 | #define OPENIXCARD_OPENIXCARD_H 14 | 15 | #include 16 | #include 17 | 18 | class OpenixCard { 19 | public: 20 | OpenixCard(int argc, char **argv); 21 | 22 | private: 23 | std::vector input_file_vector; 24 | std::string input_file; 25 | std::string temp_file_path; 26 | std::string output_file_path; 27 | 28 | enum OpenixCardOperator { 29 | NONE, 30 | PACK, 31 | UNPACK, 32 | UNPACKCFG, 33 | DUMP, 34 | SIZE, 35 | }; 36 | 37 | OpenixCardOperator mode; 38 | bool is_absolute = false; 39 | 40 | private: 41 | static void show_logo(); 42 | 43 | static void check_file(const std::string& file_path); 44 | private: 45 | void pack(); 46 | 47 | void unpack_target_image(); 48 | 49 | void dump_and_clean(); 50 | 51 | void save_cfg_file(); 52 | 53 | void get_real_size(); 54 | }; 55 | 56 | 57 | #endif //OPENIXCARD_OPENIXCARD_H 58 | -------------------------------------------------------------------------------- /src/OpenixCard/exception.h: -------------------------------------------------------------------------------- 1 | /* 2 | * exception.h 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #ifndef OPENIXCARD_EXCEPTION_H 13 | #define OPENIXCARD_EXCEPTION_H 14 | 15 | #include 16 | #include 17 | 18 | class file_open_error : public std::runtime_error { 19 | public: 20 | explicit file_open_error(const std::string &what) : std::runtime_error("Fail to open file: " + what + ".") {}; 21 | }; 22 | 23 | class file_format_error : public std::runtime_error { 24 | public: 25 | explicit file_format_error(const std::string &what) : std::runtime_error("File: " + what + " is not Allwinner image.") {}; 26 | }; 27 | 28 | class file_size_error : public std::runtime_error { 29 | public: 30 | explicit file_size_error(const std::string &what) : std::runtime_error("Invalid file size: " + what + ".") {}; 31 | }; 32 | 33 | class no_file_provide_error : public std::runtime_error { 34 | public: 35 | no_file_provide_error() : std::runtime_error("No file Provide.") {}; 36 | }; 37 | 38 | class operator_error : public std::runtime_error { 39 | public: 40 | explicit operator_error(const std::string &what) : std::runtime_error("Operate ERROR: " + what + ".") {}; 41 | }; 42 | 43 | class operator_missing_error : public std::runtime_error { 44 | public: 45 | explicit operator_missing_error() : std::runtime_error("Operate ERROR, You must specify a Operator.") {}; 46 | }; 47 | 48 | #endif //OPENIXCARD_EXCEPTION_H 49 | -------------------------------------------------------------------------------- /src/OpenixCard/payloads/android.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gloom on 2022/7/4. 3 | // 4 | -------------------------------------------------------------------------------- /src/OpenixCard/payloads/chip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * chip.h 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #ifndef OPENIXCARD_CHIP_H 13 | #define OPENIXCARD_CHIP_H 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | typedef struct partition_table_struct { 20 | std::string name; 21 | uint64_t size; 22 | std::string downloadfile; 23 | uint64_t user_type; 24 | } partition_table_struct; 25 | 26 | typedef struct linux_compensate { 27 | uint64_t gpt_location = 0x100000; 28 | uint64_t boot0_offset = 0x2000; 29 | uint64_t boot_packages_offset = 0x1004000; 30 | } linux_compensate; 31 | 32 | enum partition_table_type { 33 | hybrid, 34 | gpt, 35 | mbr 36 | }; 37 | 38 | std::string gen_linux_cfg_from_fex_map(const inicpp::config &fex, partition_table_type type); 39 | 40 | [[maybe_unused]] uint linux_common_fex_compensate(); 41 | 42 | #endif //OPENIXCARD_CHIP_H 43 | -------------------------------------------------------------------------------- /src/OpenixCard/payloads/linux.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * linux.cpp 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | #ifndef OPENIXCARD_LINUX_H 13 | #define OPENIXCARD_LINUX_H 14 | 15 | #include 16 | 17 | std::string gen_linux_cfg_from_fex_map(const inicpp::config &fex, partition_table_type type) { 18 | partition_table_struct patab; 19 | linux_compensate compensate; 20 | std::string cfg_data; 21 | 22 | // check type 23 | for (auto §: fex) { 24 | patab = {}; // reflash struce 25 | for (auto &opt: sect) { 26 | if (opt.get_name() == "name") { 27 | patab.name = opt.get(); 28 | } else if (opt.get_name() == "size") { 29 | patab.size = opt.get(); 30 | } else if (opt.get_name() == "downloadfile") { 31 | patab.downloadfile = opt.get(); 32 | } else if (opt.get_name() == "user_type") { 33 | patab.user_type = opt.get(); 34 | } else { 35 | // Droped. 36 | } 37 | } 38 | if (patab.name == "boot-resource") { 39 | type = partition_table_type::hybrid; 40 | } else if (patab.downloadfile == "\"boot-resource.fex\"") { 41 | type = partition_table_type::hybrid; 42 | } 43 | } 44 | 45 | cfg_data += "\thdimage{\n"; 46 | 47 | switch(type){ 48 | case partition_table_type::hybrid: 49 | cfg_data += "\t\tpartition-table-type = \"hybrid\"\n"; 50 | break; 51 | case partition_table_type::gpt: 52 | cfg_data += "\t\tpartition-table-type = \"gpt\"\n"; 53 | break; 54 | case partition_table_type::mbr: 55 | cfg_data += "\t\tpartition-table-type = \"mbr\"\n"; 56 | break; 57 | default: 58 | cfg_data += "\t\tpartition-table-type = \"gpt\"\n"; 59 | break; 60 | } 61 | 62 | cfg_data += "\t\tgpt-location = " + std::to_string(compensate.gpt_location / 0x100000) + "M\n"; 63 | cfg_data += "\t}\n"; 64 | 65 | // add sdcard boot image 66 | cfg_data += "\tpartition boot0 {\n" 67 | "\t\tin-partition-table = \"no\"\n" 68 | "\t\timage = \"boot0_sdcard.fex\"\n" 69 | "\t\toffset = " + std::to_string(compensate.boot0_offset / 0x400) + "K\n" + 70 | "\t}\n"; 71 | 72 | cfg_data += "\tpartition boot-packages {\n" 73 | "\t\tin-partition-table = \"no\"\n" 74 | "\t\timage = \"boot_package.fex\"\n" 75 | "\t\toffset = " + std::to_string(compensate.boot_packages_offset / 0x400) + "K\n" + 76 | "\t}\n"; 77 | 78 | for (auto §: fex) { 79 | patab = {}; // reflash struce 80 | for (auto &opt: sect) { 81 | if (opt.get_name() == "name") { 82 | patab.name = opt.get(); 83 | } else if (opt.get_name() == "size") { 84 | patab.size = opt.get(); 85 | } else if (opt.get_name() == "downloadfile") { 86 | patab.downloadfile = opt.get(); 87 | } else if (opt.get_name() == "user_type") { 88 | patab.user_type = opt.get(); 89 | } else { 90 | // Droped. 91 | } 92 | } 93 | if (patab.name != "UDISK") { 94 | cfg_data += "\tpartition " + patab.name + " {\n"; 95 | if (patab.name == "boot-resource") { 96 | cfg_data += "\t\tpartition-type = 0xC\n"; 97 | } else if (patab.downloadfile == "\"boot-resource.fex\"") { 98 | cfg_data += "\t\tpartition-type = 0xC\n"; 99 | } 100 | if (patab.downloadfile.empty()) 101 | cfg_data += "\t\timage = \"blank.fex\"\n"; 102 | else 103 | cfg_data += "\t\timage = " + patab.downloadfile + "\n"; 104 | cfg_data += "\t\tsize = " + std::to_string(patab.size / 2) + "K\n"; 105 | cfg_data += "\t}\n"; 106 | } 107 | } 108 | return cfg_data; 109 | } 110 | 111 | uint linux_common_fex_compensate() { 112 | linux_compensate compensate; 113 | return compensate.gpt_location / 0x400 + compensate.boot0_offset / 0x400 + compensate.boot_packages_offset / 0x400; 114 | } 115 | 116 | #endif //OPENIXCARD_LINUX_H 117 | -------------------------------------------------------------------------------- /src/OpenixIMG/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | 3 | project(OpenixIMG) 4 | 5 | include_directories( 6 | include 7 | lib/twofish/src 8 | lib/rc6/src 9 | ) 10 | 11 | add_subdirectory(lib/twofish) 12 | add_subdirectory(lib/rc6) 13 | 14 | add_library(OpenixIMG src/OpenixIMG.c ../GenIMG/GenimageWrapper.c) 15 | target_link_libraries(OpenixIMG twofish rc6) 16 | 17 | option(BUILD_T_OpenixIMG "Set to ON to build OpenixIMG Test" OFF) 18 | 19 | if(BUILD_T_OpenixIMG) 20 | 21 | add_executable(T_OpenixIMG test/T_OpenixIMG.c) 22 | target_link_libraries(T_OpenixIMG OpenixIMG) 23 | 24 | endif() 25 | -------------------------------------------------------------------------------- /src/OpenixIMG/include/IMAGEWTY.h: -------------------------------------------------------------------------------- 1 | /* 2 | * IMAGEWTY.h Allwinner IMAGEWTY Handler 3 | * Copyright (c) 2012, Ithamar R. Adema 4 | * Copyright (c) 2022, YuzukiTsuru 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License version 2 as 8 | * published by the Free Software Foundation. 9 | * 10 | * See README and LICENSE for more details. 11 | */ 12 | 13 | #ifndef OPENIXIMG_IMAGEWTY_H 14 | #define OPENIXIMG_IMAGEWTY_H 15 | 16 | #include 17 | 18 | #define IMAGEWTY_MAGIC "IMAGEWTY" 19 | #define IMAGEWTY_MAGIC_LEN 8 20 | 21 | struct imagewty_header { 22 | char magic[IMAGEWTY_MAGIC_LEN]; 23 | uint32_t header_version; /* Image header version (seen values are 0x0100 / 0x0300) */ 24 | uint32_t header_size; /* Image header size (sizeof(struct imagewty_header)) */ 25 | uint32_t ram_base; 26 | uint32_t version; /* format version (IMAGEWTY_VERSION) */ 27 | uint32_t image_size; /* total size of image file (rounded up to 256 bytes?) */ 28 | uint32_t image_header_size; /* image header size (including padding) */ 29 | union { 30 | struct { 31 | uint32_t pid; /* USB peripheral ID (from image.cfg) */ 32 | uint32_t vid; /* USB vendor ID (from image.cfg) */ 33 | uint32_t hardware_id; /* Hardware ID (from image.cfg) */ 34 | uint32_t firmware_id; /* Firmware ID (from image.cfg) */ 35 | uint32_t val1; /* */ 36 | uint32_t val1024; /* */ 37 | uint32_t num_files; /* Total number of files embedded */ 38 | uint32_t val1024_2; /* */ 39 | uint32_t val0; /* */ 40 | uint32_t val0_2; /* */ 41 | uint32_t val0_3; /* */ 42 | uint32_t val0_4; /* */ 43 | /* 0x0050 */ 44 | } v1; 45 | struct { 46 | uint32_t unknown; 47 | uint32_t pid; /* USB peripheral ID (from image.cfg) */ 48 | uint32_t vid; /* USB vendor ID (from image.cfg) */ 49 | uint32_t hardware_id; /* Hardware ID (from image.cfg) */ 50 | uint32_t firmware_id; /* Firmware ID (from image.cfg) */ 51 | uint32_t val1; /* */ 52 | uint32_t val1024; /* */ 53 | uint32_t num_files; /* Total number of files embedded */ 54 | uint32_t val1024_2; /* */ 55 | uint32_t val0; /* */ 56 | uint32_t val0_2; /* */ 57 | uint32_t val0_3; /* */ 58 | uint32_t val0_4; /* */ 59 | /* 0x0060 */ 60 | } v3; 61 | }; 62 | }; 63 | 64 | #define IMAGEWTY_FHDR_MAINTYPE_LEN 8 65 | #define IMAGEWTY_FHDR_SUBTYPE_LEN 16 66 | #define IMAGEWTY_FHDR_FILENAME_LEN 256 67 | 68 | struct imagewty_file_header { 69 | uint32_t filename_len; 70 | uint32_t total_header_size; 71 | const char maintype[IMAGEWTY_FHDR_MAINTYPE_LEN]; 72 | const char subtype[IMAGEWTY_FHDR_SUBTYPE_LEN]; 73 | union { 74 | struct { 75 | uint32_t unknown_3; 76 | uint32_t stored_length; 77 | uint32_t original_length; 78 | uint32_t offset; 79 | uint32_t unknown; 80 | const char filename[IMAGEWTY_FHDR_FILENAME_LEN]; 81 | } v1; 82 | struct { 83 | uint32_t unknown_0; 84 | const char filename[IMAGEWTY_FHDR_FILENAME_LEN]; 85 | uint32_t stored_length; 86 | uint32_t pad1; 87 | uint32_t original_length; 88 | uint32_t pad2; 89 | uint32_t offset; 90 | } v3; 91 | }; 92 | }; 93 | 94 | #endif /* IMAGEWTY_H */ 95 | 96 | -------------------------------------------------------------------------------- /src/OpenixIMG/include/OpenixIMG.h: -------------------------------------------------------------------------------- 1 | /* 2 | * OpenixIMG.h Tool for unpack Allwinner IMAGEWTY image 3 | * Copyright (c) 2012, Ithamar R. Adema 4 | * Copyright (c) 2022, YuzukiTsuru 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License version 2 as 8 | * published by the Free Software Foundation. 9 | * 10 | * See README and LICENSE for more details. 11 | */ 12 | 13 | #ifndef OPENIXIMG_OPENIXIMG_H 14 | #define OPENIXIMG_OPENIXIMG_H 15 | 16 | #include 17 | 18 | #include "twofish.h" 19 | #include "rc6.h" 20 | 21 | #include "IMAGEWTY.h" 22 | 23 | #ifdef WIN32 24 | #define MKDIR(p) mkdir(p) 25 | #else 26 | #define MKDIR(p) mkdir(p,S_IRWXU) 27 | #endif 28 | 29 | void recursive_mkdir(const char *dir); 30 | 31 | void crypto_init(void); 32 | 33 | void *rc6_decrypt_inplace(void *p, size_t len, rc6_ctx_t *ctx); 34 | 35 | FILE *dir_fopen(const char *dir, const char *path, const char *mode, int is_absolute); 36 | 37 | int unpack_image(const char *infn, const char *outdn, int is_absolute); 38 | 39 | #endif //OPENIXIMG_OPENIXIMG_H 40 | -------------------------------------------------------------------------------- /src/OpenixIMG/lib/rc6/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | 3 | project(rc6) 4 | 5 | include_directories(src) 6 | 7 | add_library(rc6 src/rc6.c) -------------------------------------------------------------------------------- /src/OpenixIMG/lib/rc6/src/rc6.c: -------------------------------------------------------------------------------- 1 | /* rc6.c */ 2 | /* 3 | This file is part of the AVR-Crypto-Lib. 4 | Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de) 5 | 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | /* 20 | * File: rc6.c 21 | * Author: Daniel Otte 22 | * Date: 06.08.2006 23 | * License: GPL 24 | * Description: Implementation of the RC6 cipher algorithm. 25 | * This implementation is restricted to 32-bit words and to keys up to 65535 bit in length (but this is 26 | * quite easy to expand), but free in the choice of number of rounds (0 to 125). 27 | * so it is RC6-32/r/b 28 | * THIS ONLY WORKS FOR LITTEL ENDIAN!!! 29 | */ 30 | 31 | #include 32 | #include 33 | #include "rc6.h" 34 | //#include "config.h" 35 | 36 | #define P32 0xB7E15163 /* e -2 */ 37 | #define Q32 0x9E3779B9 /* Golden Ratio -1 */ 38 | 39 | uint32_t rotl32(uint32_t a, uint8_t n) { 40 | n &= 0x1f; /* higher rotates would not bring anything */ 41 | return ((a << n) | (a >> (32 - n))); 42 | } 43 | 44 | uint32_t rotr32(uint32_t a, uint8_t n) { 45 | n &= 0x1f; /* higher rotates would not bring anything */ 46 | return ((a >> n) | (a << (32 - n))); 47 | } 48 | 49 | uint8_t rc6_init(void *key, uint16_t keylength_b, rc6_ctx_t *s) { 50 | return rc6_initl(key, keylength_b, 20, s); 51 | } 52 | 53 | 54 | uint8_t rc6_initl(void *key, uint16_t keylength_b, uint8_t rounds, rc6_ctx_t *s) { 55 | uint8_t i, j; 56 | uint16_t v, p, c; 57 | uint32_t a, b, l = 0; 58 | if (rounds > 125) 59 | return 2; 60 | if (!(s->S = malloc((2 * rounds + 4) * sizeof(uint32_t)))) 61 | return 1; 62 | 63 | s->rounds = rounds; 64 | 65 | c = keylength_b / 32; 66 | if (keylength_b % 32) { 67 | ++c; 68 | j = (keylength_b % 32) / 8; 69 | if (keylength_b % 8) 70 | ++j; 71 | for (i = 0; i < j; ++i) 72 | ((uint8_t * ) & l)[i] = ((uint8_t *) key)[(c - 1) * 4 + i]; 73 | } else { 74 | l = ((uint32_t *) key)[c - 1]; 75 | } 76 | 77 | s->S[0] = P32; 78 | for (i = 1; i < 2 * rounds + 4; ++i) { 79 | s->S[i] = s->S[i - 1] + Q32; 80 | } 81 | 82 | a = b = j = i = 0; 83 | v = 3 * ((c > 2 * rounds + 4) ? c : (2 * rounds + 4)); 84 | for (p = 1; p <= v; ++p) { 85 | a = s->S[i] = rotl32(s->S[i] + a + b, 3); 86 | if (j == c - 1) { 87 | b = l = rotl32(l + a + b, a + b); 88 | } else { 89 | b = ((uint32_t *) key)[j] = rotl32(((uint32_t *) key)[j] + a + b, a + b); 90 | } 91 | i = (i + 1) % (2 * rounds + 4); 92 | j = (j + 1) % c; 93 | } 94 | return 0; 95 | } 96 | 97 | void rc6_free(rc6_ctx_t *s) { 98 | free(s->S); 99 | } 100 | 101 | #define LG_W 5 102 | #define A (((uint32_t*)block)[0]) 103 | #define B (((uint32_t*)block)[1]) 104 | #define C (((uint32_t*)block)[2]) 105 | #define D (((uint32_t*)block)[3]) 106 | 107 | void rc6_enc(void *block, rc6_ctx_t *s) { 108 | uint8_t i; 109 | uint32_t t, u, x; /* greetings to Linux? */ 110 | B += s->S[0]; 111 | D += s->S[1]; 112 | for (i = 1; i <= s->rounds; ++i) { 113 | t = rotl32(B * (2 * B + 1), LG_W); 114 | u = rotl32(D * (2 * D + 1), LG_W); 115 | A = rotl32((A ^ t), u) + s->S[2 * i]; 116 | C = rotl32((C ^ u), t) + s->S[2 * i + 1]; 117 | x = A; 118 | A = B; 119 | B = C; 120 | C = D; 121 | D = x; 122 | } 123 | A += s->S[2 * s->rounds + 2]; 124 | C += s->S[2 * s->rounds + 3]; 125 | } 126 | 127 | void rc6_dec(void *block, rc6_ctx_t *s) { 128 | uint8_t i; 129 | uint32_t t, u, x; /* greetings to Linux? */ 130 | 131 | C -= s->S[2 * s->rounds + 3]; 132 | A -= s->S[2 * s->rounds + 2]; 133 | 134 | for (i = s->rounds; i > 0; --i) { 135 | x = D; 136 | D = C; 137 | C = B; 138 | B = A; 139 | A = x; 140 | u = rotl32(D * (2 * D + 1), LG_W); 141 | t = rotl32(B * (2 * B + 1), LG_W); 142 | C = rotr32(C - s->S[2 * i + 1], t) ^ u; 143 | A = rotr32(A - s->S[2 * i + 0], u) ^ t; 144 | } 145 | D -= s->S[1]; 146 | B -= s->S[0]; 147 | } 148 | 149 | -------------------------------------------------------------------------------- /src/OpenixIMG/lib/rc6/src/rc6.h: -------------------------------------------------------------------------------- 1 | /* rc6.h */ 2 | /* 3 | This file is part of the AVR-Crypto-Lib. 4 | Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de) 5 | 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | /* 20 | * File: rc6.h 21 | * Author: Daniel Otte 22 | * Date: 06.08.2006 23 | * License: GPL 24 | * Description: Implementation of the RC6 cipher algorithm. 25 | * This implementation is restricted to 32-bit words, but free in the choice of number of rounds (0 to 255). 26 | * so it is RC6-32/r/b 27 | */ 28 | 29 | #ifndef RC6_H_ 30 | #define RC6_H_ 31 | 32 | 33 | #include 34 | 35 | typedef struct rc6_ctx_st { 36 | uint8_t rounds; /* specifys the number of rounds; default: 20 */ 37 | uint32_t *S; /* the round-keys */ 38 | } rc6_ctx_t; 39 | 40 | 41 | uint8_t rc6_init(void *key, uint16_t keylength_b, rc6_ctx_t *s); 42 | 43 | uint8_t rc6_initl(void *key, uint16_t keylength_b, uint8_t rounds, rc6_ctx_t *s); 44 | 45 | void rc6_enc(void *block, rc6_ctx_t *s); 46 | 47 | void rc6_dec(void *block, rc6_ctx_t *s); 48 | 49 | void rc6_free(rc6_ctx_t *s); 50 | 51 | #endif /* RC6_H_ */ 52 | 53 | -------------------------------------------------------------------------------- /src/OpenixIMG/lib/twofish/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | 3 | project(twofish) 4 | 5 | include_directories(src) 6 | 7 | add_library(twofish src/twofish.c) -------------------------------------------------------------------------------- /src/OpenixIMG/lib/twofish/src/std_defs.h: -------------------------------------------------------------------------------- 1 | /* 1. Standard types for AES cryptography source code */ 2 | 3 | typedef unsigned char u1byte; /* an 8 bit unsigned character type */ 4 | typedef unsigned short u2byte; /* a 16 bit unsigned integer type */ 5 | 6 | typedef signed char s1byte; /* an 8 bit signed character type */ 7 | typedef signed short s2byte; /* a 16 bit signed integer type */ 8 | typedef signed long s4byte; /* a 32 bit signed integer type */ 9 | 10 | /* 3. Basic macros for speeding up generic operations */ 11 | 12 | /* Circular rotate of 32 bit values */ 13 | 14 | #ifndef _MSC_VER 15 | 16 | #define rotr(x, n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n)))) 17 | #define rotl(x, n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n)))) 18 | 19 | #else 20 | 21 | #include 22 | 23 | #pragma intrinsic(_lrotr,_lrotl) 24 | #define rotr(x,n) _lrotr(x,n) 25 | #define rotl(x,n) _lrotl(x,n) 26 | 27 | #endif 28 | 29 | /* Invert byte order in a 32 bit variable */ 30 | 31 | #define bswap(x) (rotl(x, 8) & 0x00ff00ff | rotr(x, 8) & 0xff00ff00) 32 | 33 | /* Extract byte from a 32 bit quantity (little endian notation) */ 34 | 35 | #define byte(x, n) ((u1byte)((x) >> (8 * n))) 36 | 37 | /* For inverting byte order in input/output 32 bit words if needed */ 38 | 39 | #ifdef BYTE_SWAP 40 | #define io_swap(x) bswap(x) 41 | #else 42 | #define io_swap(x) (x) 43 | #endif 44 | 45 | /* For inverting the byte order of input/output blocks if needed */ 46 | 47 | #ifdef WORD_SWAP 48 | 49 | #define get_block(x) \ 50 | ((u4byte*)(x))[0] = io_swap(in_blk[3]); \ 51 | ((u4byte*)(x))[1] = io_swap(in_blk[2]); \ 52 | ((u4byte*)(x))[2] = io_swap(in_blk[1]); \ 53 | ((u4byte*)(x))[3] = io_swap(in_blk[0]) 54 | 55 | #define put_block(x) \ 56 | out_blk[3] = io_swap(((u4byte*)(x))[0]); \ 57 | out_blk[2] = io_swap(((u4byte*)(x))[1]); \ 58 | out_blk[1] = io_swap(((u4byte*)(x))[2]); \ 59 | out_blk[0] = io_swap(((u4byte*)(x))[3]) 60 | 61 | #define get_key(x,len) \ 62 | ((u4byte*)(x))[4] = ((u4byte*)(x))[5] = \ 63 | ((u4byte*)(x))[6] = ((u4byte*)(x))[7] = 0; \ 64 | switch((((len) + 63) / 64)) { \ 65 | case 2: \ 66 | ((u4byte*)(x))[0] = io_swap(in_key[3]); \ 67 | ((u4byte*)(x))[1] = io_swap(in_key[2]); \ 68 | ((u4byte*)(x))[2] = io_swap(in_key[1]); \ 69 | ((u4byte*)(x))[3] = io_swap(in_key[0]); \ 70 | break; \ 71 | case 3: \ 72 | ((u4byte*)(x))[0] = io_swap(in_key[5]); \ 73 | ((u4byte*)(x))[1] = io_swap(in_key[4]); \ 74 | ((u4byte*)(x))[2] = io_swap(in_key[3]); \ 75 | ((u4byte*)(x))[3] = io_swap(in_key[2]); \ 76 | ((u4byte*)(x))[4] = io_swap(in_key[1]); \ 77 | ((u4byte*)(x))[5] = io_swap(in_key[0]); \ 78 | break; \ 79 | case 4: \ 80 | ((u4byte*)(x))[0] = io_swap(in_key[7]); \ 81 | ((u4byte*)(x))[1] = io_swap(in_key[6]); \ 82 | ((u4byte*)(x))[2] = io_swap(in_key[5]); \ 83 | ((u4byte*)(x))[3] = io_swap(in_key[4]); \ 84 | ((u4byte*)(x))[4] = io_swap(in_key[3]); \ 85 | ((u4byte*)(x))[5] = io_swap(in_key[2]); \ 86 | ((u4byte*)(x))[6] = io_swap(in_key[1]); \ 87 | ((u4byte*)(x))[7] = io_swap(in_key[0]); \ 88 | } 89 | 90 | #else 91 | 92 | #define get_block(x) \ 93 | ((u4byte*)(x))[0] = io_swap(in_blk[0]); \ 94 | ((u4byte*)(x))[1] = io_swap(in_blk[1]); \ 95 | ((u4byte*)(x))[2] = io_swap(in_blk[2]); \ 96 | ((u4byte*)(x))[3] = io_swap(in_blk[3]) 97 | 98 | #define put_block(x) \ 99 | out_blk[0] = io_swap(((u4byte*)(x))[0]); \ 100 | out_blk[1] = io_swap(((u4byte*)(x))[1]); \ 101 | out_blk[2] = io_swap(((u4byte*)(x))[2]); \ 102 | out_blk[3] = io_swap(((u4byte*)(x))[3]) 103 | 104 | #define get_key(x, len) \ 105 | ((u4byte*)(x))[4] = ((u4byte*)(x))[5] = \ 106 | ((u4byte*)(x))[6] = ((u4byte*)(x))[7] = 0; \ 107 | switch((((len) + 63) / 64)) { \ 108 | case 4: \ 109 | ((u4byte*)(x))[6] = io_swap(in_key[6]); \ 110 | ((u4byte*)(x))[7] = io_swap(in_key[7]); \ 111 | case 3: \ 112 | ((u4byte*)(x))[4] = io_swap(in_key[4]); \ 113 | ((u4byte*)(x))[5] = io_swap(in_key[5]); \ 114 | case 2: \ 115 | ((u4byte*)(x))[0] = io_swap(in_key[0]); \ 116 | ((u4byte*)(x))[1] = io_swap(in_key[1]); \ 117 | ((u4byte*)(x))[2] = io_swap(in_key[2]); \ 118 | ((u4byte*)(x))[3] = io_swap(in_key[3]); \ 119 | } 120 | 121 | #endif 122 | 123 | #ifdef BLOCK_SWAP 124 | #define BYTE_SWAP 125 | #define WORD_SWAP 126 | #endif 127 | -------------------------------------------------------------------------------- /src/OpenixIMG/lib/twofish/src/twofish.h: -------------------------------------------------------------------------------- 1 | #ifndef TWOFISH_H 2 | #define TWOFISH_H 3 | 4 | typedef unsigned long u4byte; /* a 32 bit unsigned integer type */ 5 | 6 | void tf_encrypt(const u4byte in_blk[4], u4byte out_blk[]); 7 | 8 | void tf_decrypt(const u4byte in_blk[4], u4byte out_blk[4]); 9 | 10 | u4byte *tf_init(const u4byte in_key[], u4byte key_len); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/OpenixIMG/src/OpenixIMG.c: -------------------------------------------------------------------------------- 1 | /* 2 | * OpenixIMG.c Tool for unpack Allwinner IMAGEWTY image 3 | * Copyright (c) 2012, Ithamar R. Adema 4 | * Copyright (c) 2022, YuzukiTsuru 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License version 2 as 8 | * published by the Free Software Foundation. 9 | * 10 | * See README and LICENSE for more details. 11 | */ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "OpenixIMG.h" 20 | #include "IMAGEWTY.h" 21 | 22 | int flag_encryption_enabled; 23 | 24 | #define OpenixIMG_LOG(fmt, arg...) \ 25 | do { \ 26 | printf(fmt, ##arg); \ 27 | } while (0) 28 | 29 | #define O_LOG(fmt, arg...) OpenixIMG_LOG("[OpenixIMG INFO] " fmt, ##arg) 30 | 31 | /* Crypto */ 32 | rc6_ctx_t header_ctx; 33 | rc6_ctx_t fileheaders_ctx; 34 | rc6_ctx_t filecontent_ctx; 35 | u4byte tf_key[32]; 36 | 37 | const char *progname; 38 | 39 | void recursive_mkdir(const char *dir) { 40 | char tmp[256]; 41 | char *p = NULL; 42 | size_t len; 43 | 44 | snprintf(tmp, sizeof(tmp), "%s", dir); 45 | len = strlen(tmp); 46 | 47 | if (tmp[len - 1] == '/') 48 | tmp[len - 1] = 0; 49 | 50 | for (p = tmp + 1; *p; p++) { 51 | if (*p == '/') { 52 | *p = 0; 53 | MKDIR(tmp); 54 | *p = '/'; 55 | } 56 | } 57 | 58 | MKDIR(tmp); 59 | } 60 | 61 | void crypto_init(void) { 62 | char key[32]; 63 | int i; 64 | 65 | /* Initialize RC6 context for header */ 66 | memset(key, 0, sizeof(key)); 67 | key[sizeof(key) - 1] = 'i'; 68 | rc6_init(key, sizeof(key) * 8, &header_ctx); 69 | 70 | /* Initialize RC6 context for fileheaders */ 71 | memset(key, 1, sizeof(key)); 72 | key[sizeof(key) - 1] = 'm'; 73 | rc6_init(key, sizeof(key) * 8, &fileheaders_ctx); 74 | 75 | /* Initialize RC6 context for file content */ 76 | memset(key, 2, sizeof(key)); 77 | key[sizeof(key) - 1] = 'g'; 78 | rc6_init(key, sizeof(key) * 8, &filecontent_ctx); 79 | 80 | /* Initialize TwoFish key for file content of non-fex files */ 81 | tf_key[0] = 5; 82 | tf_key[1] = 4; 83 | for (i = 2; i < 32; i++) 84 | tf_key[i] = tf_key[i - 2] + tf_key[i - 1]; 85 | } 86 | 87 | void *rc6_decrypt_inplace(void *p, size_t len, rc6_ctx_t *ctx) { 88 | size_t i; 89 | 90 | /* If encryption is disabled, we've got nothing to do */ 91 | if (!flag_encryption_enabled) 92 | return p + len; 93 | 94 | for (i = 0; i < len / 16; i++) { 95 | rc6_dec(p, ctx); 96 | p += 16; 97 | } 98 | 99 | return p; 100 | } 101 | 102 | FILE *dir_fopen(const char *dir, const char *path, const char *mode, int is_absolute) { 103 | char outfn[512]; 104 | char *p; 105 | int len; 106 | 107 | if (is_absolute) { 108 | strcpy(outfn, "/"); 109 | } else { 110 | strcpy(outfn, "./"); 111 | } 112 | strcat(outfn, dir); 113 | len = (int) strlen(outfn); 114 | if (outfn[len - 1] != '/' && path[0] != '/') 115 | strcat(outfn, "/"); 116 | strcat(outfn, path); 117 | 118 | /* If there's a directory path in there, create it */ 119 | p = strrchr(outfn, '/'); 120 | if (*p) { 121 | *p = '\0'; 122 | recursive_mkdir(outfn); 123 | *p = '/'; 124 | } 125 | 126 | return fopen(outfn, mode); 127 | } 128 | 129 | int unpack_image(const char *infn, const char *outdn, int is_absolute) { 130 | uint32_t pid, vid, hardware_id, firmware_id; 131 | FILE *ifp, *ofp, *cfp; 132 | struct imagewty_header *header; 133 | void *image, *curr; 134 | long imagesize; 135 | uint32_t num_files; 136 | size_t i; 137 | 138 | ifp = fopen(infn, "rb"); 139 | if (ifp == NULL) { 140 | return 2; 141 | } 142 | 143 | fseek(ifp, 0, SEEK_END); 144 | imagesize = ftell(ifp); 145 | fseek(ifp, 0, SEEK_SET); 146 | 147 | if (imagesize <= 0) { 148 | return 3; 149 | } 150 | 151 | image = malloc(imagesize); 152 | if (!image) { 153 | return 4; 154 | } 155 | 156 | fread(image, imagesize, 1, ifp); 157 | fclose(ifp); 158 | 159 | /* Check for encryption; see bug #2 (A31 unencrypted images) */ 160 | header = (struct imagewty_header *) image; 161 | if (memcmp(header->magic, IMAGEWTY_MAGIC, IMAGEWTY_MAGIC_LEN) == 0) 162 | flag_encryption_enabled = 0; 163 | 164 | /* Decrypt header (padded to 1024 bytes) */ 165 | O_LOG("Decrypting IMG header...\n"); 166 | curr = rc6_decrypt_inplace(image, 1024, &header_ctx); 167 | 168 | /* Check version of header and setup our local state */ 169 | O_LOG("IMG version is: 0x%0x\n", header->header_version); 170 | if (header->header_version == 0x0300) { 171 | num_files = header->v3.num_files; 172 | hardware_id = header->v3.hardware_id; 173 | firmware_id = header->v3.firmware_id; 174 | pid = header->v3.pid; 175 | vid = header->v3.vid; 176 | } else if (header->header_version == 0x0100) { 177 | num_files = header->v1.num_files; 178 | hardware_id = header->v1.hardware_id; 179 | firmware_id = header->v1.firmware_id; 180 | pid = header->v1.pid; 181 | vid = header->v1.vid; 182 | } else { 183 | return 5; 184 | } 185 | 186 | /* Decrypt file headers */ 187 | curr = rc6_decrypt_inplace(curr, num_files * 1024, &fileheaders_ctx); 188 | 189 | /* Decrypt file contents */ 190 | O_LOG("Decrypting IMG file contents...\n"); 191 | for (i = 0; i < num_files; i++) { 192 | struct imagewty_file_header *filehdr; 193 | uint64_t stored_length; 194 | void *next; 195 | 196 | filehdr = (struct imagewty_file_header *) (image + 1024 + (i * 1024)); 197 | if (header->header_version == 0x0300) { 198 | stored_length = filehdr->v3.stored_length; 199 | } else { 200 | stored_length = filehdr->v1.stored_length; 201 | } 202 | 203 | next = rc6_decrypt_inplace(curr, stored_length, &filecontent_ctx); 204 | curr = next; 205 | } 206 | 207 | O_LOG("Writing the IMG config data...\n"); 208 | cfp = dir_fopen(outdn, "image.cfg", "wb", is_absolute); 209 | if (cfp != NULL) { 210 | char timestr[256]; 211 | struct tm *tm; 212 | time_t t; 213 | time(&t); 214 | tm = localtime(&t); 215 | strcpy(timestr, asctime(tm)); 216 | /* strip newline */ 217 | timestr[strlen(timestr) - 1] = '\0'; 218 | 219 | fputs(";/**************************************************************************/\r\n", cfp); 220 | fprintf(cfp, "; %s\r\n", timestr); 221 | fprintf(cfp, "; generated by %s\r\n", progname); 222 | fprintf(cfp, "; %s\r\n", infn); 223 | fputs(";/**************************************************************************/\r\n", cfp); 224 | fputs("[DIR_DEF]\r\n", cfp); 225 | #ifdef WIN32 226 | fputs("INPUT_DIR = \".\\\\\"\r\n\r\n", cfp); 227 | #else 228 | fputs("INPUT_DIR = \"./\"\r\n\r\n", cfp); 229 | #endif 230 | fputs("[FILELIST]\r\n", cfp); 231 | } 232 | 233 | for (i = 0; i < num_files; i++) { 234 | uint32_t original_length; 235 | struct imagewty_file_header *filehdr; 236 | const char *filename; 237 | uint32_t offset; 238 | 239 | filehdr = (struct imagewty_file_header *) (image + 1024 + (i * 1024)); 240 | if (header->header_version == 0x0300) { 241 | original_length = filehdr->v3.original_length; 242 | filename = filehdr->v3.filename; 243 | offset = filehdr->v3.offset; 244 | } else { 245 | original_length = filehdr->v1.original_length; 246 | filename = filehdr->v1.filename; 247 | offset = filehdr->v1.offset; 248 | } 249 | ofp = dir_fopen(outdn, filename, "wb", is_absolute); 250 | if (ofp) { 251 | fwrite(image + offset, original_length, 1, ofp); 252 | fclose(ofp); 253 | } 254 | 255 | fprintf(cfp, "\t{filename = INPUT_DIR .. \"%s\", maintype = \"%.8s\", subtype = \"%.16s\",},\r\n", 256 | filename[0] == '/' ? filename + 1 : filename, 257 | filehdr->maintype, filehdr->subtype); 258 | } 259 | 260 | if (cfp != NULL) { 261 | /* Now print the relevant stuff for the image.cfg */ 262 | fputs("\r\n[IMAGE_CFG]\r\n", cfp); 263 | fprintf(cfp, 264 | "version = 0x%06x\r\n", header->version); 265 | fprintf(cfp, 266 | "pid = 0x%08x\r\n", pid); 267 | fprintf(cfp, 268 | "vid = 0x%08x\r\n", vid); 269 | fprintf(cfp, 270 | "hardwareid = 0x%03x\r\n", hardware_id); 271 | fprintf(cfp, 272 | "firmwareid = 0x%03x\r\n", firmware_id); 273 | fprintf(cfp, 274 | "imagename = \"%s\"\r\n", infn); 275 | fputs("filelist = FILELIST\r\n", cfp); 276 | fclose(cfp); 277 | } 278 | return 0; 279 | } -------------------------------------------------------------------------------- /src/OpenixIMG/test/T_OpenixIMG.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gloom on 2022/2/13. 3 | // 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include "OpenixIMG.h" 11 | 12 | int main(int argc, char **argv) { 13 | struct stat statbuf; 14 | char outfn[512]; 15 | char *out, *in; 16 | int rc; 17 | if (argc - optind > 2) { 18 | fprintf(stderr, "%s: extra arguments\n", argv[0]); 19 | return 1; 20 | } else if (argc - optind < 1) { 21 | fprintf(stderr, "%s: missing file argument\n", argv[0]); 22 | return 1; 23 | } 24 | /* If we get here, we have a file spec and possibly options */ 25 | //crypto_init(); 26 | rc = stat(argv[optind], &statbuf); 27 | if (rc) { 28 | fprintf(stderr, "%s: cannot stat '%s'!\n", argv[0], argv[optind]); 29 | return 1; 30 | } 31 | out = (argc - optind) == 2 ? argv[optind + 1] : NULL; 32 | in = argv[optind]; 33 | if (out == NULL) { 34 | strcpy(outfn, in); 35 | strcat(outfn, ".dump"); 36 | } else { 37 | strcpy(outfn, out); 38 | } 39 | out = outfn; 40 | unpack_image(in, out); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * main.cpp 3 | * Copyright (c) 2022, YuzukiTsuru 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * See README and LICENSE for more details. 10 | */ 11 | 12 | 13 | #include "OpenixCard.h" 14 | #include "LOG.h" 15 | 16 | int main(int argc, char *argv[]) { 17 | try { 18 | OpenixCard openixCard(argc, argv); 19 | } catch (const std::runtime_error& error) { 20 | LOG::ERROR(error.what()); 21 | return -1; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /test/.gitignore: -------------------------------------------------------------------------------- 1 | *.img 2 | *.dump --------------------------------------------------------------------------------