first and last commit

This commit is contained in:
Chris Nutter committed 2023-07-08 13:03:17 -07:00
commit 1bf4efd552
2612 files changed
+636500

No files matched your search

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_CONTEXT_H_
#define _EFFACEABLE_CONTEXT_H_
// =============================================================================
struct _effaceable_nand_context
{
void * buf;
effaceable_storage_t * storage;
effaceable_system_t * system;
effaceable_nand_hal_t * nand;
};
// =============================================================================
struct _effaceable_nor_context
{
effaceable_storage_t * storage;
effaceable_system_t * system;
effaceable_nor_hal_t * nor;
};
// =============================================================================
struct _effaceable_storage_context
{
bool enable_full_scan;
bool enable_wipe;
bool is_formatted;
uint32_t generation;
uint32_t group;
void * cache;
void * scratch;
void * sniff;
void * crypt;
void * nonce_hash;
locker_list_t locker_list;
effaceable_system_t * system;
effaceable_device_t * device;
};
// =============================================================================
typedef struct _effaceable_nor_context effaceable_nor_context_t;
typedef struct _effaceable_nand_context effaceable_nand_context_t;
typedef struct _effaceable_storage_context effaceable_storage_context_t;
// =============================================================================
#endif /* _EFFACEABLE_CONTEXT_H_ */
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_DELEGATION_H_
#define _EFFACEABLE_DELEGATION_H_
// =============================================================================
#define isFnAvailable(_me, _it, _fn) (0 != context(_me)->_it->_fn)
#define delegateFn(_me, _it, _fn, ...) context(_me)->_it->_fn(context(_me)->_it, ## __VA_ARGS__)
// =============================================================================
#endif /* _EFFACEABLE_DELEGATION_H_ */
+38
View File
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2010-11 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_LOCKER_H_
#define _EFFACEABLE_LOCKER_H_
#include "queue.h"
// =============================================================================
struct _locker;
struct _locker_list;
// -----------------------------------------------------------------------------
typedef struct _locker locker_t;
typedef TAILQ_HEAD(_locker_list, _locker) locker_list_t;
// -----------------------------------------------------------------------------
struct _locker
{
TAILQ_ENTRY(_locker) link;
AppleEffaceableStorageLockerHeader header;
uint8_t data[0];
};
// =============================================================================
#endif /* _EFFACEABLE_LOCKER_H_ */
@@ -0,0 +1,465 @@
/*
* COPYRIGHT (c) 2010-11 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
// =============================================================================
#include <effaceable_contract.h>
#include <effaceable_debug.h>
#include "effaceable_private_types.h"
#include "effaceable_storage_dev.h"
#include "effaceable_nand_core.h"
#include "effaceable_delegation.h"
// =============================================================================
#define debug(...) dlogf(device, __VA_ARGS__ )
// =============================================================================
extern void
startEffaceableNAND(effaceable_device_t * device, effaceable_system_t * system, effaceable_nand_hal_t * nand, effaceable_storage_t * storage)
{
initContext(device, system, nand, storage);
initContract(device);
setBuf(device, allocMem(device, getUnitSize(device)));
startEffaceableStorage(storage, device, system);
}
// -----------------------------------------------------------------------------
static uint32_t
getGroupCount(effaceable_device_t * device)
{
return ((uint32_t)(getBankCount(device) * getBlockCount(device)));
}
static uint32_t
getUnitsPerGroup(effaceable_device_t * device)
{
return ((uint32_t)(getPageCount(device) - kAppleEffaceableNANDTotalMetaPages));
}
static uint32_t
getUnitSize(effaceable_device_t * device)
{
return getPageSize(device);
}
static EffaceableReturn
eraseGroup(effaceable_device_t * device, uint32_t group)
{
const uint32_t bank = mapGroupToBank(device, group);
const uint32_t block = mapGroupToBlock(device, group);
EffaceableReturn ok = kEffaceableReturnError;
if (isBlockBad(device, bank, block)) {
// XXX remap block
ok = kEffaceableReturnBadMedia;
} else if (kEffaceableReturnSuccess == (ok = eraseBlock(device, bank, block))) {
openBlock(device, bank, block);
}
return ok;
}
static void
openBlock(effaceable_device_t * device, uint32_t bank, uint32_t block)
{
uint32_t page;
const uint32_t start = 0;
const uint32_t after = kAppleEffaceableNANDOpeningMetaPages;
void * buf = 0;
buf = getBuf(device);
prepOpenCloseMeta(device, kAppleEffaceableNANDMetaOpenMagic, buf);
for (page = start; page < after; page++) {
writePage(device, bank, block, page, buf, getPageSize(device));
}
}
static void
closeBlock(effaceable_device_t * device, uint32_t bank, uint32_t block)
{
uint32_t page;
const uint32_t start = (getPageCount(device) - kAppleEffaceableNANDClosingMetaPages);
const uint32_t after = getPageCount(device);
void * buf = 0;
buf = getBuf(device);
prepOpenCloseMeta(device, kAppleEffaceableNANDMetaCloseMagic, buf);
prepCloseContent(device, buf);
for (page = start; page < after; page++) {
writePage(device, bank, block, page, buf, getPageSize(device));
}
}
static void
prepOpenCloseMeta(effaceable_device_t * device, uint32_t magic, void * buf)
{
AppleEffaceableNANDMeta * meta = (AppleEffaceableNANDMeta*)buf;
whitenMeta(device, meta);
meta->header.magic = magic;
meta->header.version_major = kAppleEffaceableNANDMetaVersionMajorCurrent;
meta->header.version_minor = kAppleEffaceableNANDMetaVersionMinorCurrent;
meta->header.flags = kAppleEffaceableNANDMetaFlagsNone;
}
static void
prepCloseContent(effaceable_device_t * device, void * buf)
{
AppleEffaceableNANDMeta * meta = (AppleEffaceableNANDMeta*)buf;
if (requestPartitionTableDiff(device, meta->content, getMetaContentSize(device))) {
meta->header.flags |= kAppleEffaceableNANDMetaFlagsHasPTabDiff;
debug(INFO, "successfully requested PTabDiff");
} else {
debug(WARN, "unable to request PTabDiff");
}
}
static EffaceableReturn
writeUnit(effaceable_device_t * device, const void * buf, uint32_t group, uint32_t unit)
{
const uint32_t bank = mapGroupToBank(device, group);
const uint32_t block = mapGroupToBlock(device, group);
const uint32_t page = mapUnitToPage(device, unit);
EffaceableReturn ok = kEffaceableReturnError;
if (isBlockBad(device, bank, block)) {
// XXX replace block iff passing below critical good block count threshold
ok = kEffaceableReturnBadMedia;
} else {
if (kEffaceableReturnSuccess == (ok = writePage(device, bank, block, page, buf, getPageSize(device)))) {
// if that was final content page, write block closing meta pages
if ((getUnitsPerGroup(device) - 1) == unit) {
closeBlock(device, bank, block);
}
}
}
return ok;
}
static EffaceableReturn
readUnit(effaceable_device_t * device, void * buf, uint32_t group, uint32_t unit)
{
const uint32_t bank = mapGroupToBank(device, group);
const uint32_t block = mapGroupToBlock(device, group);
const uint32_t page = mapUnitToPage(device, unit);
EffaceableReturn ok;
if (isBlockBad(device, bank, block)) {
// XXX replace block iff passing below critical good block count threshold
ok = kEffaceableReturnBadMedia;
} else {
ok = readPage(device, bank, block, page, buf, getPageSize(device));
}
return ok;
}
static uint32_t
mapGroupToBank(effaceable_device_t * device, uint32_t group)
{
const uint32_t bank = ((uint32_t)group) % getBankCount(device);
// XXX check bounds
return bank;
}
static uint32_t
mapGroupToBlock(effaceable_device_t * device, uint32_t group)
{
const uint32_t block = ((uint32_t)group) / getBankCount(device);
// XXX check bounds
return block;
}
static uint32_t
mapUnitToPage(effaceable_device_t * device, uint32_t unit)
{
const uint32_t page = ((uint32_t)unit) + kAppleEffaceableNANDOpeningMetaPages;
// XXX check bounds
return page;
}
// =============================================================================
static uint32_t
getCopiesSize(effaceable_device_t * device)
{
return getCopySize(device) * getCopiesPerUnit(device);
}
static void
cleanBuffers(effaceable_device_t * device)
{
setMem(device, getBuf(device), 0, getCopiesSize(device));
}
static void
whitenExtra(effaceable_device_t * device, void * buf)
{
// XXX seems like this should be getting called; why isn't it?
const uint32_t copies_size = getCopiesSize(device);
const uint32_t extra_size = getUnitSize(device) - copies_size;
readRandom(device, ((uint8_t*)buf) + copies_size, extra_size);
}
static void
whitenMeta(effaceable_device_t * device, void * buf)
{
readRandom(device, buf, getPageSize(device));
}
static void
foundCurrentCloneInGroup(effaceable_device_t * device, uint32_t group)
{
const uint32_t magic = kAppleEffaceableNANDMetaCloseMagic;
const uint32_t major = kAppleEffaceableNANDMetaVersionMajorPTabDiff;
const uint32_t minor = kAppleEffaceableNANDMetaVersionMinorPTabDiff;
const uint32_t flag = kAppleEffaceableNANDMetaFlagsHasPTabDiff;
const uint32_t bank = mapGroupToBank(device, group);
const uint32_t block = mapGroupToBlock(device, group);
const uint32_t start = (getPageCount(device) - kAppleEffaceableNANDClosingMetaPages);
const uint32_t after = getPageCount(device);
void * buf = getBuf(device);
uint32_t page;
AppleEffaceableNANDMeta * meta;
for (page = start; page < after; page++) {
debug(INFO, "scanning %lu:%lu:%lu for ptab diff", bank, block, page);
if (kEffaceableReturnSuccess == readPage(device, bank, block, page, buf, getPageSize(device))) {
meta = (AppleEffaceableNANDMeta *)buf;
if (magic != meta->header.magic) {
debug(ERR, "bad magic for closing page");
} else if (0 > compareMetaVersions(device, major, minor, meta->header.version_major, meta->header.version_minor)) {
debug(INFO, "meta version precedes ptab diff implementation");
} else if (flag != (flag & meta->header.flags)) {
debug(INFO, "meta doesn't have ptab diff content");
} else if (!providePartitionTableDiff(device, meta->content, getMetaContentSize(device))) {
debug(WARN, "ptab diff content rejected");
} else {
debug(INFO, "successfully provided ptab diff");
}
/* readable closing pages should all be identical; no reason to continue */
break;
}
}
}
static int
compareMetaVersions(effaceable_device_t * device, uint32_t major1, uint32_t minor1, uint32_t major2, uint32_t minor2)
{
if (major1 < major2) {
return -1;
} else if (major1 > major2) {
return 1;
} else if (minor1 < minor2) {
return -1;
} else if (minor1 > minor2) {
return 1;
}
return 0;
}
static uint32_t
getMetaContentSize(effaceable_device_t * device)
{
return (getPageSize(device) - kAppleEffaceableNANDMetaHeaderSize);
}
// -----------------------------------------------------------------------------
static void
initContract(effaceable_device_t * device)
{
// hook required operations
device->getGroupCount = getGroupCount;
device->getUnitsPerGroup = getUnitsPerGroup;
device->getUnitSize = getUnitSize;
device->eraseGroup = eraseGroup;
device->writeUnit = writeUnit;
device->readUnit = readUnit;
// hook optional operations
device->cleanBuffers = cleanBuffers;
device->foundCurrentCloneInGroup = foundCurrentCloneInGroup;
device->updateDynamicProperties = (void *)0;
}
// =============================================================================
static inline effaceable_nand_context_t * context(effaceable_device_t * device)
{
return (effaceable_nand_context_t *)device->opaque;
}
// -----------------------------------------------------------------------------
static inline void * getBuf(effaceable_device_t * device)
{
return context(device)->buf;
}
static inline void * setBuf(effaceable_device_t * device, void * buf)
{
return context(device)->buf = buf;
}
// -----------------------------------------------------------------------------
static inline uint32_t getCopiesPerUnit(effaceable_device_t * device)
{
return delegateFn(device, storage, getCopiesPerUnit);
}
static inline uint32_t getCopySize(effaceable_device_t * device)
{
return delegateFn(device, storage, getCopySize);
}
// -----------------------------------------------------------------------------
static inline uint32_t getPageSize(effaceable_device_t * device)
{
return delegateFn(device, nand, getPageSize);
}
static inline uint32_t getPageCount(effaceable_device_t * device)
{
return delegateFn(device, nand, getPageCount);
}
static inline uint32_t getBlockCount(effaceable_device_t * device)
{
return delegateFn(device, nand, getBlockCount);
}
static inline uint32_t getBankCount(effaceable_device_t * device)
{
return delegateFn(device, nand, getBankCount);
}
static inline bool isBlockBad(effaceable_device_t * device, uint32_t bank, uint32_t block)
{
return delegateFn(device, nand, isBlockBad, bank, block);
}
static inline EffaceableReturn eraseBlock(effaceable_device_t * device, uint32_t bank, uint32_t block)
{
return delegateFn(device, nand, eraseBlock, bank, block);
}
static inline EffaceableReturn writePage(effaceable_device_t * device, uint32_t bank, uint32_t block, uint32_t page, const void * buf, uint32_t length)
{
return delegateFn(device, nand, writePage, bank, block, page, buf, length);
}
static inline EffaceableReturn readPage(effaceable_device_t * device, uint32_t bank, uint32_t block, uint32_t page, void * buf, uint32_t length)
{
return delegateFn(device, nand, readPage, bank, block, page, buf, length);
}
static inline bool requestPartitionTableDiff(effaceable_device_t * device, void * buf, uint32_t length)
{
return delegateFn(device, nand, requestPartitionTableDiff, buf, length);
}
static inline bool providePartitionTableDiff(effaceable_device_t * device, void * buf, uint32_t length)
{
return delegateFn(device, nand, providePartitionTableDiff, buf, length);
}
// -----------------------------------------------------------------------------
static inline void * allocMem(effaceable_device_t * device, uint32_t size)
{
return delegateFn(device, system, allocMem, size);
}
static inline void freeMem(effaceable_device_t * device, void * buf, uint32_t size)
{
delegateFn(device, system, freeMem, buf, size);
}
static inline void * setMem(effaceable_device_t * device, void * buf, uint8_t val, uint32_t size)
{
return delegateFn(device, system, setMem, buf, val, size);
}
static inline void * moveMem(effaceable_device_t * device, void * dst, const void * src, uint32_t size)
{
return delegateFn(device, system, moveMem, dst, src, size);
}
static inline int cmpMem(effaceable_device_t * device, const void * lhs, const void * rhs, uint32_t size)
{
return delegateFn(device, system, cmpMem, lhs, rhs, size);
}
static inline void readRandom(effaceable_device_t * device, void * buf, uint32_t size)
{
delegateFn(device, system, readRandom, buf, size);
}
static inline uint32_t crc32Sys(effaceable_device_t * device, uint32_t crc, const void * buf, uint32_t size, bool finish)
{
return delegateFn(device, system, crc32, crc, buf, size, finish);
}
static inline bool setPropertySys(effaceable_device_t * device, const char * key, uint32_t value)
{
return delegateFn(device, system, setProperty, key, value);
}
static inline void panicSys(effaceable_device_t * device, const char * msg)
{
delegateFn(device, system, panicSys, msg);
}
static int logf(effaceable_device_t * device, const char * fmt, ...)
{
int err;
va_list ap;
va_start(ap, fmt);
err = delegateFn(device, system, vlogf, fmt, ap);
va_end(ap);
return err;
}
// -----------------------------------------------------------------------------
static void
initContext(effaceable_device_t * device, effaceable_system_t * system, effaceable_nand_hal_t * nand, effaceable_storage_t * storage)
{
device->opaque = system->allocMem(system, sizeof(effaceable_nand_context_t));
system->setMem(system, context(device), 0, sizeof(effaceable_nand_context_t));
context(device)->storage = storage;
context(device)->system = system;
context(device)->nand = nand;
}
// =============================================================================
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2010-11 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_NAND_CORE_H_
#define _EFFACEABLE_NAND_CORE_H_
// =============================================================================
// reserve pages at beginning and end of block for future metadata
#define kAppleEffaceableNANDOpeningMetaPages 16
#define kAppleEffaceableNANDClosingMetaPages 16
#define kAppleEffaceableNANDTotalMetaPages (kAppleEffaceableNANDOpeningMetaPages + kAppleEffaceableNANDClosingMetaPages)
#define kAppleEffaceableNANDMetaOpenMagic 'open'
#define kAppleEffaceableNANDMetaCloseMagic 'clos'
#define kAppleEffaceableNANDMetaVersionMajorUnknown ((uint32_t)0x00000000)
#define kAppleEffaceableNANDMetaVersionMajorInitial ((uint32_t)0x00000001)
#define kAppleEffaceableNANDMetaVersionMajorBungled ((uint32_t)0x00000002)
#define kAppleEffaceableNANDMetaVersionMajorPTabDiff ((uint32_t)0x00000002)
#define kAppleEffaceableNANDMetaVersionMajorCurrent kAppleEffaceableNANDMetaVersionMajorPTabDiff
#define kAppleEffaceableNANDMetaVersionMajorInvalid ((uint32_t)0xFFFFFFFF)
#define kAppleEffaceableNANDMetaVersionMinorUnknown ((uint32_t)0x00000000)
#define kAppleEffaceableNANDMetaVersionMinorInitial ((uint32_t)0x00000001)
#define kAppleEffaceableNANDMetaVersionMinorBungled ((uint32_t)0x00000002)
#define kAppleEffaceableNANDMetaVersionMinorPTabDiff ((uint32_t)0x00000003)
#define kAppleEffaceableNANDMetaVersionMinorCurrent kAppleEffaceableNANDMetaVersionMinorPTabDiff
#define kAppleEffaceableNANDMetaVersionMinorInvalid ((uint32_t)0xFFFFFFFF)
#define kAppleEffaceableNANDMetaFlagsNone ((uint32_t)0x00000000)
#define kAppleEffaceableNANDMetaFlagsHasPTabDiff ((uint32_t)0x00000001)
// =============================================================================
// All structures defined here are on-media formats.
typedef struct
{
uint32_t magic;
uint32_t version_major;
uint32_t version_minor;
uint32_t flags;
} __attribute__((__packed__)) AppleEffaceableNANDMetaHeader;
#define kAppleEffaceableNANDMetaHeaderSize sizeof(AppleEffaceableNANDMetaHeader)
typedef struct
{
AppleEffaceableNANDMetaHeader header;
uint8_t content[0];
} __attribute__((__packed__)) AppleEffaceableNANDMeta;
// =============================================================================
__BEGIN_DECLS
// -----------------------------------------------------------------------------
static uint32_t getGroupCount(effaceable_device_t * device);
static uint32_t getUnitsPerGroup(effaceable_device_t * device);
static uint32_t getUnitSize(effaceable_device_t * device);
static EffaceableReturn eraseGroup(effaceable_device_t * device, uint32_t group);
static EffaceableReturn writeUnit(effaceable_device_t * device, const void * buf, uint32_t group, uint32_t unit);
static EffaceableReturn readUnit(effaceable_device_t * device, void * buf, uint32_t group, uint32_t unit);
// -----------------------------------------------------------------------------
static void openBlock(effaceable_device_t * device, uint32_t bank, uint32_t block);
static void closeBlock(effaceable_device_t * device, uint32_t bank, uint32_t block);
static void prepOpenCloseMeta(effaceable_device_t * device, uint32_t magic, void * buf);
static void prepCloseContent(effaceable_device_t * device, void * buf);
// -----------------------------------------------------------------------------
static uint32_t mapGroupToBank(effaceable_device_t * device, uint32_t group);
static uint32_t mapGroupToBlock(effaceable_device_t * device, uint32_t group);
static uint32_t mapUnitToPage(effaceable_device_t * device, uint32_t unit);
// -----------------------------------------------------------------------------
static uint32_t getCopiesSize(effaceable_device_t * device);
static void cleanBuffers(effaceable_device_t * device);
static void whitenExtra(effaceable_device_t * device, void * buf);
static void whitenMeta(effaceable_device_t * device, void * buf);
static void foundCurrentCloneInGroup(effaceable_device_t * device, uint32_t group);
static int compareMetaVersions(effaceable_device_t * device, uint32_t major1, uint32_t minor1, uint32_t major2, uint32_t minor2);
static uint32_t getMetaContentSize(effaceable_device_t * device);
// -----------------------------------------------------------------------------
static void initContract(effaceable_device_t * device);
// =============================================================================
static inline void * getBuf(effaceable_device_t * device);
static inline void * setBuf(effaceable_device_t * device, void * buf);
// -----------------------------------------------------------------------------
static inline uint32_t getCopiesPerUnit(effaceable_device_t * device);
static inline uint32_t getCopySize(effaceable_device_t * device);
// -----------------------------------------------------------------------------
static inline uint32_t getPageSize(effaceable_device_t * device);
static inline uint32_t getPageCount(effaceable_device_t * device);
static inline uint32_t getBlockCount(effaceable_device_t * device);
static inline uint32_t getBankCount(effaceable_device_t * device);
static inline bool isBlockBad(effaceable_device_t * device, uint32_t bank, uint32_t block);
static inline EffaceableReturn eraseBlock(effaceable_device_t * device, uint32_t bank, uint32_t block);
static inline EffaceableReturn writePage(effaceable_device_t * device, uint32_t bank, uint32_t block, uint32_t page, const void * buf, uint32_t length);
static inline EffaceableReturn readPage(effaceable_device_t * device, uint32_t bank, uint32_t block, uint32_t page, void * buf, uint32_t length);
static inline bool requestPartitionTableDiff(effaceable_device_t * device, void * buf, uint32_t length);
static inline bool providePartitionTableDiff(effaceable_device_t * device, void * buf, uint32_t length);
// -----------------------------------------------------------------------------
static inline void * allocMem(effaceable_device_t * device, uint32_t size);
static inline void freeMem(effaceable_device_t * device, void * buf, uint32_t size);
static inline void * setMem(effaceable_device_t * device, void * buf, uint8_t val, uint32_t size);
static inline void * moveMem(effaceable_device_t * device, void * dst, const void * src, uint32_t size);
static inline int cmpMem(effaceable_device_t * device, const void * lhs, const void * rhs, uint32_t size);
static inline void readRandom(effaceable_device_t * device, void * buf, uint32_t size);
static inline uint32_t crc32Sys(effaceable_device_t * device, uint32_t crc, const void * buf, uint32_t size, bool finish);
static inline bool setPropertySys(effaceable_device_t * device, const char * key, uint32_t value);
static inline void panicSys(effaceable_device_t * device, const char * msg);
static int logf(effaceable_device_t * device, const char * fmt, ...);
// -----------------------------------------------------------------------------
static void initContext(effaceable_device_t * device, effaceable_system_t * system, effaceable_nand_hal_t * nand, effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
__END_DECLS
// =============================================================================
#endif /* _EFFACEABLE_NAND_CORE_H_ */
+214
View File
@@ -0,0 +1,214 @@
/*
* Copyright (c) 2010-11 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
// =============================================================================
#include <effaceable_contract.h>
#include <effaceable_debug.h>
#include "effaceable_private_types.h"
#include "effaceable_storage_dev.h"
#include "effaceable_nor_core.h"
#include "effaceable_delegation.h"
// =============================================================================
#define debug(...) dlogf(device, __VA_ARGS__ )
// =============================================================================
extern void
startEffaceableNOR(effaceable_device_t * device, effaceable_system_t * system, effaceable_nor_hal_t * nor, effaceable_storage_t * storage)
{
initContext(device, system, nor, storage);
initContract(device);
startEffaceableStorage(storage, device, system);
}
// =============================================================================
static uint32_t
getGroupCount(effaceable_device_t * device)
{
return getRegionCount(device);
}
static uint32_t
getUnitsPerGroup(effaceable_device_t * device)
{
return 1;
}
static uint32_t
getUnitSize(effaceable_device_t * device)
{
return getRegionSize(device, 0);
}
static EffaceableReturn
eraseGroup(effaceable_device_t * device, uint32_t group)
{
return eraseRegion(device, group, getUnitSize(device));
}
static EffaceableReturn
writeUnit(effaceable_device_t * device, const void * buf, uint32_t group, uint32_t unit)
{
// XXX assert unit == 0
return writeRegion(device, group, buf, getUnitSize(device));
}
static EffaceableReturn
readUnit(effaceable_device_t * device, void * buf, uint32_t group, uint32_t unit)
{
// XXX assert unit == 0
return readRegion(device, group, buf, getUnitSize(device));
}
// =============================================================================
static void
initContract(effaceable_device_t * device)
{
// hook required operations
device->getGroupCount = getGroupCount;
device->getUnitsPerGroup = getUnitsPerGroup;
device->getUnitSize = getUnitSize;
device->eraseGroup = eraseGroup;
device->writeUnit = writeUnit;
device->readUnit = readUnit;
// hook optional operations
device->cleanBuffers = (void *)0;
device->foundCurrentCloneInGroup = (void *)0;
device->updateDynamicProperties = (void *)0;
}
// =============================================================================
static inline effaceable_nor_context_t * context(effaceable_device_t * device)
{
return (effaceable_nor_context_t *)device->opaque;
}
// -----------------------------------------------------------------------------
static inline uint32_t getCopiesPerUnit(effaceable_device_t * device)
{
return delegateFn(device, storage, getCopiesPerUnit);
}
static inline uint32_t getCopySize(effaceable_device_t * device)
{
return delegateFn(device, storage, getCopySize);
}
// -----------------------------------------------------------------------------
static inline uint32_t getRegionCount(effaceable_device_t * device)
{
return delegateFn(device, nor, getRegionCount);
}
static inline uint32_t getRegionSize(effaceable_device_t * device, uint32_t index)
{
return delegateFn(device, nor, getRegionSize, index);
}
static inline EffaceableReturn eraseRegion(effaceable_device_t * device, uint32_t index, uint32_t length)
{
return delegateFn(device, nor, eraseRegion, index, length);
}
static inline uint32_t writeRegion(effaceable_device_t * device, uint32_t index, const void * buf, uint32_t length)
{
return delegateFn(device, nor, writeRegion, index, buf, length);
}
static inline bool readRegion(effaceable_device_t * device, uint32_t index, void * buf, uint32_t length)
{
return delegateFn(device, nor, readRegion, index, buf, length);
}
// -----------------------------------------------------------------------------
static inline void * allocMem(effaceable_device_t * device, uint32_t size)
{
return delegateFn(device, system, allocMem, size);
}
static inline void freeMem(effaceable_device_t * device, void * buf, uint32_t size)
{
delegateFn(device, system, freeMem, buf, size);
}
static inline void * setMem(effaceable_device_t * device, void * buf, uint8_t val, uint32_t size)
{
return delegateFn(device, system, setMem, buf, val, size);
}
static inline void * moveMem(effaceable_device_t * device, void * dst, const void * src, uint32_t size)
{
return delegateFn(device, system, moveMem, dst, src, size);
}
static inline int cmpMem(effaceable_device_t * device, const void * lhs, const void * rhs, uint32_t size)
{
return delegateFn(device, system, cmpMem, lhs, rhs, size);
}
static inline void readRandom(effaceable_device_t * device, void * buf, uint32_t size)
{
delegateFn(device, system, readRandom, buf, size);
}
static inline uint32_t crc32Sys(effaceable_device_t * device, uint32_t crc, const void * buf, uint32_t size, bool finish)
{
return delegateFn(device, system, crc32, crc, buf, size, finish);
}
static inline bool setPropertySys(effaceable_device_t * device, const char * key, uint32_t value)
{
return delegateFn(device, system, setProperty, key, value);
}
static inline void panicSys(effaceable_device_t * device, const char * msg)
{
delegateFn(device, system, panicSys, msg);
}
static int logf(effaceable_device_t * device, const char * fmt, ...)
{
int err;
va_list ap;
va_start(ap, fmt);
err = delegateFn(device, system, vlogf, fmt, ap);
va_end(ap);
return err;
}
// =============================================================================
static void
initContext(effaceable_device_t * device, effaceable_system_t * system, effaceable_nor_hal_t * nor, effaceable_storage_t * storage)
{
device->opaque = system->allocMem(system, sizeof(effaceable_nor_context_t));
system->setMem(system, context(device), 0, sizeof(effaceable_nor_context_t));
context(device)->system = system;
context(device)->nor = nor;
context(device)->storage = storage;
}
// =============================================================================
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2010-11 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_NOR_CORE_H_
#define _EFFACEABLE_NOR_CORE_H_
// =============================================================================
__BEGIN_DECLS
// -----------------------------------------------------------------------------
static uint32_t getGroupCount(effaceable_device_t * device);
static uint32_t getUnitsPerGroup(effaceable_device_t * device);
static uint32_t getUnitSize(effaceable_device_t * device);
static EffaceableReturn eraseGroup(effaceable_device_t * device, uint32_t group);
static EffaceableReturn writeUnit(effaceable_device_t * device, const void * buf, uint32_t group, uint32_t unit);
static EffaceableReturn readUnit(effaceable_device_t * device, void * buf, uint32_t group, uint32_t unit);
// -----------------------------------------------------------------------------
static void initContract(effaceable_device_t * device);
// -----------------------------------------------------------------------------
static inline uint32_t getCopiesPerUnit(effaceable_device_t * device);
static inline uint32_t getCopySize(effaceable_device_t * device);
// -----------------------------------------------------------------------------
static inline uint32_t getRegionCount(effaceable_device_t * device);
static inline uint32_t getRegionSize(effaceable_device_t * device, uint32_t index);
static inline EffaceableReturn eraseRegion(effaceable_device_t * device, uint32_t index, uint32_t length);
static inline uint32_t writeRegion(effaceable_device_t * device, uint32_t index, const void * buf, uint32_t length);
static inline bool readRegion(effaceable_device_t * device, uint32_t index, void * buf, uint32_t length);
// -----------------------------------------------------------------------------
static inline void * allocMem(effaceable_device_t * device, uint32_t size);
static inline void freeMem(effaceable_device_t * device, void * buf, uint32_t size);
static inline void * setMem(effaceable_device_t * device, void * buf, uint8_t val, uint32_t size);
static inline void * moveMem(effaceable_device_t * device, void * dst, const void * src, uint32_t size);
static inline int cmpMem(effaceable_device_t * device, const void * lhs, const void * rhs, uint32_t size);
static inline void readRandom(effaceable_device_t * device, void * buf, uint32_t size);
static inline uint32_t crc32Sys(effaceable_device_t * device, uint32_t crc, const void * buf, uint32_t size, bool finish);
static inline bool setPropertySys(effaceable_device_t * device, const char * key, uint32_t value);
static inline void panicSys(effaceable_device_t * device, const char * msg);
static int logf(effaceable_device_t * device, const char * fmt, ...);
// -----------------------------------------------------------------------------
static void initContext(effaceable_device_t * device, effaceable_system_t * system, effaceable_nor_hal_t * nor, effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
__END_DECLS
// =============================================================================
#endif /* _EFFACEABLE_NOR_CORE_H_ */
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_PRIVATE_TYPES_H_
#define _EFFACEABLE_PRIVATE_TYPES_H_
// =============================================================================
#include <AppleEffaceableStorageFormat.h>
#include "effaceable_locker.h"
#include "effaceable_context.h"
// =============================================================================
// grrrr
#ifndef NULL
#define NULL ((void*)0)
#endif
// -----------------------------------------------------------------------------
#define min(a,b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})
// =============================================================================
#endif /* _EFFACEABLE_PRIVATE_TYPES_H_ */
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2010-11 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_STORAGE_CORE_H_
#define _EFFACEABLE_STORAGE_CORE_H_
// =============================================================================
__BEGIN_DECLS
// -----------------------------------------------------------------------------
static void updateInitialProperties(effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
static uint32_t extGetCopiesPerUnit(effaceable_storage_t * storage);
static uint32_t extGetCopySize(effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
static uint32_t getCopiesPerUnit(effaceable_storage_t * storage);
static uint32_t getCopySize(effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
static EffaceableReturn getLocker(effaceable_storage_t * storage, uint32_t type_id, void *data, uint32_t *data_size, bool untrusted);
static EffaceableReturn getLockerExternal(effaceable_storage_t * storage, uint32_t type_id, void *data, uint32_t *data_size, bool untrusted);
static EffaceableReturn getLockerInternal(effaceable_storage_t * storage, uint32_t type_id, void *data, uint32_t *data_size, bool untrusted, bool notprivate);
static EffaceableReturn setLocker(effaceable_storage_t * storage, uint32_t type_id, const void *data, uint32_t data_size, bool untrusted);
static EffaceableReturn setLockerExternal(effaceable_storage_t * storage, uint32_t type_id, const void *data, uint32_t data_size, bool untrusted);
static EffaceableReturn setLockerInternal(effaceable_storage_t * storage, uint32_t type_id, const void *data, uint32_t data_size, bool untrusted, bool notprivate);
static EffaceableReturn spaceForLocker(effaceable_storage_t * storage, uint32_t type_id, uint32_t *data_space);
static EffaceableReturn effaceLocker(effaceable_storage_t * storage, uint32_t type_id, bool untrusted);
static EffaceableReturn effaceLockerExternal(effaceable_storage_t * storage, uint32_t type_id, bool untrusted);
static EffaceableReturn effaceLockerInternal(effaceable_storage_t * storage, uint32_t type_id, bool untrusted, bool notprivate);
static EffaceableReturn getBytes(effaceable_storage_t * storage, void * client_buf, uint32_t offset, uint32_t count);
static EffaceableReturn wipeStorage(effaceable_storage_t * storage, void * tmp_buf);
static uint32_t getCapacity(effaceable_storage_t * storage);
static bool isFormatted(effaceable_storage_t * storage);
static EffaceableReturn formatStorage(effaceable_storage_t * storage);
static EffaceableReturn generateNonce(effaceable_storage_t * storage, void * hash);
static EffaceableReturn consumeNonce(effaceable_storage_t * storage, uint64_t * nonce);
// -----------------------------------------------------------------------------
static EffaceableReturn eraseGroupLogged(effaceable_storage_t * storage, uint32_t group);
static EffaceableReturn writeUnitLogged(effaceable_storage_t * storage, const void * io_buf, uint32_t group, uint32_t unit);
static EffaceableReturn readUnitLogged(effaceable_storage_t * storage, void * io_buf, uint32_t group, uint32_t unit);
// -----------------------------------------------------------------------------
static void findCurrentClone(effaceable_storage_t * storage, void * tmp_buf, void * scan_buf, void * clone_buf);
static bool isGroupEffaced(effaceable_storage_t * storage, void * tmp_buf, uint32_t group);
static EffaceableReturn obscureCopy(effaceable_storage_t * storage, void * clear_buf, void * crypt_buf, uint32_t copy);
static EffaceableReturn revealCopy(effaceable_storage_t * storage, void * crypt_buf, void * clear_buf, uint32_t copy);
static EffaceableReturn commitClone(effaceable_storage_t * storage, void * clone_buf, uint32_t group);
static EffaceableReturn writeClone(effaceable_storage_t * storage, void * tmp_buf, void * clone_buf, uint32_t group, uint32_t generation);
static uint32_t checksumCopy(effaceable_storage_t * storage, void * clone_buf);
static bool isCopyValid(effaceable_storage_t * storage, const void * clone_buf);
static EffaceableReturn confirmClone(effaceable_storage_t * storage, void * tmp_buf, void * clone_buf, uint32_t group);
// -----------------------------------------------------------------------------
static uint32_t nextGroup(effaceable_storage_t * storage, uint32_t group);
static EffaceableReturn effaceGroup(effaceable_storage_t * storage, void * tmp_buf, uint32_t group);
// -----------------------------------------------------------------------------
static locker_t * findLockerForRequest(effaceable_storage_t * storage, uint32_t type_id);
static EffaceableReturn discardLockers(effaceable_storage_t * storage);
static void appendSentinel(effaceable_storage_t * storage);
static locker_t * findLockerForRequest(effaceable_storage_t * storage, uint32_t type_id);
static EffaceableReturn getLockersFromStorage(effaceable_storage_t * storage);
static EffaceableReturn putLockersInStorage(effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
static void cleanBuffers(effaceable_storage_t * storage);
static void foundCurrentCloneInGroup(effaceable_storage_t * storage, uint32_t group);
static void updateDynamicProperties(effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
static void initContract(effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
static inline void * getCache(effaceable_storage_t * storage);
static inline void * setCache(effaceable_storage_t * storage, void * buf);
static inline void * getScratch(effaceable_storage_t * storage);
static inline void * setScratch(effaceable_storage_t * storage, void * buf);
static inline void * getSniff(effaceable_storage_t * storage);
static inline void * setSniff(effaceable_storage_t * storage, void * buf);
static inline void * getCrypt(effaceable_storage_t * storage);
static inline void * setCrypt(effaceable_storage_t * storage, void * buf);
static inline void * getNonceHash(effaceable_storage_t * storage);
static inline void * setNonceHash(effaceable_storage_t * storage, void * buf);
static inline uint32_t getGeneration(effaceable_storage_t * storage);
static inline uint32_t setGeneration(effaceable_storage_t * storage, uint32_t generation);
static inline uint32_t getGroup(effaceable_storage_t * storage);
static inline uint32_t setGroup(effaceable_storage_t * storage, uint32_t group);
static inline bool getIsFormatted(effaceable_storage_t * storage);
static inline bool setIsFormatted(effaceable_storage_t * storage, bool is_formatted);
static inline bool getFullScanEnabled(effaceable_storage_t * storage);
static inline bool setFullScanEnabled(effaceable_storage_t * storage, bool enable);
static inline bool getWipeEnabled(effaceable_storage_t * storage);
static inline bool setWipeEnabled(effaceable_storage_t * storage, bool enable);
static inline locker_list_t * getLockerList(effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
static inline uint32_t getGroupCount(effaceable_storage_t * storage);
static inline uint32_t getUnitsPerGroup(effaceable_storage_t * storage);
static inline uint32_t getUnitSize(effaceable_storage_t * storage);
static inline bool eraseGroup(effaceable_storage_t * storage, uint32_t group);
static inline bool writeUnit(effaceable_storage_t * storage, const void * buf, uint32_t group, uint32_t unit);
static inline bool readUnit(effaceable_storage_t * storage, void * buf, uint32_t group, uint32_t unit);
// -----------------------------------------------------------------------------
static inline void deviceCleanBuffers(effaceable_storage_t * storage);
static inline void deviceFoundCurrentCloneInGroup(effaceable_storage_t * storage, uint32_t group);
static inline void deviceUpdateDynamicProperties(effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
static inline void * allocMem(effaceable_storage_t * storage, uint32_t size);
static inline void freeMem(effaceable_storage_t * storage, void * buf, uint32_t size);
static inline void * setMem(effaceable_storage_t * storage, void * buf, uint8_t val, uint32_t size);
static inline void * moveMem(effaceable_storage_t * storage, void * dst, const void * src, uint32_t size);
static inline int cmpMem(effaceable_storage_t * storage, const void * lhs, const void * rhs, uint32_t size);
static inline void readRandom(effaceable_storage_t * storage, void * buf, uint32_t size);
static inline void calcSHA1(effaceable_storage_t * storage, const void * buf, uint32_t size, void * hash);
static inline uint32_t crc32Sys(effaceable_storage_t * storage, uint32_t crc, const void * buf, uint32_t size, bool finish);
static inline bool setPropertySys(effaceable_storage_t * storage, const char * key, uint32_t value);
static inline void panicSys(effaceable_storage_t * storage, const char * msg);
static int logf(effaceable_storage_t * storage, const char * fmt, ...);
// -----------------------------------------------------------------------------
static void initContext(effaceable_storage_t * storage, effaceable_device_t * device, effaceable_system_t * system);
// -----------------------------------------------------------------------------
__END_DECLS
// =============================================================================
#endif /* _EFFACEABLE_STORAGE_CORE_H_ */
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
// XXX find a better name for this file. perhaps 'effaceable_storage_entry.h'?
#ifndef _EFFACEABLE_STORAGE_DEV_H_
#define _EFFACEABLE_STORAGE_DEV_H_
// =============================================================================
__BEGIN_DECLS
// -----------------------------------------------------------------------------
extern void startEffaceableStorage(effaceable_storage_t * storage, effaceable_device_t * device, effaceable_system_t * system);
// -----------------------------------------------------------------------------
__END_DECLS
// =============================================================================
#endif /* _EFFACEABLE_STORAGE_DEV_H_ */
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2010-11 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _APPLE_EFFACEABLE_STORAGE_FORMAT_H
#define _APPLE_EFFACEABLE_STORAGE_FORMAT_H
// =============================================================================
#include <stdint.h>
// =============================================================================
#define kAppleEffaceableStorageMagic 'Face'
#define kAppleEffaceableStorageGenerationUnknown ((uint32_t)0x00000000)
#define kAppleEffaceableStorageGenerationInitial ((uint32_t)0x00000001)
#define kAppleEffaceableStorageGenerationInvalid ((uint32_t)0xFFFFFFFF)
#define kAppleEffaceableStorageVersionMajorUnknown ((uint32_t)0x00000000)
#define kAppleEffaceableStorageVersionMajorInitial ((uint32_t)0x00000001)
#define kAppleEffaceableStorageVersionMajorNonce ((uint32_t)0x00000001)
#define kAppleEffaceableStorageVersionMajorCurrent kAppleEffaceableStorageVersionMajorNonce
#define kAppleEffaceableStorageVersionMajorInvalid ((uint32_t)0xFFFFFFFF)
#define kAppleEffaceableStorageVersionMinorUnknown ((uint32_t)0x00000000)
#define kAppleEffaceableStorageVersionMinorInitial ((uint32_t)0x00000001)
#define kAppleEffaceableStorageVersionMinorNonce ((uint32_t)0x00000002)
#define kAppleEffaceableStorageVersionMinorCurrent kAppleEffaceableStorageVersionMajorNonce
#define kAppleEffaceableStorageVersionMinorInvalid ((uint32_t)0xFFFFFFFF)
#define kAppleEffaceableStorageFlagsNone ((uint32_t)0x00000000)
#define kAppleEffaceableStorageSize 1024
#define kAppleEffaceableStorageSaltSize 16
#define kAppleEffaceableStorageReservedSize 24
#define kAppleEffaceableStorageContentSize (kAppleEffaceableStorageSize - sizeof(AppleEffaceableStorageHeader))
#define kAppleEffaceableStorageLockerBase 0
#define kAppleEffaceableStorageLockerSize (kAppleEffaceableStorageContentSize - kAppleEffaceableStorageLockerBase)
// =============================================================================
// All structures defined here are on-media formats.
typedef struct
{
uint32_t magic;
uint32_t version_major;
uint32_t version_minor;
uint32_t flags;
} __attribute__((__packed__)) AppleEffaceableStorageNaked;
typedef struct
{
uint8_t reserved[kAppleEffaceableStorageReservedSize];
uint32_t generation;
uint32_t checksum;
} __attribute__((__packed__)) AppleEffaceableStorageVeiled;
typedef struct
{
AppleEffaceableStorageNaked naked;
uint8_t salt[kAppleEffaceableStorageSaltSize];
AppleEffaceableStorageVeiled veiled;
} __attribute__((__packed__)) AppleEffaceableStorageHeader;
typedef struct
{
AppleEffaceableStorageHeader header;
uint8_t content[kAppleEffaceableStorageContentSize];
} __attribute__((__packed__)) AppleEffaceableStorageClone;
typedef struct
{
uint16_t magic;
uint16_t data_size;
uint32_t type_id;
} __attribute__((__packed__)) AppleEffaceableStorageLockerHeader;
#define kAppleEffaceableStorageLockerMagic 0x4c6b // 'Lk'
#define kAppleEffaceableStorageLockerSentinel 'DONE'
// =============================================================================
#endif /* _APPLE_EFFACEABLE_STORAGE_FORMAT_H */
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2010-11 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _APPLE_EFFACEABLE_STORAGE_KEYS_H
#define _APPLE_EFFACEABLE_STORAGE_KEYS_H
__BEGIN_DECLS
// =============================================================================
// callPlatformFunction methods
//
// GetLocker
// Returns the contents of the locker type_id, if it exists.
//
// SetLocker
// Updates or creates the locker type_id.
//
// Space
// Returns the largest amount of data that could be stored in a locker
// with type_id, accounting for overwriting any current value.
//
// Efface
// Destroys the locker type_id and all its contents.
//
// GenerateNonce
// Populate private nonce locker with random 64-bit value and return its hash.
//
// type_id
// Unique identifier for the locker. Typically a four-byte ASCII value.
//
// data
// Pointer to read/write data buffer.
//
// data_size
// Buffer/operation size in bytes. Note that for GetLocker this is a
// pointer to an IOByteCount that is updated to the actual size of the
// locker, and for Space this is the amount of space available for
// a locker with type_id.
//
// untrusted
// True if the client is untrusted and should not be allowed to operate
// on protected lockers.
//
// hash
// Pointer to 20 byte buffer into which the nonce hash will be copied.
//
#define kAppleEffaceableStorageFunctionGet "GetLocker" // (type_id, *data, *data_size, untrusted)
#define kAppleEffaceableStorageFunctionSet "SetLocker" // (type_id, *data, data_size, untrusted)
#define kAppleEffaceableStorageFunctionEfface "EffaceLocker" // (type_id, untrusted)
#define kAppleEffaceableStorageFunctionGenerateNonce "GenerateNonce" // (*hash)
#define kAppleEffaceableStorageFunctionSpace "Space" // (type_id, *free_size)
// When set on a request's type_id, the locker can only be read, overwritten and
// effaced by a kernel client. Cannot be set by a user client.
#define kAppleEffaceableStorageLockerProtected (1<<31)
// Special type for manipulating nonce locker, which is permitted
// only by the effaceable storage driver
#define kAppleEffaceableStorageLockerPrivate (1<<23)
// These bits must not be set in a locker's type_id.
#define kAppleEffaceableStorageLockerReserved ((1<<15) | (1<<7))
// This type_id is considered a wildcard; passed to Efface it will cause
// every locker to be effaced.
#define kAppleEffaceableStorageLockerWildcard '****'
// =============================================================================
#define kAppleEffaceableStorageClassName "AppleEffaceableStorage"
// DeviceTree properties
#define kAppleEffaceableStoragePropertyVersionMajor "VersionMajor"
#define kAppleEffaceableStoragePropertyVersionMinor "VersionMinor"
#define kAppleEffaceableStoragePropertyIsFormatted "IsFormatted"
// UserClient methods
enum _AppleEffaceableStorageMethod
{
// Maintenance interface.
kAppleEffaceableStorageMethodGetCapacity,
kAppleEffaceableStorageMethodGetBytes,
kAppleEffaceableStorageMethodSetBytes,
kAppleEffaceableStorageMethodIsFormatted,
kAppleEffaceableStorageMethodFormatStorage,
// Userclient locker methods.
//
// The type_id argument is always passed in scalarInput[0].
//
// GetLocker returns the actual size of the locker in scalarOutput[0],
// and if structureOutput/structureOutputSize describe a buffer, as much
// of the locker data as will fit is returned into the buffer.
//
// SetLocker creates or replaces an existing locker with the contents of
// structureInput/structureInputSize.
//
// EffaceLocker destroys the locker.
//
kAppleEffaceableStorageMethodGetLocker,
kAppleEffaceableStorageMethodSetLocker,
kAppleEffaceableStorageMethodEffaceLocker,
kAppleEffaceableStorageMethodSpace,
// Debug-only interface.
kAppleEffaceableStorageMethodWipeStorage,
// Nonce support
kAppleEffaceableStorageMethodGenerateNonce,
kAppleEffaceableStorageMethodCount
};
typedef enum _AppleEffaceableStorageMethod AppleEffaceableStorageMethod;
// =============================================================================
__END_DECLS
#endif /* _APPLE_EFFACEABLE_STORAGE_KEYS_H */
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_CONTRACT_H_
#define _EFFACEABLE_CONTRACT_H_
// =============================================================================
#include <sys/types.h>
#include <stdarg.h>
/* horrible that this is necessary */
#ifndef __cplusplus
# ifndef false
# include <stdbool.h>
# endif
#endif
// =============================================================================
enum _EffaceableReturn
{
kEffaceableReturnSuccess = 0,
kEffaceableReturnError,
kEffaceableReturnNotFound,
kEffaceableReturnUnsupported,
kEffaceableReturnNotPrivileged,
kEffaceableReturnNotPermitted,
kEffaceableReturnBadArgument,
kEffaceableReturnBadMedia,
kEffaceableReturnUnformattedMedia,
kEffaceableReturnIOError,
kEffaceableReturnNoMemory,
kEffaceableReturnNoSpace,
kEffaceableReturnInternalError,
kEffaceableReturnInvalid
};
typedef enum _EffaceableReturn EffaceableReturn;
// =============================================================================
struct _effaceable_storage;
struct _effaceable_device;
struct _effaceable_system;
struct _effaceable_nor_hal;
struct _effaceable_nand_hal;
// -----------------------------------------------------------------------------
typedef struct _effaceable_storage effaceable_storage_t;
typedef struct _effaceable_device effaceable_device_t;
typedef struct _effaceable_system effaceable_system_t;
typedef struct _effaceable_nor_hal effaceable_nor_hal_t;
typedef struct _effaceable_nand_hal effaceable_nand_hal_t;
// =============================================================================
struct _effaceable_storage
{
void * opaque;
/* client api for use externally */
EffaceableReturn (* getLocker )(effaceable_storage_t * storage, uint32_t type_id, void *data, uint32_t *data_size, bool untrusted);
EffaceableReturn (* setLocker )(effaceable_storage_t * storage, uint32_t type_id, const void *data, uint32_t data_size, bool untrusted);
EffaceableReturn (* spaceForLocker )(effaceable_storage_t * storage, uint32_t type_id, uint32_t *data_space);
EffaceableReturn (* effaceLocker )(effaceable_storage_t * storage, uint32_t type_id, bool untrusted);
EffaceableReturn (* generateNonce )(effaceable_storage_t * storage, void * hash);
EffaceableReturn (* consumeNonce )(effaceable_storage_t * storage, uint64_t * nonce);
/* management api for use externally */
EffaceableReturn (* getBytes )(effaceable_storage_t * storage, void * client_buf, uint32_t offset, uint32_t count);
EffaceableReturn (* wipeStorage )(effaceable_storage_t * storage, void * tmp_buf);
uint32_t (* getCapacity )(effaceable_storage_t * storage);
bool (* isFormatted )(effaceable_storage_t * storage);
EffaceableReturn (* formatStorage )(effaceable_storage_t * storage);
/* spi for use by device-specific controller */
uint32_t (* getCopiesPerUnit )(effaceable_storage_t * storage);
uint32_t (* getCopySize )(effaceable_storage_t * storage);
};
// -----------------------------------------------------------------------------
struct _effaceable_device
{
void * opaque;
/* device controller must provide concrete implementation of these operations */
uint32_t (* getGroupCount )(effaceable_device_t * device);
uint32_t (* getUnitsPerGroup )(effaceable_device_t * device);
uint32_t (* getUnitSize )(effaceable_device_t * device);
EffaceableReturn (* eraseGroup )(effaceable_device_t * device, uint32_t group);
EffaceableReturn (* writeUnit )(effaceable_device_t * device, const void * buf, uint32_t group, uint32_t unit);
EffaceableReturn (* readUnit )(effaceable_device_t * device, void * buf, uint32_t group, uint32_t unit);
/* device controller may optionally extend these device; if NULL, code should not call */
void (* cleanBuffers )(effaceable_device_t * device);
void (* foundCurrentCloneInGroup )(effaceable_device_t * device, uint32_t group);
void (* updateDynamicProperties )(effaceable_device_t * device);
};
// -----------------------------------------------------------------------------
struct _effaceable_system
{
void * opaque;
/* operating system specific operations must be supplied when starting device */
void * (* allocMem )(effaceable_system_t * system, uint32_t size);
void (* freeMem )(effaceable_system_t * system, void * buf, uint32_t size);
void * (* setMem )(effaceable_system_t * system, void * buf, uint8_t val, uint32_t size);
void * (* moveMem )(effaceable_system_t * system, void * dst, const void * src, uint32_t size);
int (* cmpMem )(effaceable_system_t * system, const void * lhs, const void * rhs, uint32_t size);
void (* readRandom )(effaceable_system_t * system, void * buf, uint32_t size);
void (* calcSHA1 )(effaceable_system_t * system, const void * buf, uint32_t size, void * hash);
uint32_t (* crc32 )(effaceable_system_t * system, uint32_t crc, const void * buf, uint32_t size, bool finish);
bool (* setProperty )(effaceable_system_t * system, const char * key, uint32_t value);
void (* panicSys )(effaceable_system_t * system, const char * msg);
int (* vlogf )(effaceable_system_t * system, const char * fmt, va_list ap);
};
// -----------------------------------------------------------------------------
/* contract for NOR HAL operations */
struct _effaceable_nor_hal
{
void * opaque;
/* NOR HAL operations must be supplied when starting device */
uint32_t (* getRegionCount )(effaceable_nor_hal_t * nor);
uint32_t (* getRegionSize )(effaceable_nor_hal_t * nor, uint32_t index);
EffaceableReturn (* eraseRegion )(effaceable_nor_hal_t * nor, uint32_t index, uint32_t length);
uint32_t (* writeRegion )(effaceable_nor_hal_t * nor, uint32_t index, const void * buf, uint32_t length);
bool (* readRegion )(effaceable_nor_hal_t * nor, uint32_t index, void * buf, uint32_t length);
};
// -----------------------------------------------------------------------------
/* contract for NAND HAL operations */
struct _effaceable_nand_hal
{
void * opaque;
/* NAND HAL operations must be supplied when starting device */
uint32_t (* getPageSize )(effaceable_nand_hal_t * nand);
uint32_t (* getPageCount )(effaceable_nand_hal_t * nand);
uint32_t (* getBlockCount )(effaceable_nand_hal_t * nand);
uint32_t (* getBankCount )(effaceable_nand_hal_t * nand);
bool (* isBlockBad )(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block);
EffaceableReturn (* eraseBlock )(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block);
EffaceableReturn (* writePage )(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block, uint32_t page, const void * buf, uint32_t length);
EffaceableReturn (* readPage )(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block, uint32_t page, void * buf, uint32_t length);
bool (* requestPartitionTableDiff )(effaceable_nand_hal_t * nand, void * buf, uint32_t length);
bool (* providePartitionTableDiff )(effaceable_nand_hal_t * nand, void * buf, uint32_t length);
};
// =============================================================================
__BEGIN_DECLS
// -----------------------------------------------------------------------------
extern void startEffaceableNOR(effaceable_device_t * device, effaceable_system_t * system, effaceable_nor_hal_t * nor, effaceable_storage_t * storage);
extern void startEffaceableNAND(effaceable_device_t * device, effaceable_system_t * system, effaceable_nand_hal_t * nor, effaceable_storage_t * storage);
// -----------------------------------------------------------------------------
__END_DECLS
// =============================================================================
#endif /* _EFFACEABLE_CONTRACT_H_ */
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#ifndef _EFFACEABLE_DEBUG_H_
#define _EFFACEABLE_DEBUG_H_
// =============================================================================
#define EFFACEABLE_DBGMSK_INIT (1<<0)
#define EFFACEABLE_DBGMSK_ERR (1<<1)
#define EFFACEABLE_DBGMSK_WARN (1<<2)
#define EFFACEABLE_DBGMSK_INFO (1<<3)
#define EFFACEABLE_DBGMSK_SPEW (1<<4)
// -----------------------------------------------------------------------------
#define EFFACEABLE_DBGTAG_INIT "INIT"
#define EFFACEABLE_DBGTAG_ERR "ERR "
#define EFFACEABLE_DBGTAG_WARN "WARN"
#define EFFACEABLE_DBGTAG_INFO "INFO"
#define EFFACEABLE_DBGTAG_SPEW "SPEW"
// -----------------------------------------------------------------------------
#define EFFACEABLE_DEBUG_ALL (EFFACEABLE_DBGMSK_INIT | \
EFFACEABLE_DBGMSK_ERR | \
EFFACEABLE_DBGMSK_WARN | \
EFFACEABLE_DBGMSK_INFO | \
EFFACEABLE_DBGMSK_SPEW)
#define EFFACEABLE_DEBUG_MOST (EFFACEABLE_DBGMSK_INIT | \
EFFACEABLE_DBGMSK_ERR | \
EFFACEABLE_DBGMSK_WARN | \
EFFACEABLE_DBGMSK_INFO)
#define EFFACEABLE_DEBUG_DEFAULT (EFFACEABLE_DBGMSK_INIT | \
EFFACEABLE_DBGMSK_ERR)
// -----------------------------------------------------------------------------
#define EFFACEABLE_DEBUG EFFACEABLE_DEBUG_DEFAULT
// -----------------------------------------------------------------------------
#if defined(EFFACEABLE_DEBUG) && EFFACEABLE_DEBUG
# define dlogf(_api, _fac, _fmt, ...) \
do { \
if (EFFACEABLE_DBGMSK_##_fac & \
EFFACEABLE_DEBUG) { \
logf(_api, "[effaceable:%s] " _fmt "\n", \
EFFACEABLE_DBGTAG_##_fac , \
## __VA_ARGS__ ); \
} \
} while(0)
# define dhexdump(_api, _fac, _buf, _count) \
do { \
if (EFFACEABLE_DBGMSK_##_fac & \
EFFACEABLE_DEBUG) { \
unsigned _i, _j; \
for (_i = 0; _i < _count; _i += 16) { \
logf(_api, "[effaceable:%s] 0x%08x:", \
EFFACEABLE_DBGTAG_##_fac , \
((unsigned) _buf) + _i); \
for (_j = 0; (_j < 16) && ((_i + _j) < _count); _j++) \
logf(_api, " %2.2x", ((uint8_t*)_buf)[_i + _j]); \
logf(_api, "\n"); \
} \
} \
} while(0)
#else
# define dlogf(...) do { /* nothing */ } while(0)
# define dhexdump(...) do { /* nothing */ } while(0)
#endif
// =============================================================================
#endif /* _EFFACEABLE_DEBUG_H_ */
+768
View File
@@ -0,0 +1,768 @@
/*
* Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)queue.h 8.5 (Berkeley) 8/20/94
*/
#ifndef _SYS_QUEUE_H_
#define _SYS_QUEUE_H_
/*
* This file defines five types of data structures: singly-linked lists,
* singly-linked tail queues, lists, tail queues, and circular queues.
*
* A singly-linked list is headed by a single forward pointer. The elements
* are singly linked for minimum space and pointer manipulation overhead at
* the expense of O(n) removal for arbitrary elements. New elements can be
* added to the list after an existing element or at the head of the list.
* Elements being removed from the head of the list should use the explicit
* macro for this purpose for optimum efficiency. A singly-linked list may
* only be traversed in the forward direction. Singly-linked lists are ideal
* for applications with large datasets and few or no removals or for
* implementing a LIFO queue.
*
* A singly-linked tail queue is headed by a pair of pointers, one to the
* head of the list and the other to the tail of the list. The elements are
* singly linked for minimum space and pointer manipulation overhead at the
* expense of O(n) removal for arbitrary elements. New elements can be added
* to the list after an existing element, at the head of the list, or at the
* end of the list. Elements being removed from the head of the tail queue
* should use the explicit macro for this purpose for optimum efficiency.
* A singly-linked tail queue may only be traversed in the forward direction.
* Singly-linked tail queues are ideal for applications with large datasets
* and few or no removals or for implementing a FIFO queue.
*
* A list is headed by a single forward pointer (or an array of forward
* pointers for a hash table header). The elements are doubly linked
* so that an arbitrary element can be removed without a need to
* traverse the list. New elements can be added to the list before
* or after an existing element or at the head of the list. A list
* may only be traversed in the forward direction.
*
* A tail queue is headed by a pair of pointers, one to the head of the
* list and the other to the tail of the list. The elements are doubly
* linked so that an arbitrary element can be removed without a need to
* traverse the list. New elements can be added to the list before or
* after an existing element, at the head of the list, or at the end of
* the list. A tail queue may be traversed in either direction.
*
* A circle queue is headed by a pair of pointers, one to the head of the
* list and the other to the tail of the list. The elements are doubly
* linked so that an arbitrary element can be removed without a need to
* traverse the list. New elements can be added to the list before or after
* an existing element, at the head of the list, or at the end of the list.
* A circle queue may be traversed in either direction, but has a more
* complex end of list detection.
* Note that circle queues are deprecated, because, as the removal log
* in FreeBSD states, "CIRCLEQs are a disgrace to everything Knuth taught
* us in Volume 1 Chapter 2. [...] Use TAILQ instead, it provides the same
* functionality." Code using them will continue to compile, but they
* are no longer documented on the man page.
*
* For details on the use of these macros, see the queue(3) manual page.
*
*
* SLIST LIST STAILQ TAILQ CIRCLEQ
* _HEAD + + + + +
* _HEAD_INITIALIZER + + + + -
* _ENTRY + + + + +
* _INIT + + + + +
* _EMPTY + + + + +
* _FIRST + + + + +
* _NEXT + + + + +
* _PREV - - - + +
* _LAST - - + + +
* _FOREACH + + + + +
* _FOREACH_SAFE + + + + -
* _FOREACH_REVERSE - - - + -
* _FOREACH_REVERSE_SAFE - - - + -
* _INSERT_HEAD + + + + +
* _INSERT_BEFORE - + - + +
* _INSERT_AFTER + + + + +
* _INSERT_TAIL - - + + +
* _CONCAT - - + + -
* _REMOVE_AFTER + - + - -
* _REMOVE_HEAD + - + - -
* _REMOVE_HEAD_UNTIL - - + - -
* _REMOVE + + + + +
* _SWAP - + + + -
*
*/
#ifdef QUEUE_MACRO_DEBUG
/* Store the last 2 places the queue element or head was altered */
struct qm_trace {
char * lastfile;
int lastline;
char * prevfile;
int prevline;
};
#define TRACEBUF struct qm_trace trace;
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
#define QMD_TRACE_HEAD(head) do { \
(head)->trace.prevline = (head)->trace.lastline; \
(head)->trace.prevfile = (head)->trace.lastfile; \
(head)->trace.lastline = __LINE__; \
(head)->trace.lastfile = __FILE__; \
} while (0)
#define QMD_TRACE_ELEM(elem) do { \
(elem)->trace.prevline = (elem)->trace.lastline; \
(elem)->trace.prevfile = (elem)->trace.lastfile; \
(elem)->trace.lastline = __LINE__; \
(elem)->trace.lastfile = __FILE__; \
} while (0)
#else
#define QMD_TRACE_ELEM(elem)
#define QMD_TRACE_HEAD(head)
#define TRACEBUF
#define TRASHIT(x)
#endif /* QUEUE_MACRO_DEBUG */
/*
* Singly-linked List declarations.
*/
#define SLIST_HEAD(name, type) \
struct name { \
struct type *slh_first; /* first element */ \
}
#define SLIST_HEAD_INITIALIZER(head) \
{ NULL }
#define SLIST_ENTRY(type) \
struct { \
struct type *sle_next; /* next element */ \
}
/*
* Singly-linked List functions.
*/
#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
#define SLIST_FIRST(head) ((head)->slh_first)
#define SLIST_FOREACH(var, head, field) \
for ((var) = SLIST_FIRST((head)); \
(var); \
(var) = SLIST_NEXT((var), field))
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = SLIST_FIRST((head)); \
(var) && ((tvar) = SLIST_NEXT((var), field), 1); \
(var) = (tvar))
#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
for ((varp) = &SLIST_FIRST((head)); \
((var) = *(varp)) != NULL; \
(varp) = &SLIST_NEXT((var), field))
#define SLIST_INIT(head) do { \
SLIST_FIRST((head)) = NULL; \
} while (0)
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
SLIST_NEXT((slistelm), field) = (elm); \
} while (0)
#define SLIST_INSERT_HEAD(head, elm, field) do { \
SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
SLIST_FIRST((head)) = (elm); \
} while (0)
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
#define SLIST_REMOVE(head, elm, type, field) do { \
if (SLIST_FIRST((head)) == (elm)) { \
SLIST_REMOVE_HEAD((head), field); \
} \
else { \
struct type *curelm = SLIST_FIRST((head)); \
while (SLIST_NEXT(curelm, field) != (elm)) \
curelm = SLIST_NEXT(curelm, field); \
SLIST_REMOVE_AFTER(curelm, field); \
} \
TRASHIT((elm)->field.sle_next); \
} while (0)
#define SLIST_REMOVE_AFTER(elm, field) do { \
SLIST_NEXT(elm, field) = \
SLIST_NEXT(SLIST_NEXT(elm, field), field); \
} while (0)
#define SLIST_REMOVE_HEAD(head, field) do { \
SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
} while (0)
/*
* Singly-linked Tail queue declarations.
*/
#define STAILQ_HEAD(name, type) \
struct name { \
struct type *stqh_first;/* first element */ \
struct type **stqh_last;/* addr of last next element */ \
}
#define STAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).stqh_first }
#define STAILQ_ENTRY(type) \
struct { \
struct type *stqe_next; /* next element */ \
}
/*
* Singly-linked Tail queue functions.
*/
#define STAILQ_CONCAT(head1, head2) do { \
if (!STAILQ_EMPTY((head2))) { \
*(head1)->stqh_last = (head2)->stqh_first; \
(head1)->stqh_last = (head2)->stqh_last; \
STAILQ_INIT((head2)); \
} \
} while (0)
#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
#define STAILQ_FIRST(head) ((head)->stqh_first)
#define STAILQ_FOREACH(var, head, field) \
for((var) = STAILQ_FIRST((head)); \
(var); \
(var) = STAILQ_NEXT((var), field))
#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = STAILQ_FIRST((head)); \
(var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
(var) = (tvar))
#define STAILQ_INIT(head) do { \
STAILQ_FIRST((head)) = NULL; \
(head)->stqh_last = &STAILQ_FIRST((head)); \
} while (0)
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
STAILQ_NEXT((tqelm), field) = (elm); \
} while (0)
#define STAILQ_INSERT_HEAD(head, elm, field) do { \
if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
STAILQ_FIRST((head)) = (elm); \
} while (0)
#define STAILQ_INSERT_TAIL(head, elm, field) do { \
STAILQ_NEXT((elm), field) = NULL; \
*(head)->stqh_last = (elm); \
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
} while (0)
#define STAILQ_LAST(head, type, field) \
(STAILQ_EMPTY((head)) ? \
NULL : \
((struct type *)(void *) \
((char *)((head)->stqh_last) - __offsetof(struct type, field))))
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
#define STAILQ_REMOVE(head, elm, type, field) do { \
if (STAILQ_FIRST((head)) == (elm)) { \
STAILQ_REMOVE_HEAD((head), field); \
} \
else { \
struct type *curelm = STAILQ_FIRST((head)); \
while (STAILQ_NEXT(curelm, field) != (elm)) \
curelm = STAILQ_NEXT(curelm, field); \
STAILQ_REMOVE_AFTER(head, curelm, field); \
} \
TRASHIT((elm)->field.stqe_next); \
} while (0)
#define STAILQ_REMOVE_HEAD(head, field) do { \
if ((STAILQ_FIRST((head)) = \
STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
(head)->stqh_last = &STAILQ_FIRST((head)); \
} while (0)
#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
(head)->stqh_last = &STAILQ_FIRST((head)); \
} while (0)
#define STAILQ_REMOVE_AFTER(head, elm, field) do { \
if ((STAILQ_NEXT(elm, field) = \
STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
} while (0)
#define STAILQ_SWAP(head1, head2, type) do { \
struct type *swap_first = STAILQ_FIRST(head1); \
struct type **swap_last = (head1)->stqh_last; \
STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
(head1)->stqh_last = (head2)->stqh_last; \
STAILQ_FIRST(head2) = swap_first; \
(head2)->stqh_last = swap_last; \
if (STAILQ_EMPTY(head1)) \
(head1)->stqh_last = &STAILQ_FIRST(head1); \
if (STAILQ_EMPTY(head2)) \
(head2)->stqh_last = &STAILQ_FIRST(head2); \
} while (0)
/*
* List declarations.
*/
#define LIST_HEAD(name, type) \
struct name { \
struct type *lh_first; /* first element */ \
}
#define LIST_HEAD_INITIALIZER(head) \
{ NULL }
#define LIST_ENTRY(type) \
struct { \
struct type *le_next; /* next element */ \
struct type **le_prev; /* address of previous next element */ \
}
/*
* List functions.
*/
#if (defined(_KERNEL) && defined(INVARIANTS)) || defined(QUEUE_MACRO_DEBUG)
#define QMD_LIST_CHECK_HEAD(head, field) do { \
if (LIST_FIRST((head)) != NULL && \
LIST_FIRST((head))->field.le_prev != \
&LIST_FIRST((head))) \
panic("Bad list head %p first->prev != head", (head)); \
} while (0)
#define QMD_LIST_CHECK_NEXT(elm, field) do { \
if (LIST_NEXT((elm), field) != NULL && \
LIST_NEXT((elm), field)->field.le_prev != \
&((elm)->field.le_next)) \
panic("Bad link elm %p next->prev != elm", (elm)); \
} while (0)
#define QMD_LIST_CHECK_PREV(elm, field) do { \
if (*(elm)->field.le_prev != (elm)) \
panic("Bad link elm %p prev->next != elm", (elm)); \
} while (0)
#else
#define QMD_LIST_CHECK_HEAD(head, field)
#define QMD_LIST_CHECK_NEXT(elm, field)
#define QMD_LIST_CHECK_PREV(elm, field)
#endif /* (_KERNEL && INVARIANTS) || QUEUE_MACRO_DEBUG */
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
#define LIST_FIRST(head) ((head)->lh_first)
#define LIST_FOREACH(var, head, field) \
for ((var) = LIST_FIRST((head)); \
(var); \
(var) = LIST_NEXT((var), field))
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = LIST_FIRST((head)); \
(var) && ((tvar) = LIST_NEXT((var), field), 1); \
(var) = (tvar))
#define LIST_INIT(head) do { \
LIST_FIRST((head)) = NULL; \
} while (0)
#define LIST_INSERT_AFTER(listelm, elm, field) do { \
QMD_LIST_CHECK_NEXT(listelm, field); \
if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
LIST_NEXT((listelm), field)->field.le_prev = \
&LIST_NEXT((elm), field); \
LIST_NEXT((listelm), field) = (elm); \
(elm)->field.le_prev = &LIST_NEXT((listelm), field); \
} while (0)
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
QMD_LIST_CHECK_PREV(listelm, field); \
(elm)->field.le_prev = (listelm)->field.le_prev; \
LIST_NEXT((elm), field) = (listelm); \
*(listelm)->field.le_prev = (elm); \
(listelm)->field.le_prev = &LIST_NEXT((elm), field); \
} while (0)
#define LIST_INSERT_HEAD(head, elm, field) do { \
QMD_LIST_CHECK_HEAD((head), field); \
if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
LIST_FIRST((head)) = (elm); \
(elm)->field.le_prev = &LIST_FIRST((head)); \
} while (0)
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
#define LIST_REMOVE(elm, field) do { \
QMD_LIST_CHECK_NEXT(elm, field); \
QMD_LIST_CHECK_PREV(elm, field); \
if (LIST_NEXT((elm), field) != NULL) \
LIST_NEXT((elm), field)->field.le_prev = \
(elm)->field.le_prev; \
*(elm)->field.le_prev = LIST_NEXT((elm), field); \
TRASHIT((elm)->field.le_next); \
TRASHIT((elm)->field.le_prev); \
} while (0)
#define LIST_SWAP(head1, head2, type, field) do { \
struct type *swap_tmp = LIST_FIRST((head1)); \
LIST_FIRST((head1)) = LIST_FIRST((head2)); \
LIST_FIRST((head2)) = swap_tmp; \
if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
} while (0)
/*
* Tail queue declarations.
*/
#define TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; /* first element */ \
struct type **tqh_last; /* addr of last next element */ \
TRACEBUF \
}
#define TAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).tqh_first }
#define TAILQ_ENTRY(type) \
struct { \
struct type *tqe_next; /* next element */ \
struct type **tqe_prev; /* address of previous next element */ \
TRACEBUF \
}
/*
* Tail queue functions.
*/
#define TAILQ_CONCAT(head1, head2, field) do { \
if (!TAILQ_EMPTY(head2)) { \
*(head1)->tqh_last = (head2)->tqh_first; \
(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
(head1)->tqh_last = (head2)->tqh_last; \
TAILQ_INIT((head2)); \
QMD_TRACE_HEAD(head1); \
QMD_TRACE_HEAD(head2); \
} \
} while (0)
#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
#define TAILQ_FIRST(head) ((head)->tqh_first)
#define TAILQ_FOREACH(var, head, field) \
for ((var) = TAILQ_FIRST((head)); \
(var); \
(var) = TAILQ_NEXT((var), field))
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = TAILQ_FIRST((head)); \
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
(var) = (tvar))
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
for ((var) = TAILQ_LAST((head), headname); \
(var); \
(var) = TAILQ_PREV((var), headname, field))
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
for ((var) = TAILQ_LAST((head), headname); \
(var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
(var) = (tvar))
#define TAILQ_INIT(head) do { \
TAILQ_FIRST((head)) = NULL; \
(head)->tqh_last = &TAILQ_FIRST((head)); \
QMD_TRACE_HEAD(head); \
} while (0)
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
TAILQ_NEXT((elm), field)->field.tqe_prev = \
&TAILQ_NEXT((elm), field); \
else { \
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
QMD_TRACE_HEAD(head); \
} \
TAILQ_NEXT((listelm), field) = (elm); \
(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
QMD_TRACE_ELEM(&(elm)->field); \
QMD_TRACE_ELEM(&listelm->field); \
} while (0)
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
TAILQ_NEXT((elm), field) = (listelm); \
*(listelm)->field.tqe_prev = (elm); \
(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
QMD_TRACE_ELEM(&(elm)->field); \
QMD_TRACE_ELEM(&listelm->field); \
} while (0)
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
TAILQ_FIRST((head))->field.tqe_prev = \
&TAILQ_NEXT((elm), field); \
else \
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
TAILQ_FIRST((head)) = (elm); \
(elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
QMD_TRACE_HEAD(head); \
QMD_TRACE_ELEM(&(elm)->field); \
} while (0)
#define TAILQ_INSERT_TAIL(head, elm, field) do { \
TAILQ_NEXT((elm), field) = NULL; \
(elm)->field.tqe_prev = (head)->tqh_last; \
*(head)->tqh_last = (elm); \
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
QMD_TRACE_HEAD(head); \
QMD_TRACE_ELEM(&(elm)->field); \
} while (0)
#define TAILQ_LAST(head, headname) \
(*(((struct headname *)((head)->tqh_last))->tqh_last))
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
#define TAILQ_PREV(elm, headname, field) \
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
#define TAILQ_REMOVE(head, elm, field) do { \
if ((TAILQ_NEXT((elm), field)) != NULL) \
TAILQ_NEXT((elm), field)->field.tqe_prev = \
(elm)->field.tqe_prev; \
else { \
(head)->tqh_last = (elm)->field.tqe_prev; \
QMD_TRACE_HEAD(head); \
} \
*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
TRASHIT((elm)->field.tqe_next); \
TRASHIT((elm)->field.tqe_prev); \
QMD_TRACE_ELEM(&(elm)->field); \
} while (0)
#define TAILQ_SWAP(head1, head2, type, field) do { \
struct type *swap_first = (head1)->tqh_first; \
struct type **swap_last = (head1)->tqh_last; \
(head1)->tqh_first = (head2)->tqh_first; \
(head1)->tqh_last = (head2)->tqh_last; \
(head2)->tqh_first = swap_first; \
(head2)->tqh_last = swap_last; \
if ((swap_first = (head1)->tqh_first) != NULL) \
swap_first->field.tqe_prev = &(head1)->tqh_first; \
else \
(head1)->tqh_last = &(head1)->tqh_first; \
if ((swap_first = (head2)->tqh_first) != NULL) \
swap_first->field.tqe_prev = &(head2)->tqh_first; \
else \
(head2)->tqh_last = &(head2)->tqh_first; \
} while (0)
/*
* Circular queue definitions.
*/
#define CIRCLEQ_HEAD(name, type) \
struct name { \
struct type *cqh_first; /* first element */ \
struct type *cqh_last; /* last element */ \
}
#define CIRCLEQ_ENTRY(type) \
struct { \
struct type *cqe_next; /* next element */ \
struct type *cqe_prev; /* previous element */ \
}
/*
* Circular queue functions.
*/
#define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
#define CIRCLEQ_FOREACH(var, head, field) \
for((var) = (head)->cqh_first; \
(var) != (void *)(head); \
(var) = (var)->field.cqe_next)
#define CIRCLEQ_INIT(head) do { \
(head)->cqh_first = (void *)(head); \
(head)->cqh_last = (void *)(head); \
} while (0)
#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
(elm)->field.cqe_next = (listelm)->field.cqe_next; \
(elm)->field.cqe_prev = (listelm); \
if ((listelm)->field.cqe_next == (void *)(head)) \
(head)->cqh_last = (elm); \
else \
(listelm)->field.cqe_next->field.cqe_prev = (elm); \
(listelm)->field.cqe_next = (elm); \
} while (0)
#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
(elm)->field.cqe_next = (listelm); \
(elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
if ((listelm)->field.cqe_prev == (void *)(head)) \
(head)->cqh_first = (elm); \
else \
(listelm)->field.cqe_prev->field.cqe_next = (elm); \
(listelm)->field.cqe_prev = (elm); \
} while (0)
#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
(elm)->field.cqe_next = (head)->cqh_first; \
(elm)->field.cqe_prev = (void *)(head); \
if ((head)->cqh_last == (void *)(head)) \
(head)->cqh_last = (elm); \
else \
(head)->cqh_first->field.cqe_prev = (elm); \
(head)->cqh_first = (elm); \
} while (0)
#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
(elm)->field.cqe_next = (void *)(head); \
(elm)->field.cqe_prev = (head)->cqh_last; \
if ((head)->cqh_first == (void *)(head)) \
(head)->cqh_first = (elm); \
else \
(head)->cqh_last->field.cqe_next = (elm); \
(head)->cqh_last = (elm); \
} while (0)
#define CIRCLEQ_LAST(head) ((head)->cqh_last)
#define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
#define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
#define CIRCLEQ_REMOVE(head, elm, field) do { \
if ((elm)->field.cqe_next == (void *)(head)) \
(head)->cqh_last = (elm)->field.cqe_prev; \
else \
(elm)->field.cqe_next->field.cqe_prev = \
(elm)->field.cqe_prev; \
if ((elm)->field.cqe_prev == (void *)(head)) \
(head)->cqh_first = (elm)->field.cqe_next; \
else \
(elm)->field.cqe_prev->field.cqe_next = \
(elm)->field.cqe_next; \
} while (0)
#ifdef _KERNEL
#if NOTFB31
/*
* XXX insque() and remque() are an old way of handling certain queues.
* They bogusly assumes that all queue heads look alike.
*/
struct quehead {
struct quehead *qh_link;
struct quehead *qh_rlink;
};
#ifdef __GNUC__
static __inline void
insque(void *a, void *b)
{
struct quehead *element = (struct quehead *)a,
*head = (struct quehead *)b;
element->qh_link = head->qh_link;
element->qh_rlink = head;
head->qh_link = element;
element->qh_link->qh_rlink = element;
}
static __inline void
remque(void *a)
{
struct quehead *element = (struct quehead *)a;
element->qh_link->qh_rlink = element->qh_rlink;
element->qh_rlink->qh_link = element->qh_link;
element->qh_rlink = 0;
}
#else /* !__GNUC__ */
void insque(void *a, void *b);
void remque(void *a);
#endif /* __GNUC__ */
#endif
#endif /* _KERNEL */
#endif /* !_SYS_QUEUE_H_ */
+19
View File
@@ -0,0 +1,19 @@
#
# Copyright (c) 2011 Apple Inc. All rights reserved.
#
# This document is the property of Apple Inc.
# It is considered confidential and proprietary.
#
# This document may not be reproduced or transmitted in any form,
# in whole or in part, without the express written permission of
# Apple Inc.
#
LOCAL_DIR := $(GET_LOCAL_DIR)
GLOBAL_INCLUDES += \
$(LOCAL_DIR)/include
ALL_OBJS += \
$(LOCAL_DIR)/effaceable_storage_core.o \
$(LOCAL_DIR)/effaceable_nand_core.o \
$(LOCAL_DIR)/effaceable_nor_core.o