Skip to content

Fix device ID validation bound, refactor COE injection, fix typos and type annotations#606

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-device-id-validation
Draft

Fix device ID validation bound, refactor COE injection, fix typos and type annotations#606
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-device-id-validation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 12, 2026

Several correctness, maintainability, and code-quality issues across the device config, file manager, and Vivado utilities modules.

Changes

src/device_clone/device_config.py

  • Device ID validation: Lower bound corrected from 0x00010x0000; 0x0000 is a valid PCI device ID per spec

src/file_management/file_manager.py

  • Refactor: Extracted inline .coe injection block from copy_ip_files into _inject_generated_coe_files — separates "copy templates" from "overwrite with generated files"
  • Documentation: Added inline comment explaining the .coe cfgspace dword layout (DDDDVVVV at offset 0x00) and why the hex string is split at [0:4]/[4:8]
# DDDDVVVV dword: chars [0:4] => Device ID, chars [4:8] => Vendor ID
match = re.search(r'^\s*([0-9a-fA-F]{8})', content, re.MULTILINE)

src/vivado_handling/build_state_cleaner.py

  • Type annotation: seen: setseen: set[Path]

src/vivado_handling/vivado_utils.py

  • Rename: _VIVADOct_version_vivado_detect_version; old name retained as a backward-compatible alias
  • Spelling: VIVADOctiondetection in comment and debug_vivado_search docstring/print

tests/test_file_manager.py

  • Mock fix: test_bug528_coe_files_overwrite_template_with_device_ids was calling mock_repo_manager.get_board_path on the class mock rather than the instance; fixed by routing through mock_repo_manager_class.return_value
Original prompt
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes being applied.

[{"message":"Device ID validation should allow 0x0000 as it's a valid device ID in the PCIe specification. The lower bound should be 0x0000, not 0x0001.","fixFiles":[{"filePath":"src/device_clone/device_config.py","diff":"diff --git a/src/device_clone/device_config.py b/src/device_clone/device_config.py\n--- a/src/device_clone/device_config.py\n+++ b/src/device_clone/device_config.py\n@@ -155,7 +155,7 @@\n         \"\"\"Validate device identification values.\"\"\"\n         if not (0x0001 <= self.vendor_id <= 0xFFFE):\n             raise ValueError(f\"Invalid vendor ID: 0x{self.vendor_id:04X}\")\n-        if not (0x0001 <= self.device_id <= 0xFFFF):\n+        if not (0x0000 <= self.device_id <= 0xFFFF):\n             raise ValueError(f\"Invalid device ID: 0x{self.device_id:04X}\")\n         if not (0x000000 <= self.class_code <= 0xFFFFFF):\n             raise ValueError(f\"Invalid class code: 0x{self.class_code:06X}\")\n"}]},{"message":"The logic for overwriting template .coe files with generated ones (lines 875-932) is embedded within `copy_ip_files`. This creates a long method that mixes two distinct responsibilities: copying IP files and overwriting specific files with generated versions. Consider extracting this .coe file injection logic into a separate private method like `_inject_generated_coe_files` to improve maintainability and testability.","fixFiles":[{"filePath":"src/file_management/file_manager.py","diff":"diff --git a/src/file_management/file_manager.py b/src/file_management/file_manager.py\n--- a/src/file_management/file_manager.py\n+++ b/src/file_management/file_manager.py\n@@ -833,6 +833,68 @@\n         Raises:\n             FileNotFoundError: If IP files are not found\n         \"\"\"\n+    def _inject_generated_coe_files(self, ip_dir: Path, copied_files: List[Path]) -> None:\n+        \"\"\"Overwrite template .coe files with generated ones from output/src.\"\"\"\n+        src_dir = self.output_dir / \"src\"\n+        if src_dir.exists():\n+            generated_coe_files = list(src_dir.glob(\"*.coe\"))\n+            if generated_coe_files:\n+                log_info_safe(\n+                    logger,\n+                    \"Injecting device IDs into IP configuration files\",\n+                    prefix=\"FILEMGR\",\n+                )\n+\n+                # Extract and display device IDs from config space file\n+                for coe_file in generated_coe_files:\n+                    if \"cfgspace\" in coe_file.name and \"writemask\" not in coe_file.name:\n+                        try:\n+                            content = coe_file.read_text()\n+                            match = re.search(\n+                                r'^\\s*([0-9a-fA-F]{8})',\n+                                content,\n+                                re.MULTILINE\n+                            )\n+                            if match:\n+                                hex_value = match.group(1)\n+                                vendor_id = hex_value[4:8].upper()\n+                                device_id = hex_value[0:4].upper()\n+                                log_info_safe(\n+                                    logger,\n+                                    safe_format(\n+                                        \"Device: 0x{device}  Vendor: 0x{vendor}\",\n+                                        device=device_id,\n+                                        vendor=vendor_id\n+                                    ),\n+                                    prefix=\"FILEMGR\",\n+                                )\n+                        except Exception as exc:\n+                            log_error_safe(\n+                                logger,\n+                                safe_format(\n+                                    \"Failed to parse device IDs from COE file {file}: {error}\",\n+                                    file=coe_file,\n+                                    error=exc,\n+                                ),\n+                                prefix=\"FILEMGR\",\n+                            )\n+\n+                # Copy generated files over templates\n+                for coe_file in generated_coe_files:\n+                    dest_file = ip_dir / coe_file.name\n+                    shutil.copy2(coe_file, dest_file)\n+\n+                    log_info_safe(\n+                        logger,\n+                        safe_format(\"✓ {ip_name}\", ip_name=coe_file.name),\n+                        prefix=\"FILEMGR\",\n+                    )\n+\n+                    # Add to copied files if not already there\n+                    if dest_file not in copied_files:\n+                        copied_files.append(dest_file)\n+\n+    def copy_ip_files(self, board: str) -> bool:\n         try:\n             from pcileechfwgenerator.file_managemen...

…annotations, spelling corrections, and test mock fix

Agent-Logs-Url: https://github.com/VoltCyclone/PCILeechFWGenerator/sessions/4af478b0-41d8-4479-959d-0a8f53d36fe0

Co-authored-by: ramseymcgrath <2006510+ramseymcgrath@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix device ID validation to allow 0x0000 Fix device ID validation bound, refactor COE injection, fix typos and type annotations May 12, 2026
Copilot AI requested a review from ramseymcgrath May 12, 2026 06:40
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.

2 participants