Skip to content

Expand SDRAM code space to 4MB.#288

Merged
lihuanhuan merged 2 commits intoOneKeyHQ:mainfrom
lihuanhuan:pro_main
Apr 14, 2025
Merged

Expand SDRAM code space to 4MB.#288
lihuanhuan merged 2 commits intoOneKeyHQ:mainfrom
lihuanhuan:pro_main

Conversation

@lihuanhuan
Copy link
Copy Markdown
Contributor

@lihuanhuan lihuanhuan commented Apr 14, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced validations to prevent errors when processing oversized firmware files and ensure reliable firmware verification.
  • New Features

    • Increased memory allocation to support larger firmware images and improved update capacity.
    • Introduced new configuration parameters to better manage firmware size limits.
  • Refactor

    • Streamlined image loading, installation, and validation processes for a smoother firmware update experience.
    • Consolidated hash computation logic for consistent firmware integrity checks.

@lihuanhuan lihuanhuan requested a review from a team as a code owner April 14, 2025 07:16
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 14, 2025

Walkthrough

The pull request implements changes to firmware validation and memory layout across several modules. It adds stricter firmware file size checks and updates function signatures by adding a boolean parameter. The firmware header is modified by replacing reserved bytes with a declared hash block. Memory regions in the linker script and SDRAM definitions are doubled. The Trezor utility removes progress tracking and simplifies firmware hashing. Python modules now update hash assignments, include a new constant, and change the return type for hash calculations.

Changes

File(s) Change Summary
core/embed/emmc_wrapper/emmc_commands.c, core/embed/trezorhal/image.c, core/embed/trezorhal/image.h Enhanced firmware size validation; updated function signatures (added boolean flag) and image header now includes a defined hash_block field.
core/embed/extmod/modtrezorutils/modtrezorutils.c Removed ui_progress and simplified firmware hash logic by eliminating sector loops and progress tracking.
core/embed/firmware/header.S Replaced reserved 4 bytes with .word 0 to declare the hash block size.
core/embed/firmware/memory_H.ld Doubled memory sizes for FLASH2 and EXRAM from 2048K to 4096K.
core/embed/trezorhal/sdram.h Updated FMC_SDRAM_FIRMWARE_P2_LEN from MB(2) to MB(4) and changed its address.
python/src/trezorlib/_internal/firmware_headers.py, python/src/trezorlib/firmware.py Changed hash assignment in BinImage; added constant FIREMWARE_SIZE_LIMIT; updated calculate_code_hashes to return a tuple; replaced _reserved with hash_block in firmware header.

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (6)
python/src/trezorlib/firmware.py (1)

74-74: Fix typo in constant name

The constant name should be "FIRMWARE_SIZE_LIMIT" not "FIREMWARE_SIZE_LIMIT".

-FIREMWARE_SIZE_LIMIT = V2_CHUNK_SIZE * 16
+FIRMWARE_SIZE_LIMIT = V2_CHUNK_SIZE * 16
core/embed/extmod/modtrezorutils/modtrezorutils.c (1)

49-55: Comment removal needs attention

The ui_progress function is commented out rather than properly removed.

-// static void ui_progress(mp_obj_t ui_wait_callback, uint32_t current,
-//                         uint32_t total) {
-//   if (mp_obj_is_callable(ui_wait_callback)) {
-//     mp_call_function_2_protected(ui_wait_callback, mp_obj_new_int(current),
-//                                  mp_obj_new_int(total));
-//   }
-// }
core/embed/trezorhal/image.h (1)

190-191: Fix typo in parameter name

Parameter name has a spelling error: "chcek_remain" should be "check_remain".

-                                       bool chcek_remain);
+                                       bool check_remain);
core/embed/trezorhal/image.c (3)

334-334: Fix typo in parameter name.

Parameter chcek_remain has a typo. Should be check_remain.

-                                 const size_t code_len_check,
-                                 bool chcek_remain) {
+                                 const size_t code_len_check,
+                                 bool check_remain) {

377-387: Use shorter variable name in loop and fix typo references.

The i loop variable is fine but chcek_remain typo appears again.

-      if (chcek_remain) {
+      if (check_remain) {

766-775: Change misleading error message.

Error says "invalid" when the problem is read failure.

  ExecuteCheck_ADV(
      emmc_fs_file_read("0:data/fw_p2.bin", 0,
                        (uint32_t*)FMC_SDRAM_FIRMWARE_P2_ADDRESS,
                        MAX(path_info.size, fw_external_size), &processed_len),
      true, {
        if (error_msg != NULL)
-          strncpy(error_msg, "Firmware code invalid! (P2_EMMC_2)",
+          strncpy(error_msg, "Failed to read firmware P2 file!",
                  error_msg_len);
        return secfalse;
      });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cd4c4cc and f6db9ab.

📒 Files selected for processing (9)
  • core/embed/emmc_wrapper/emmc_commands.c (3 hunks)
  • core/embed/extmod/modtrezorutils/modtrezorutils.c (2 hunks)
  • core/embed/firmware/header.S (1 hunks)
  • core/embed/firmware/memory_H.ld (1 hunks)
  • core/embed/trezorhal/image.c (9 hunks)
  • core/embed/trezorhal/image.h (4 hunks)
  • core/embed/trezorhal/sdram.h (1 hunks)
  • python/src/trezorlib/_internal/firmware_headers.py (2 hunks)
  • python/src/trezorlib/firmware.py (5 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
core/embed/trezorhal/image.c (4)
core/embed/emmc_wrapper/emmc_fs.h (5)
  • emmc_fs_dir_make (150-150)
  • emmc_fs_file_delete (138-138)
  • emmc_fs_file_write (133-136)
  • emmc_fs_path_info (124-124)
  • emmc_fs_file_read (130-132)
core/embed/trezorhal/common.c (1)
  • hal_delay (292-292)
core/embed/trezorhal/common.h (1)
  • hal_delay (107-107)
core/embed/trezorhal/image.h (1)
  • check_image_contents_ADV (186-191)
core/embed/emmc_wrapper/emmc_commands.c (4)
core/embed/bootloader/messages.c (1)
  • send_failure (312-317)
core/embed/bootloader/messages.h (1)
  • send_failure (41-41)
core/embed/trezorhal/image.h (1)
  • check_image_contents_ADV (186-191)
core/embed/trezorhal/image.c (1)
  • check_image_contents_ADV (329-399)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Gen check
  • GitHub Check: Defs check
  • GitHub Check: Style check
🔇 Additional comments (18)
core/embed/trezorhal/sdram.h (1)

65-65: Code space doubled to 4MB.

The firmware memory allocation is now doubled from 2MB to 4MB, matching PR objective.

core/embed/firmware/header.S (1)

30-30: Reserved field repurposed for hash block size.

Previously unused reserved field now stores hash block size.

core/embed/firmware/memory_H.ld (2)

7-7: FLASH2 memory region doubled.

FLASH2 length increased from 2048K to 4096K.


14-14: EXRAM memory region doubled.

EXRAM length increased from 2048K to 4096K.

python/src/trezorlib/_internal/firmware_headers.py (3)

256-258: Updated code hash calculation to return hash block.

Now captures hash_block from calculate_code_hashes.


261-261: Added hash_block to digest header.

Ensures hash_block is included in digest calculations.


273-273: Added hash_block to rehash method.

Maintains hash_block consistency during rehashing.

python/src/trezorlib/firmware.py (3)

197-198: Appropriate replacement of reserved field

Replacing the reserved field with a meaningful hash_block parameter aligns with the firmware header changes in the core.


357-380: Return type and logic changes for chunk size calculation

The function now returns a tuple with hash list and chunk size flag. The logic correctly doubles the chunk size when needed.


396-398: Call site updated correctly

The call to calculate_code_hashes properly unpacks the tuple return value.

core/embed/extmod/modtrezorutils/modtrezorutils.c (1)

163-169: Function implementation significantly simplified

The function now returns empty data without computing a hash. Comment doesn't explain the rationale.

What happens when a caller expects actual hash data from this function? Is this intentional?

core/embed/emmc_wrapper/emmc_commands.c (4)

603-608: Added size verification for P2 segment

New check prevents oversized firmware segments. Good defensive programming.


614-614: Updated function call with new parameter

Added boolean parameter to check_image_contents_ADV call.


625-625: Updated size check against new macro

Size verification now uses the updated FIRMWARE_IMAGE_MAXSIZE constant.


853-853: Added parameter to function call

Consistent update of check_image_contents_ADV call with the boolean parameter.

core/embed/trezorhal/image.h (3)

26-26: Added necessary include

Including sdram.h provides access to FMC_SDRAM_FIRMWARE_P2_LEN used below.


43-47: Improved size definition macros

Splitting into FIRMWARE_IMAGE_INNER_SIZE and FIRMWARE_IMAGE_MAXSIZE improves clarity.


64-64: Replaced reserved field with meaningful variable

Changed from reserved bytes to a typed hash_block field, matching firmware.py changes.

@lihuanhuan lihuanhuan merged commit 53d859a into OneKeyHQ:main Apr 14, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants