Nikolai Artemiev has uploaded this change for review.

View Change

writeprotect.c: move wp_bits -> register values/masks conversion into func

Extract the code that converts `struct wp_bits` into a collection of
register values and bit masks out of `write_wp_bits()` into a new
funciton.

TEST=ninja test

Change-Id: I604478ecbb70392c5584bf5d87c76b6f20f882b1
Signed-off-by: Nikolai Artemiev <nartemiev@google.com>
---
M writeprotect.c
1 file changed, 33 insertions(+), 10 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/46/69846/1
diff --git a/writeprotect.c b/writeprotect.c
index 38ac997..46b864c 100644
--- a/writeprotect.c
+++ b/writeprotect.c
@@ -140,18 +140,16 @@
}
}

-/** Write WP configuration bits to the flash's registers. */
-static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits)
+/** Convert wp_bits to register values and write masks */
+static void get_wp_bits_reg_values(
+ uint8_t *reg_values, uint8_t *write_masks, uint8_t *bit_masks,
+ const struct reg_bit_map *reg_bits, struct wp_bits bits)
{
- size_t i;
- const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
+ memset(reg_values, 0, sizeof(uint8_t) * MAX_REGISTERS);
+ memset(write_masks, 0, sizeof(uint8_t) * MAX_REGISTERS);
+ memset(bit_masks, 0, sizeof(uint8_t) * MAX_REGISTERS);

- /* Convert wp_bits to register values and masks */
- uint8_t reg_values[MAX_REGISTERS] = {0};
- uint8_t bit_masks[MAX_REGISTERS] = {0}; /* masks of valid bits */
- uint8_t write_masks[MAX_REGISTERS] = {0}; /* masks of written bits */
-
- for (i = 0; i < bits.bp_bit_count; i++)
+ for (size_t i = 0; i < bits.bp_bit_count; i++)
set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->bp[i], bits.bp[i]);

set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->tb, bits.tb);
@@ -161,6 +159,15 @@
set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srl, bits.srl);
/* Note: always setting WPS bit to zero until its fully supported. */
set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->wps, 0);
+}
+
+/** Write WP configuration bits to the flash's registers. */
+static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits)
+{
+ uint8_t reg_values[MAX_REGISTERS];
+ uint8_t bit_masks[MAX_REGISTERS]; /* masks of valid bits */
+ uint8_t write_masks[MAX_REGISTERS]; /* masks of written bits */
+ get_wp_bits_reg_values(reg_values, bit_masks, write_masks, &flash->chip->reg_bits, bits);

/* Write each register whose value was updated */
for (enum flash_reg reg = STATUS1; reg < MAX_REGISTERS; reg++) {

To view, visit change 69846. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I604478ecbb70392c5584bf5d87c76b6f20f882b1
Gerrit-Change-Number: 69846
Gerrit-PatchSet: 1
Gerrit-Owner: Nikolai Artemiev <nartemiev@google.com>
Gerrit-MessageType: newchange