blob: 41c315f412516bc717793545dd839bc6518b7c63 [file] [log] [blame]
Lawrence Tang02c801a2022-07-18 14:43:52 +01001/**
Lawrence Tangefe17e22022-08-08 09:16:23 +01002 * Functions for generating pseudo-random CPER generic processor sections.
Ed Tanousfedd4572024-07-12 13:56:00 -07003 *
Lawrence Tang02c801a2022-07-18 14:43:52 +01004 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdlib.h>
Thu Nguyene42fb482024-10-15 14:43:11 +00008#include <libcper/BaseTypes.h>
9#include <libcper/generator/gen-utils.h>
10#include <libcper/generator/sections/gen-section.h>
Lawrence Tang02c801a2022-07-18 14:43:52 +010011
Lawrence Tangefe17e22022-08-08 09:16:23 +010012//Generates a single pseudo-random generic processor section, saving the resulting address to the given
Lawrence Tang02c801a2022-07-18 14:43:52 +010013//location. Returns the size of the newly created section.
John Chungf8fc7052024-05-03 20:05:29 +080014size_t generate_section_generic(void **location)
Lawrence Tang02c801a2022-07-18 14:43:52 +010015{
John Chungf8fc7052024-05-03 20:05:29 +080016 //Create random bytes.
17 size_t size = generate_random_section(location, 192);
Lawrence Tang02c801a2022-07-18 14:43:52 +010018
John Chungf8fc7052024-05-03 20:05:29 +080019 //Set reserved locations to zero.
20 UINT8 *start_byte = (UINT8 *)*location;
21 *((UINT64 *)start_byte) &= 0xFFF;
22 *(start_byte + 12) &= 0x7;
23 *((UINT16 *)(start_byte + 14)) = 0x0;
Lawrence Tangaacf0e22022-07-20 13:28:52 +010024
John Chungf8fc7052024-05-03 20:05:29 +080025 //Ensure CPU brand string does not terminate early.
26 for (int i = 0; i < 128; i++) {
27 UINT8 *byte = start_byte + 24 + i;
28 if (*byte == 0x0) {
29 *byte = rand() % 127 + 1;
30 }
Lawrence Tangaacf0e22022-07-20 13:28:52 +010031
John Chungf8fc7052024-05-03 20:05:29 +080032 //Null terminate last byte.
33 if (i == 127) {
34 *byte = 0x0;
35 }
36 }
37
38 return size;
39}