Skip to content

MT1959 flashing support#377

Merged
superg merged 7 commits into
mainfrom
asus_flash
May 2, 2026
Merged

MT1959 flashing support#377
superg merged 7 commits into
mainfrom
asus_flash

Conversation

@superg
Copy link
Copy Markdown
Owner

@superg superg commented Apr 29, 2026

Summary by CodeRabbit

  • New Features

    • Added firmware flashing support for MT1959 drives: device validation, on-drive configuration retrieval, firmware patching, chunked flashing with progress reporting, retries, and clear error messages.
    • Added SCSI READ BUFFER support to improve device read/write operations.
  • Documentation

    • CLI help updated to list the new MT1959 flash command.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 29, 2026

Warning

Rate limit exceeded

@superg has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 36 minutes and 22 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4f07bb24-921e-4d4a-ab4f-9f01bbfbf59c

📥 Commits

Reviewing files that changed from the base of the PR and between b38ad8a and d05d299.

📒 Files selected for processing (1)
  • drive/flash_mt1959.ixx
📝 Walkthrough

Walkthrough

Adds MT1959 firmware flashing: new exported module implements drive-specific patching and chunked WRITE_BUFFER flashing, SCSI READ_BUFFER support, CLI registration, and build integration. (≤50 words)

Changes

Cohort / File(s) Summary
Build & CLI
CMakeLists.txt, redumper.ixx, options.ixx
Include drive/flash_mt1959.ixx in build, import drive.flash.mt1959, register "flash::mt1959" in the command map, and add it to CLI help text.
MT1959 Flash Implementation
drive/flash_mt1959.ixx
Add exported module drive.flash.mt1959 and redumper_flash_mt1959(Context&, Options&): firmware load/size checks, extract/derive firmware/config IDs, optional allowlist checks, read drive config via READ_BUFFER, patch firmware (FF-fill then inject config), write in block-sized chunks with WRITE_BUFFER (DOWNLOAD_MICROCODE_WITH_OFFSETS), poll drive ready, and error handling/logging.
SCSI: READ_BUFFER & CDBs
scsi/mmc.ixx, scsi/cmd.ixx
Add READ_BUFFER = 0x3C opcode, READ_BUFFER_Mode, CDB10_ReadBuffer layout, and exported cmd_read_buffer wrapper to issue SCSI READ BUFFER commands and return SPTD status.

Sequence Diagram

sequenceDiagram
    participant CLI as "User / CLI"
    participant Dispatcher as "Command Dispatcher"
    participant MT1959 as "drive.flash.mt1959"
    participant SPTD as "SPTD / SCSI Layer"
    participant Drive as "MT1959 Device"

    CLI->>Dispatcher: Issue "flash::mt1959"
    Dispatcher->>MT1959: redumper_flash_mt1959(ctx, options)
    MT1959->>MT1959: Load firmware, validate sizes, derive firmware_id
    MT1959->>SPTD: cmd_read_buffer(mode, offset, length)
    SPTD->>Drive: READ BUFFER CDB (0x3C)
    Drive-->>SPTD: Return config data
    SPTD-->>MT1959: Provide config bytes
    MT1959->>MT1959: Patch firmware (FF fill, inject config)
    MT1959->>SPTD: WRITE BUFFER chunks (DOWNLOAD_MICROCODE_WITH_OFFSETS)
    SPTD->>Drive: Send chunk
    Drive-->>SPTD: Acknowledge / status
    SPTD-->>MT1959: Report status/progress
    MT1959->>SPTD: cmd_drive_ready() (retry loop)
    MT1959-->>Dispatcher: return 0 (on success)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • Improved flashing checks #307: Adds Options::force_flash, allowlist/drive-guard checks and related flashing patterns used by the new MT1959 implementation.

Poem

🐰 I hopped in code with nimble feet,
Read buffers, patches—what a treat!
Chunks of firmware in careful rows,
MT1959 now proudly glows.
A tiny hop, a rabbit’s cheer!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'MT1959 flashing support' directly and clearly describes the main feature being added—support for MT1959 firmware flashing across multiple modules.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch asus_flash

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 36 minutes and 22 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@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: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@drive/flash_mt1959.ixx`:
- Around line 33-55: The post-flash readiness check in flash_mt1959 is currently
ignored: call cmd_drive_ready(sptd), capture its status/return value and verify
it indicates the drive re-entered ready state, and on failure log or throw
similarly to the other SCSI failures (e.g. use throw_line("drive failed to
become ready after flash, SCSI ({})", SPTD::StatusMessage(status)) or an
equivalent processLogger.error path); ensure you reference cmd_drive_ready and
SPTD::StatusMessage to build the error message so flashing does not report
success when the device remains not-ready.
- Around line 69-73: The modify_firmware function currently assumes
firmware_data is large enough and can OOB-write; add explicit size validation at
the start of modify_firmware(std::span<uint8_t> firmware_data, uint32_t
config_offset, std::span<const uint8_t> drive_config): check
firmware_data.size() >= 0x10000 and firmware_data.size() >=
static_cast<size_t>(config_offset) + drive_config.size(), and if either check
fails, fail fast (throw a clear exception such as std::out_of_range or return an
error) instead of proceeding to std::fill and std::copy; keep the existing
fill/copy logic but only execute it after these validations.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 878ae1f4-188e-4320-8aec-3dc74cc4b422

📥 Commits

Reviewing files that changed from the base of the PR and between d6ec7f1 and 02fac10.

📒 Files selected for processing (6)
  • CMakeLists.txt
  • drive/flash_mt1959.ixx
  • options.ixx
  • redumper.ixx
  • scsi/cmd.ixx
  • scsi/mmc.ixx

Comment thread drive/flash_mt1959.ixx
Comment thread drive/flash_mt1959.ixx Outdated
Copy link
Copy Markdown
Contributor

@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

♻️ Duplicate comments (1)
drive/flash_mt1959.ixx (1)

63-72: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

i = wait_seconds - 2 causes an infinite loop on successful flash.

When cmd_drive_ready returns success, i is set to wait_seconds - 2 = 13. The for-loop's own ++i then advances i to 14, which still satisfies 14 < 15, so the loop body runs again. On that next iteration the drive is still ready, i is reset to 13 again, ++i → 14, and the cycle repeats forever. Every successful flash will hang the process indefinitely.

A secondary problem that persists from the prior review: if the 15-second window expires with the drive still not ready, the function falls through and logs "flashing success" anyway.

Both are fixed by using break and checking the outcome:

🐛 Proposed fix
-    constexpr uint32_t wait_seconds = 15;
-    for(uint32_t i = 0; i < wait_seconds; ++i)
-    {
-        LOGC_RF("waiting for drive to become ready... [{}/{}]", i + 1, wait_seconds);
-        if(!cmd_drive_ready(sptd).status_code)
-            i = wait_seconds - 2;
-        std::this_thread::sleep_for(std::chrono::seconds(1));
-    }
-
-    LOGC_RF("");
-    LOGC("flashing success");
+    constexpr uint32_t wait_seconds = 15;
+    bool drive_ready = false;
+    for(uint32_t i = 0; i < wait_seconds; ++i)
+    {
+        LOGC_RF("waiting for drive to become ready... [{}/{}]", i + 1, wait_seconds);
+        if(!cmd_drive_ready(sptd).status_code)
+        {
+            drive_ready = true;
+            break;
+        }
+        std::this_thread::sleep_for(std::chrono::seconds(1));
+    }
+    LOGC("");
+    if(!drive_ready)
+        throw_line("drive did not become ready after flashing");
+    LOGC("flashing success");
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@drive/flash_mt1959.ixx` around lines 63 - 72, The loop uses i = wait_seconds
- 2 which creates an infinite loop; instead call cmd_drive_ready(sptd) once per
iteration and if it indicates success, break out of the for-loop (do not mutate
i). After the loop, check the final readiness result (store the cmd_drive_ready
return in a variable) and only call LOGC("flashing success") when that result
was successful; otherwise log an error/timeout and handle failure accordingly.
Ensure you update references around wait_seconds, cmd_drive_ready, LOGC_RF, and
LOGC to use the stored result.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@drive/flash_mt1959.ixx`:
- Around line 86-87: The error thrown when cmd_read_buffer fails omits the SCSI
status; update the failure path in the cmd_read_buffer call (the if that checks
status.status_code) to include the SPTD::StatusMessage(status) in the throw_line
message so the thrown error contains diagnostic details (referencing
cmd_read_buffer, SPTD::StatusMessage, throw_line, config, config_offset and
READ_BUFFER_Mode::DOWNLOAD_MICROCODE_WITH_OFFSETS).

---

Duplicate comments:
In `@drive/flash_mt1959.ixx`:
- Around line 63-72: The loop uses i = wait_seconds - 2 which creates an
infinite loop; instead call cmd_drive_ready(sptd) once per iteration and if it
indicates success, break out of the for-loop (do not mutate i). After the loop,
check the final readiness result (store the cmd_drive_ready return in a
variable) and only call LOGC("flashing success") when that result was
successful; otherwise log an error/timeout and handle failure accordingly.
Ensure you update references around wait_seconds, cmd_drive_ready, LOGC_RF, and
LOGC to use the stored result.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c67f8b62-9e86-404d-b84b-129af0f2900c

📥 Commits

Reviewing files that changed from the base of the PR and between 82b1031 and b38ad8a.

📒 Files selected for processing (1)
  • drive/flash_mt1959.ixx

Comment thread drive/flash_mt1959.ixx Outdated
@superg superg merged commit 856faf2 into main May 2, 2026
11 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.

1 participant