zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.77k stars 6.57k forks source link

Current Atmel SAM flash driver will not erase last page #70344

Open adkinsjd opened 7 months ago

adkinsjd commented 7 months ago

Describe the bug I have been trying to erase the end of flash on an Atmel Sam4s(8b) using the current sam flash driver. When you attempt to erase the last page the driver returns an EFAULT error despite successfully erasing the last page. I believe this is because the success condition at https://github.com/zephyrproject-rtos/zephyr/blob/2f6b41a694ee47dac5c431a81b7673026b0908c3/drivers/flash/flash_sam.c#L397

cannot be met for the final page, as it requires a start_offset of the last page+1 to meet the condition. I am unsure of the best way to fix the issue, but currently in my application I have added the following check after the erase page function runs and it seems to be working:

/* Check if we've reached the end of pages to erase */                                          
if (info->start_offset + info->size >= erase_data->section_end) {                               
         /* Succeeded, stop iterating */                                                             
         LOG_INF("Marking erase_data succeeded as true.");                                                      
         erase_data->succeeded = true;                                                               
         return false;                                                                               
}          

Note this can't replace the current check condition or else the final page would never be erased.

To Reproduce Run flash_erase on an atmel sam chip for the last page of flash.

Expected behavior No error is reported.

Impact I believe I have solved the issue in my working tree, but I don't feel knowledgeable enough about the flash architecture to submit a PR.

Logs and console output

2024-03-17 16:52:44.852627: [00:01:09.945,000] <dbg> flash_sam: sam_flash_erase: Erasing sector from 0x46000 to 0x80000
2024-03-17 16:52:44.863496: [00:01:09.945,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x46000 of size 0x1000
2024-03-17 16:52:44.874021: [00:01:09.965,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x47000 of size 0x1000
2024-03-17 16:52:44.879790: [00:01:09.985,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x48000 of size 0x1000
2024-03-17 16:52:44.890265: [00:01:10.005,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x49000 of size 0x1000
2024-03-17 16:52:44.900877: [00:01:10.030,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x4a000 of size 0x1000
2024-03-17 16:52:44.906467: [00:01:10.050,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x4b000 of size 0x1000
2024-03-17 16:52:44.917442: [00:01:10.070,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x4c000 of size 0x1000
2024-03-17 16:52:44.927895: [00:01:10.090,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x4d000 of size 0x1000
2024-03-17 16:52:44.938566: [00:01:10.116,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x4e000 of size 0x1000
2024-03-17 16:52:44.943953: [00:01:10.136,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x4f000 of size 0x1000
2024-03-17 16:52:44.954727: [00:01:10.156,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x50000 of size 0x1000
2024-03-17 16:52:44.965497: [00:01:10.176,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x51000 of size 0x1000
2024-03-17 16:52:44.971133: [00:01:10.195,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x52000 of size 0x1000
2024-03-17 16:52:44.981563: [00:01:10.215,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x53000 of size 0x1000
2024-03-17 16:52:44.992396: [00:01:10.235,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x54000 of size 0x1000
2024-03-17 16:52:44.997922: [00:01:10.255,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x55000 of size 0x1000
2024-03-17 16:52:45.008955: [00:01:10.275,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x56000 of size 0x1000
2024-03-17 16:52:45.019503: [00:01:10.295,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x57000 of size 0x1000
2024-03-17 16:52:45.029884: [00:01:10.315,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x58000 of size 0x1000
2024-03-17 16:52:45.035813: [00:01:10.341,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x59000 of size 0x1000
2024-03-17 16:52:45.046110: [00:01:10.361,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x5a000 of size 0x1000
2024-03-17 16:52:45.056767: [00:01:10.381,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x5b000 of size 0x1000
2024-03-17 16:52:45.062654: [00:01:10.400,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x5c000 of size 0x1000
2024-03-17 16:52:45.073025: [00:01:10.420,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x5d000 of size 0x1000
2024-03-17 16:52:45.083698: [00:01:10.440,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x5e000 of size 0x1000
2024-03-17 16:52:45.094327: [00:01:10.460,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x5f000 of size 0x1000
2024-03-17 16:52:45.099948: [00:01:10.480,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x60000 of size 0x1000
2024-03-17 16:52:45.110736: [00:01:10.500,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x61000 of size 0x1000
2024-03-17 16:52:45.121211: [00:01:10.520,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x62000 of size 0x1000
2024-03-17 16:52:45.127038: [00:01:10.540,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x63000 of size 0x1000
2024-03-17 16:52:45.137798: [00:01:10.559,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x64000 of size 0x1000
2024-03-17 16:52:45.148319: [00:01:10.585,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x65000 of size 0x1000
2024-03-17 16:52:45.158831: [00:01:10.605,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x66000 of size 0x1000
2024-03-17 16:52:45.164528: [00:01:10.625,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x67000 of size 0x1000
2024-03-17 16:52:45.175105: [00:01:10.651,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x68000 of size 0x1000
2024-03-17 16:52:45.185737: [00:01:10.671,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x69000 of size 0x1000
2024-03-17 16:52:45.192730: [00:01:10.691,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x6a000 of size 0x1000
2024-03-17 16:52:45.201943: [00:01:10.716,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x6b000 of size 0x1000
2024-03-17 16:52:45.212577: [00:01:10.736,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x6c000 of size 0x1000
2024-03-17 16:52:45.223228: [00:01:10.756,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x6d000 of size 0x1000
2024-03-17 16:52:45.229074: [00:01:10.776,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x6e000 of size 0x1000
2024-03-17 16:52:45.239540: [00:01:10.795,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x6f000 of size 0x1000
2024-03-17 16:52:45.250233: [00:01:10.815,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x70000 of size 0x1000
2024-03-17 16:52:45.255787: [00:01:10.835,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x71000 of size 0x1000
2024-03-17 16:52:45.266485: [00:01:10.855,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x72000 of size 0x1000
2024-03-17 16:52:45.277139: [00:01:10.875,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x73000 of size 0x1000
2024-03-17 16:52:45.282555: [00:01:10.895,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x74000 of size 0x1000
2024-03-17 16:52:45.293350: [00:01:10.921,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x75000 of size 0x1000
2024-03-17 16:52:45.304075: [00:01:10.941,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x76000 of size 0x1000
2024-03-17 16:52:45.314846: [00:01:10.960,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x77000 of size 0x1000
2024-03-17 16:52:45.320146: [00:01:10.980,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x78000 of size 0x1000
2024-03-17 16:52:45.331103: [00:01:11.000,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x79000 of size 0x1000
2024-03-17 16:52:45.341622: [00:01:11.020,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x7a000 of size 0x1000
2024-03-17 16:52:45.347313: [00:01:11.040,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x7b000 of size 0x1000
2024-03-17 16:52:45.357798: [00:01:11.066,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x7c000 of size 0x1000
2024-03-17 16:52:45.368567: [00:01:11.086,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x7d000 of size 0x1000
2024-03-17 16:52:45.379138: [00:01:11.106,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x7e000 of size 0x1000
2024-03-17 16:52:45.384813: [00:01:11.126,000] <dbg> flash_sam: sam_flash_erase_page: Erasing page at 0x7f000 of size 0x1000
2024-03-17 16:52:45.395485: [00:01:11.145,000] <err> firmwareupdate: Failed to erase flash: -14. Cannot proceed with update

Environment (please complete the following information):

github-actions[bot] commented 7 months ago

Hi @adkinsjd! We appreciate you submitting your first issue for our open-source project. 🌟

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙

nandojve commented 7 months ago

Hi @adkinsjd ,

We really appreciate when community sends PR. Take a look at contribute-guidelines for more info about how to do it.

github-actions[bot] commented 5 months ago

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

nandojve commented 5 months ago

Hi @adkinsjd , Do you have plans to send the patch ?

github-actions[bot] commented 3 months ago

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

github-actions[bot] commented 1 month ago

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.