Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 1 | /** |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 2 | * Functions for generating pseudo-random CPER PCI component error sections. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdlib.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 8 | #include <libcper/BaseTypes.h> |
| 9 | #include <libcper/generator/gen-utils.h> |
| 10 | #include <libcper/generator/sections/gen-section.h> |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 11 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 12 | //Generates a single pseudo-random PCI component error section, saving the resulting address to the given |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 13 | //location. Returns the size of the newly created section. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 14 | size_t generate_section_pci_dev(void **location) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 15 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 16 | //Generate how many register pairs will be attached to this section. |
| 17 | UINT32 num_memory_pairs = rand() % 4; |
| 18 | UINT32 num_io_pairs = rand() % 4; |
| 19 | UINT32 num_registers = num_memory_pairs + num_io_pairs; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 20 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 21 | //Create random bytes. |
| 22 | int size = 40 + (num_registers * 16); |
| 23 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 24 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 25 | //Set reserved areas to zero. |
| 26 | UINT64 *validation = (UINT64 *)bytes; |
| 27 | *validation &= 0x1F; //Validation 5-63 |
| 28 | for (int i = 0; i < 5; i++) { |
| 29 | *(bytes + 27 + i) = 0; //Bytes 11-15 of ID info. |
| 30 | } |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 31 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 32 | //Set expected values. |
| 33 | UINT32 *memory_number_field = (UINT32 *)(bytes + 32); |
| 34 | UINT32 *io_number_field = (UINT32 *)(bytes + 36); |
| 35 | *memory_number_field = num_memory_pairs; |
| 36 | *io_number_field = num_io_pairs; |
| 37 | |
| 38 | //Fix error status. |
| 39 | create_valid_error_section(bytes + 8); |
| 40 | |
| 41 | //Set return values, exit. |
| 42 | *location = bytes; |
| 43 | return size; |
| 44 | } |