/* * This file is part of the flashrom project. * * Copyright (C) 2005-2008 coresystems GmbH * (Written by Stefan Reinauer for coresystems GmbH) * Copyright (C) 2011-2013 Stefan Tauner * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include #include #include #include #include #include #include "flash.h" #include "programmer.h" #include "layout.h" struct flashrom_layout { struct romentry *head; }; struct layout_include_args { char *name; char *file; struct layout_include_args *next; }; const struct flashrom_layout *get_default_layout(const struct flashrom_flashctx *const flashctx) { return flashctx->default_layout; } const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx) { if (flashctx->layout) return flashctx->layout; else return get_default_layout(flashctx); } static struct romentry *mutable_layout_next( const struct flashrom_layout *const layout, struct romentry *iterator) { return iterator ? iterator->next : layout->head; } static struct romentry *_layout_entry_by_name( const struct flashrom_layout *const layout, const char *name) { struct romentry *entry = NULL; while ((entry = mutable_layout_next(layout, entry))) { if (!strcmp(entry->name, name)) return entry; } return NULL; } #ifndef __LIBPAYLOAD__ int layout_from_file(struct flashrom_layout **layout, const char *name) { FILE *romlayout; char tempstr[256], tempname[256]; int ret = 1; if (flashrom_layout_new(layout)) return 1; romlayout = fopen(name, "r"); if (!romlayout) { msg_gerr("ERROR: Could not open layout file (%s).\n", name); return -1; } while (!feof(romlayout)) { char *tstr1, *tstr2; if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname)) continue; #if 0 // fscanf does not like arbitrary comments like that :( later if (tempstr[0] == '#') { continue; } #endif tstr1 = strtok(tempstr, ":"); tstr2 = strtok(NULL, ":"); if (!tstr1 || !tstr2) { msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr); goto _close_ret; } if (flashrom_layout_add_region(*layout, strtol(tstr1, NULL, 16), strtol(tstr2, NULL, 16), tempname)) goto _close_ret; } ret = 0; _close_ret: (void)fclose(romlayout); return ret; } #endif /* register an include argument (-i) for later processing */ int register_include_arg(struct layout_include_args **args, const char *arg) { struct layout_include_args *tmp; char *colon; char *name; char *file; if (arg == NULL) { msg_gerr(" is a bad region name.\n"); return 1; } /* -i [:] */ colon = strchr(arg, ':'); if (colon && !colon[1]) { msg_gerr("Missing filename parameter in %s\n", arg); return 1; } name = colon ? strndup(arg, colon - arg) : strdup(arg); file = colon ? strdup(colon + 1) : NULL; for (tmp = *args; tmp; tmp = tmp->next) { if (!strcmp(tmp->name, name)) { msg_gerr("Duplicate region name: \"%s\".\n", name); goto error; } } tmp = malloc(sizeof(*tmp)); if (tmp == NULL) { msg_gerr("Could not allocate memory"); goto error; } tmp->name = name; tmp->file = file; tmp->next = *args; *args = tmp; return 0; error: free(name); free(file); return 1; } /* returns 0 to indicate success, 1 to indicate failure */ static int include_region(struct flashrom_layout *const l, const char *name, const char *file) { struct romentry *const entry = _layout_entry_by_name(l, name); if (entry) { entry->included = true; if (file) entry->file = strdup(file); return 0; } return 1; } /* returns -1 if an entry is not found, 0 if found. */ static int find_romentry(struct flashrom_layout *const l, char *name, char *file) { if (!l->head) return -1; msg_gspew("Looking for region \"%s\"... ", name); if (include_region(l, name, file)) { msg_gspew("not found.\n"); return -1; } msg_gspew("found.\n"); return 0; } int get_region_range(struct flashrom_layout *const l, const char *name, unsigned int *start, unsigned int *len) { const struct romentry *const entry = _layout_entry_by_name(l, name); if (entry) { *start = entry->start; *len = entry->end - entry->start + 1; return 0; } return 1; } /* process -i arguments * returns 0 to indicate success, >0 to indicate failure */ int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args) { unsigned int found = 0; const struct layout_include_args *tmp; if (args == NULL) return 0; /* User has specified an include argument, but no layout is loaded. */ if (!l || !l->head) { msg_gerr("Region requested (with -i \"%s\"), " "but no layout data is available.\n", args->name); return 1; } tmp = args; while (tmp) { if (find_romentry(l, tmp->name, tmp->file) < 0) { msg_gerr("Invalid region specified: \"%s\".\n", tmp->name); return 1; } tmp = tmp->next; found++; } msg_ginfo("Using region%s: ", found > 1 ? "s" : ""); tmp = args; while (tmp) { msg_ginfo("\"%s\"", tmp->name); if (tmp->file) msg_ginfo(":\"%s\"", tmp->file); if (found > 1) msg_ginfo(", "); found--; tmp = tmp->next; } msg_ginfo(".\n"); return 0; } /* returns boolean 1 if any regions overlap, 0 otherwise */ int included_regions_overlap(const struct flashrom_layout *const l) { const struct romentry *lhs = NULL; int overlap_detected = 0; while ((lhs = layout_next(l, lhs))) { if (!lhs->included) continue; const struct romentry *rhs = lhs; while ((rhs = layout_next(l, rhs))) { if (!rhs->included) continue; if (lhs->start > rhs->end) continue; if (lhs->end < rhs->start) continue; msg_gwarn("Regions %s [0x%08x-0x%08x] and %s [0x%08x-0x%08x] overlap\n", lhs->name, lhs->start, lhs->end, rhs->name, rhs->start, rhs->end); overlap_detected = 1; } } return overlap_detected; } void cleanup_include_args(struct layout_include_args **args) { struct layout_include_args *tmp; while (*args) { tmp = (*args)->next; free((*args)->name); free((*args)->file); free(*args); *args = tmp; } } int layout_sanity_checks(const struct flashrom_flashctx *const flash) { const struct flashrom_layout *const layout = get_layout(flash); const chipsize_t total_size = flash->chip->total_size * 1024; int ret = 0; const struct romentry *entry = NULL; while ((entry = layout_next(layout, entry))) { if (entry->start >= total_size || entry->end >= total_size) { msg_gwarn("Warning: Address range of region \"%s\" " "exceeds the current chip's address space.\n", entry->name); if (entry->included) ret = 1; } if (entry->start > entry->end) { msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n", entry->name); ret = 1; } } return ret; } void prepare_layout_for_extraction(struct flashctx *flash) { const struct flashrom_layout *const l = get_layout(flash); struct romentry *entry = NULL; unsigned int i; while ((entry = mutable_layout_next(l, entry))) { entry->included = true; if (!entry->file) entry->file = strdup(entry->name); for (i = 0; entry->file[i]; ++i) { if (isspace(entry->file[i])) entry->file[i] = '_'; } } } const struct romentry *layout_next_included_region( const struct flashrom_layout *const l, const chipoff_t where) { const struct romentry *entry = NULL, *lowest = NULL; while ((entry = layout_next(l, entry))) { if (!entry->included) continue; if (entry->end < where) continue; if (!lowest || lowest->start > entry->start) lowest = entry; } return lowest; } const struct romentry *layout_next_included( const struct flashrom_layout *const layout, const struct romentry *iterator) { while ((iterator = layout_next(layout, iterator))) { if (iterator->included) break; } return iterator; } const struct romentry *layout_next( const struct flashrom_layout *const layout, const struct romentry *iterator) { return iterator ? iterator->next : layout->head; } /** * @addtogroup flashrom-layout * @{ */ /** * @brief Create a new, empty layout. * * @param layout Pointer to returned layout reference. * * @return 0 on success, * 1 if out of memory. */ int flashrom_layout_new(struct flashrom_layout **const layout) { *layout = malloc(sizeof(**layout)); if (!*layout) { msg_gerr("Error creating layout: %s\n", strerror(errno)); return 1; } const struct flashrom_layout tmp = { 0 }; **layout = tmp; return 0; } /** * @brief Add another region to an existing layout. * * @param layout The existing layout. * @param start Start address of the region. * @param end End address (inclusive) of the region. * @param name Name of the region. * * @return 0 on success, * 1 if out of memory. */ int flashrom_layout_add_region( struct flashrom_layout *const layout, const size_t start, const size_t end, const char *const name) { struct romentry *const entry = malloc(sizeof(*entry)); if (!entry) goto _err_ret; const struct romentry tmp = { .next = layout->head, .start = start, .end = end, .included = false, .name = strdup(name), .file = NULL, }; *entry = tmp; if (!entry->name) goto _err_ret; msg_gdbg("Added layout entry %08zx - %08zx named %s\n", start, end, name); layout->head = entry; return 0; _err_ret: msg_gerr("Error adding layout entry: %s\n", strerror(errno)); free(entry); return 1; } /** * @brief Mark given region as included. * * @param layout The layout to alter. * @param name The name of the region to include. * * @return 0 on success, * 1 if the given name can't be found. */ int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name) { return include_region(layout, name, NULL); } /** * @br
/*
    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#ifndef MCUCONF_H
#define MCUCONF_H

/*
 * STM32F0xx drivers configuration.
 * The following settings override the default settings present in
 * the various device driver implementation headers.
 * Note that the settings for each driver only have effect if the whole
 * driver is enabled in halconf.h.
 *
 * IRQ priorities:
 * 3...0       Lowest...Highest.
 *
 * DMA priorities:
 * 0...3        Lowest...Highest.
 */

#define STM32F0xx_MCUCONF

/*
 * HAL driver system settings.
 */
#define STM32_NO_INIT                       FALSE
#define STM32_PVD_ENABLE                    FALSE
#define STM32_PLS                           STM32_PLS_LEV0
#define STM32_HSI_ENABLED                   TRUE
#define STM32_HSI14_ENABLED                 TRUE
#define STM32_LSI_ENABLED                   TRUE
#define STM32_HSE_ENABLED                   FALSE
#define STM32_LSE_ENABLED                   FALSE
#define STM32_SW                            STM32_SW_PLL
#define STM32_PLLSRC                        STM32_PLLSRC_HSI_DIV2
#define STM32_PREDIV_VALUE                  1
#define STM32_PLLMUL_VALUE                  12
#define STM32_HPRE                          STM32_HPRE_DIV1
#define STM32_PPRE                          STM32_PPRE_DIV1
#define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK
#define STM32_MCOPRE                        STM32_MCOPRE_DIV1
#define STM32_PLLNODIV                      STM32_PLLNODIV_DIV2
#define STM32_USBSW                         STM32_USBSW_HSI48
#define STM32_CECSW                         STM32_CECSW_HSI
#define STM32_I2C1SW                        STM32_I2C1SW_HSI
#define STM32_USART1SW                      STM32_USART1SW_PCLK
#define STM32_RTCSEL                        STM32_RTCSEL_LSI

/*
 * IRQ system settings.
 */
#define STM32_IRQ_EXTI0_1_IRQ_PRIORITY      3
#define STM32_IRQ_EXTI2_3_IRQ_PRIORITY      3
#define STM32_IRQ_EXTI4_15_IRQ_PRIORITY     3
#define STM32_IRQ_EXTI16_IRQ_PRIORITY       3
#define STM32_IRQ_EXTI17_20_IRQ_PRIORITY    3
#define STM32_IRQ_EXTI21_22_IRQ_PRIORITY    3

/*
 * ADC driver system settings.
 */
#define STM32_ADC_USE_ADC1                  TRUE
#define STM32_ADC_ADC1_CKMODE               STM32_ADC_CKMODE_ADCCLK
#define STM32_ADC_ADC1_DMA_PRIORITY         2
#define STM32_ADC_ADC1_DMA_IRQ_PRIORITY     2
#define STM32_ADC_ADC1_DMA_STREAM           STM32_DMA_STREAM_ID(1, 1)

/*
 * GPT driver system settings.
 */
#define STM32_GPT_USE_TIM1                  FALSE
#define STM32_GPT_USE_TIM2                  FALSE
#define STM32_GPT_USE_TIM3                  FALSE
#define STM32_GPT_USE_TIM14                 FALSE
#define STM32_GPT_TIM1_IRQ_PRIORITY         2
#define STM32_GPT_TIM2_IRQ_PRIORITY         2
#define STM32_GPT_TIM3_IRQ_PRIORITY         2
#define STM32_GPT_TIM14_IRQ_PRIORITY        2

/*
 * I2C driver system settings.
 */
#define STM32_I2C_USE_I2C1                  FALSE
#define STM32_I2C_USE_I2C2                  FALSE
#define STM32_I2C_BUSY_TIMEOUT              50
#define STM32_I2C_I2C1_IRQ_PRIORITY         3
#define STM32_I2C_I2C2_IRQ_PRIORITY         3
#define STM32_I2C_USE_DMA                   TRUE
#define STM32_I2C_I2C1_DMA_PRIORITY         1
#define STM32_I2C_I2C2_DMA_PRIORITY         1
#define STM32_I2C_I2C1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 3)
#define STM32_I2C_I2C1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 2)
#define STM32_I2C_I2C2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 5)
#define STM32_I2C_I2C2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
#define STM32_I2C_DMA_ERROR_HOOK(i2cp)      osalSysHalt("DMA failure")

/*
 * I2S driver system settings.
 */
#define STM32_I2S_USE_SPI1                  FALSE
#define STM32_I2S_USE_SPI2                  FALSE
#define STM32_I2S_SPI1_MODE                 (STM32_I2S_MODE_MASTER |        \
                                             STM32_I2S_MODE_RX)
#define STM32_I2S_SPI2_MODE                 (STM32_I2S_MODE_MASTER |        \
                                             STM32_I2S_MODE_RX)
#define STM32_I2S_SPI1_IRQ_PRIORITY         2
#define STM32_I2S_SPI2_IRQ_PRIORITY         2
#define STM32_I2S_SPI1_DMA_PRIORITY         1
#define STM32_I2S_SPI2_DMA_PRIORITY         1
#define STM32_I2S_SPI1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 2)
#define STM32_I2S_SPI1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 3)
#define STM32_I2S_SPI2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
#define STM32_I2S_SPI2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 5)
#define STM32_I2S_DMA_ERROR_HOOK(i2sp)      osalSysHalt("DMA failure")

/*
 * ICU driver system settings.
 */
#define STM32_ICU_USE_TIM1                  FALSE
#define STM32_ICU_USE_TIM2                  FALSE
#define STM32_ICU_USE_TIM3                  FALSE
#define STM32_ICU_TIM1_IRQ_PRIORITY         3
#define STM32_ICU_TIM2_IRQ_PRIORITY         3
#define STM32_ICU_TIM3_IRQ_PRIORITY         3

/*
 * PWM driver system settings.
 */
#define STM32_PWM_USE_ADVANCED              FALSE
#define STM32_PWM_USE_TIM1                  FALSE
#define STM32_PWM_USE_TIM2                  FALSE
#define STM32_PWM_USE_TIM3                  FALSE
#define STM32_PWM_TIM1_IRQ_PRIORITY         3
#define STM32_PWM_TIM2_IRQ_PRIORITY         3
#define STM32_PWM_TIM3_IRQ_PRIORITY         3

/*
 * SERIAL driver system settings.
 */
#define STM32_SERIAL_USE_USART1             FALSE
#define STM32_SERIAL_USE_USART2             FALSE
#define STM32_SERIAL_USART1_PRIORITY        3
#define STM32_SERIAL_USART2_PRIORITY        3

/*
 * SPI driver system settings.
 */
#define STM32_SPI_USE_SPI1                  FALSE
#define STM32_SPI_USE_SPI2                  FALSE
#define STM32_SPI_SPI1_DMA_PRIORITY         1
#define STM32_SPI_SPI2_DMA_PRIORITY         1
#define STM32_SPI_SPI1_IRQ_PRIORITY         2
#define STM32_SPI_SPI2_IRQ_PRIORITY         2
#define STM32_SPI_SPI1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 2)
#define STM32_SPI_SPI1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 3)
#define STM32_SPI_SPI2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
#define STM32_SPI_SPI2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 5)
#define STM32_SPI_DMA_ERROR_HOOK(spip)      osalSysHalt("DMA failure")

/*
 * ST driver system settings.
 */
#define STM32_ST_IRQ_PRIORITY               2
#define STM32_ST_USE_TIMER                  2

/*
 * UART driver system settings.
 */
#define STM32_UART_USE_USART1               FALSE
#define STM32_UART_USE_USART2               FALSE
#define STM32_UART_USART1_IRQ_PRIORITY      3
#define STM32_UART_USART2_IRQ_PRIORITY      3
#define STM32_UART_USART1_DMA_PRIORITY      0
#define STM32_UART_USART2_DMA_PRIORITY      0
#define STM32_UART_USART1_RX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 3)
#define STM32_UART_USART1_TX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 2)
#define STM32_UART_USART2_RX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 5)
#define STM32_UART_USART2_TX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 4)
#define STM32_UART_DMA_ERROR_HOOK(uartp)    osalSysHalt("DMA failure")

/*
 * WDG driver system settings.
 */
#define STM32_WDG_USE_IWDG                  FALSE

#include "mcuconf_community.h"

#endif /* MCUCONF_H */