first and last commit
This commit is contained in:
commit
1bf4efd552
2612 files changed
+636500
No files matched your search
@@ -0,0 +1,9 @@
|
||||
Table of Contents:
|
||||
|
||||
README - this file, containing an overview of the contents of the AppleEffaceableStorage project
|
||||
AppleEffaceableStorage.xcodeproj - xcode project definition bundle for building iokit driver and ios utility command
|
||||
common - portable core of the effaceable storage implementation, common to all build contexts
|
||||
docs - various notes and (coming soon) design documents discussing effaceable storage
|
||||
iboot - code specific to hosting portable core in iBoot context
|
||||
iokit - code specific to hosting portable core in IOKit context
|
||||
ios - code providing 'effaceable' command line utility in iOS; used only for user client example and internal development
|
||||
@@ -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_ */
|
||||
@@ -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_ */
|
||||
@@ -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_ */
|
||||
@@ -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_ */
|
||||
@@ -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
|
||||
@@ -0,0 +1,18 @@
|
||||
Significant changes were made to the structure of the project in the transition between AppleEffaceableStorage-24 and AppleEffaceableStorage-25.
|
||||
|
||||
To assist auditors with their task (and anyone else analyzing the history of the project), it is useful to observe the following points:
|
||||
|
||||
* core logic was refactored from the prior classes into a portable core implementation, located in the 'common' directory
|
||||
- compare previous ./AppleEffaceableStorage.cpp to current ./common/effaceable_storage_core.c
|
||||
- compare previous ./AppleEffaceableNAND.cpp to current ./common/effaceable_nand_core.c
|
||||
- compare previous ./AppleEffaceableNOR.cpp to current ./common/effaceable_nor_core.c
|
||||
* additional directory restructuring was done to make major implementation components more obvious to the reader
|
||||
- private headers made available for exither external or internal cross-component use were moved to ./include subdirectory
|
||||
- remaining IOKit code was moved to the ./iokit subdirectory
|
||||
- implementation of the 'effaceable' command line utility was moved to the ./ios subdirectory
|
||||
* more other
|
||||
- there was also a minor bit of new functionality added to support generation and consumption of the private nonce locker
|
||||
- the 'moveMem' system function contract uses 'memmove' arg ordering, so 'bcopy' instances replaced have swapped pointer args
|
||||
- isCopyValid() modified to report real errors; in doing so, structure of logic changed such that all prior checks are negated
|
||||
- deleted all trailing whitespace (with exception of 'queue.h', since it is imported from 'xnu/bsd/sys/queue.h')
|
||||
- converted any tabs that crept in to four space hard indent to match style in majority of project (exception: 'queue.h')
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* 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 <AppleEffaceableStorageFormat.h>
|
||||
#include <AppleEffaceableStorageKeys.h>
|
||||
|
||||
#include <lib/effaceable.h>
|
||||
|
||||
#include <debug.h>
|
||||
#include <sys/menu.h>
|
||||
#include <drivers/sha1.h>
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int do_effaceable(int argc, struct cmd_arg *args);
|
||||
static void printUsage(int argc, struct cmd_arg *args);
|
||||
static void effaceable_hexdump(const void * buf, uint32_t count);
|
||||
static int lockerList(void);
|
||||
|
||||
#if DEBUG_BUILD
|
||||
static int consumeNonce(void);
|
||||
#endif
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int
|
||||
do_effaceable(int argc, struct cmd_arg *args)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (1 >= argc) {
|
||||
printUsage(argc, args);
|
||||
err = 0;
|
||||
} else if (0 == strcmp("lockers", args[1].str)) {
|
||||
err = lockerList();
|
||||
#if DEBUG_BUILD
|
||||
} else if (0 == strcmp("nonce", args[1].str)) {
|
||||
err = consumeNonce();
|
||||
#endif
|
||||
} else {
|
||||
printUsage(argc, args);
|
||||
printf("unrecognized subcommand\n");
|
||||
err = -1;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static void
|
||||
printUsage(int argc, struct cmd_arg *args)
|
||||
{
|
||||
printf("USAGE: %s <subcmd>\n\n", ((0 < argc) ? args[0].str : "?"));
|
||||
printf(" Where <subcmd> is one of following\n\n");
|
||||
printf(" lockers - list lockers and contents\n");
|
||||
|
||||
#if DEBUG_BUILD
|
||||
printf(" nonce - consume nonce; then, report its SHA1 hash and value\n");
|
||||
#endif
|
||||
|
||||
printf(" ...(more to come)...\n\n");
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static void
|
||||
effaceable_hexdump(const void * buf, uint32_t count)
|
||||
{
|
||||
uint32_t i, j;
|
||||
for (i = 0; i < count; i += 16) {
|
||||
printf("0x%08x:", ((uint32_t) buf) + i);
|
||||
for (j = 0; (j < 16) && ((i + j) < count); j++) {
|
||||
printf(" %02.2x", ((uint8_t*)buf)[i+j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define UNTAG(x) \
|
||||
\
|
||||
(uint8_t)(((x) >> 24) & 0xff), \
|
||||
(uint8_t)(((x) >> 16) & 0xff), \
|
||||
(uint8_t)(((x) >> 8) & 0xff), \
|
||||
(uint8_t)((x) & 0xff)
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int
|
||||
lockerList(void)
|
||||
{
|
||||
uint8_t *buf, *cursor;
|
||||
uint32_t size;
|
||||
AppleEffaceableStorageLockerHeader *header;
|
||||
|
||||
size = effaceable_get_capacity();
|
||||
if (0 == size) {
|
||||
printf("ERROR: unable to get capacity\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf = malloc(size);
|
||||
|
||||
if (!effaceable_get_bytes(buf, 0, size)) {
|
||||
printf("ERROR: unable to read\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
cursor = buf;
|
||||
for (;;) {
|
||||
header = (AppleEffaceableStorageLockerHeader *)cursor;
|
||||
if (header->magic != kAppleEffaceableStorageLockerMagic) {
|
||||
printf("WARNING: unexpected magic 0x%04x at %u, expected 0x%04x\n",
|
||||
header->magic, cursor - buf, kAppleEffaceableStorageLockerMagic);
|
||||
break;
|
||||
}
|
||||
if (header->type_id == kAppleEffaceableStorageLockerSentinel) {
|
||||
printf("<END>\n");
|
||||
break;
|
||||
}
|
||||
printf("0x%08x/0x%04x %c%c%c%c %s\n",
|
||||
header->type_id,
|
||||
header->data_size,
|
||||
UNTAG(header->type_id & ~kAppleEffaceableStorageLockerProtected),
|
||||
(header->type_id & kAppleEffaceableStorageLockerProtected) ? "(protected)" : "");
|
||||
cursor += sizeof(*header);
|
||||
if (header->data_size > 0) {
|
||||
if ((cursor + header->data_size) > (buf + size)) {
|
||||
printf("WARNING: data overflows buffer\n");
|
||||
break;
|
||||
}
|
||||
effaceable_hexdump(cursor, header->data_size);
|
||||
cursor += header->data_size;
|
||||
}
|
||||
if ((cursor + sizeof(*header)) > (buf + size)) {
|
||||
printf("WARNING: header overflows buffer\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
// XXX correct hard-coding of SHA1 buffer size
|
||||
#define SHA1_HASH_SIZE 20
|
||||
|
||||
static int
|
||||
consumeNonce(void)
|
||||
{
|
||||
uint64_t nonce;
|
||||
uint8_t hash[SHA1_HASH_SIZE];
|
||||
int idx;
|
||||
|
||||
if (!effaceable_consume_nonce(&nonce)) {
|
||||
printf("ERROR: unable to consume nonce\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
sha1_calculate(&nonce, sizeof(nonce), hash);
|
||||
|
||||
for (idx = 0; idx < SHA1_HASH_SIZE; idx++) {
|
||||
printf("%s%02X", (idx ? " " : ""), hash[idx]);
|
||||
}
|
||||
printf(" (0x%016llX)\n", nonce);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
MENU_COMMAND_DEBUG(effaceable, do_effaceable, "effaceable storage utilities", NULL);
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#include <lib/effaceable.h>
|
||||
#include <lib/effaceable_impl.h>
|
||||
#include <lib/libc.h>
|
||||
|
||||
#include <effaceable_contract.h>
|
||||
#include <effaceable_debug.h>
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int logf(void * ignored, const char * fmt, ...);
|
||||
|
||||
#define debug(...) dlogf((void*)0, __VA_ARGS__ )
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static effaceable_storage_t * gEffaceableStorage = (void*)0;
|
||||
|
||||
// =============================================================================
|
||||
|
||||
extern void registerEffaceableStorage(effaceable_storage_t * storage)
|
||||
{
|
||||
gEffaceableStorage = storage;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
effaceable_storage_t * locateEffaceableStorage(void)
|
||||
{
|
||||
if (0 == gEffaceableStorage) {
|
||||
debug(ERR, "no storage available");
|
||||
}
|
||||
|
||||
return gEffaceableStorage;
|
||||
}
|
||||
|
||||
bool trustEffaceableContext(void)
|
||||
{
|
||||
#if DEBUG_BUILD
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
bool effaceable_get_locker(uint32_t type_id, void * data, uint32_t * data_size)
|
||||
{
|
||||
bool trust = trustEffaceableContext();
|
||||
effaceable_storage_t * storage = locateEffaceableStorage();
|
||||
|
||||
if (0 == storage) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (kEffaceableReturnSuccess != storage->getLocker(storage, type_id, data, data_size, !trust)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool effaceable_set_locker(uint32_t type_id, const void * data, uint32_t data_size)
|
||||
{
|
||||
bool trust = trustEffaceableContext();
|
||||
effaceable_storage_t * storage = locateEffaceableStorage();
|
||||
|
||||
if (0 == storage) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (kEffaceableReturnSuccess != storage->setLocker(storage, type_id, data, data_size, !trust)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool effaceable_space_for_locker(uint32_t type_id, uint32_t * data_space)
|
||||
{
|
||||
effaceable_storage_t * storage = locateEffaceableStorage();
|
||||
|
||||
if (0 == storage) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (kEffaceableReturnSuccess != storage->spaceForLocker(storage, type_id, data_space)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool effaceable_efface_locker(uint32_t type_id)
|
||||
{
|
||||
bool trust = trustEffaceableContext();
|
||||
effaceable_storage_t * storage = locateEffaceableStorage();
|
||||
|
||||
if (0 == storage) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (kEffaceableReturnSuccess != storage->effaceLocker(storage, type_id, !trust)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool effaceable_consume_nonce(uint64_t * nonce)
|
||||
{
|
||||
effaceable_storage_t * storage = locateEffaceableStorage();
|
||||
|
||||
if (0 == storage) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (kEffaceableReturnSuccess != storage->consumeNonce(storage, nonce)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#if DEBUG_BUILD
|
||||
|
||||
bool effaceable_get_bytes(void * client_buf, uint32_t offset, uint32_t count)
|
||||
{
|
||||
effaceable_storage_t * storage = locateEffaceableStorage();
|
||||
|
||||
if (0 == storage) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (kEffaceableReturnSuccess != storage->getBytes(storage, client_buf, offset, count)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t effaceable_get_capacity(void)
|
||||
{
|
||||
effaceable_storage_t * storage = locateEffaceableStorage();
|
||||
uint32_t capacity = 0;
|
||||
|
||||
if (0 != storage) {
|
||||
capacity = storage->getCapacity(storage);
|
||||
}
|
||||
|
||||
debug(INFO, "capacity is %d bytes", capacity);
|
||||
|
||||
return capacity;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int logf(void * ignored, const char * fmt, ...)
|
||||
{
|
||||
int err;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
err = vprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2012 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 <lib/effaceable_impl.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <lib/random.h>
|
||||
#include <lib/cksum.h>
|
||||
#include <lib/libc.h>
|
||||
#include <drivers/sha1.h>
|
||||
#include <sys.h>
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#if DEBUG_BUILD
|
||||
# define USING_STRONG_SEED false
|
||||
#else
|
||||
# define USING_STRONG_SEED true
|
||||
#endif
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int logf(void * ignored, const char * fmt, ...);
|
||||
|
||||
#define debug(...) dlogf((void*)0, __VA_ARGS__ )
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void * allocMemIboot(effaceable_system_t * system, uint32_t size)
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void freeMemIboot(effaceable_system_t * system, void * buf, uint32_t size)
|
||||
{
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void * setMemIboot(effaceable_system_t * system, void * buf, uint8_t val, uint32_t size)
|
||||
{
|
||||
return memset(buf, val, size);
|
||||
}
|
||||
|
||||
void * moveMemIboot(effaceable_system_t * system, void * dst, const void * src, uint32_t size)
|
||||
{
|
||||
return memmove(dst, src, size);
|
||||
}
|
||||
|
||||
int cmpMemIboot(effaceable_system_t * system, const void * lhs, const void * rhs, uint32_t size)
|
||||
{
|
||||
return memcmp(lhs, rhs, size);
|
||||
}
|
||||
|
||||
void readRandomIboot(effaceable_system_t * system, void * buf, uint32_t size)
|
||||
{
|
||||
static bool is_seeded = false;
|
||||
const unsigned increment = sizeof(int);
|
||||
uint8_t * cursor = (uint8_t *)buf;
|
||||
unsigned idx;
|
||||
|
||||
// if we're using a strong seed, draw from the entropy pool to
|
||||
// re-seed psuedo-random generator the first time function is called
|
||||
if (!is_seeded && USING_STRONG_SEED) {
|
||||
unsigned int seed;
|
||||
random_get_bytes((u_int8_t*)&seed, sizeof(seed));
|
||||
srand(seed);
|
||||
is_seeded = true;
|
||||
}
|
||||
|
||||
// fill with psuedo-random values by 'int'-sized chunks as long as possible
|
||||
for (idx = 0; (idx + increment) <= size; idx += increment, cursor += increment) {
|
||||
*((int*)cursor) = rand();
|
||||
}
|
||||
|
||||
// if size doesn't divide evenly by increment, fill fringe bytewise
|
||||
for (; idx < size; idx++, cursor++) {
|
||||
*cursor = (uint8_t)rand();
|
||||
}
|
||||
}
|
||||
|
||||
void calcSHA1Iboot(effaceable_system_t * system, const void * buf, uint32_t size, void * hash)
|
||||
{
|
||||
sha1_calculate(buf, size, hash);
|
||||
}
|
||||
|
||||
uint32_t crc32Iboot(effaceable_system_t * system, uint32_t crc, const void * buf, uint32_t size, bool finish)
|
||||
{
|
||||
uint32_t val;
|
||||
|
||||
if ((0 == crc) && (0 == buf) && (0 == size)) {
|
||||
val = update_crc32(0xffffffffL, 0, 0);
|
||||
} else {
|
||||
val = update_crc32(crc, (unsigned char *)buf, size);
|
||||
}
|
||||
|
||||
return (finish ? val ^ 0xffffffffL : val);
|
||||
}
|
||||
|
||||
bool setPropertyIboot(effaceable_system_t * system, const char * key, uint32_t value)
|
||||
{
|
||||
debug(INFO, "%s := %u", key, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
void panicIboot(effaceable_system_t * system, const char * msg)
|
||||
{
|
||||
panic("%s", msg);
|
||||
}
|
||||
|
||||
int vlogfIboot(effaceable_system_t * system, const char * fmt, va_list ap)
|
||||
{
|
||||
return vprintf(fmt, ap);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
extern EffaceableReturn
|
||||
setupEffaceableSystemContract(effaceable_system_t * system)
|
||||
{
|
||||
system->opaque = 0;
|
||||
|
||||
system->allocMem = allocMemIboot;
|
||||
system->freeMem = freeMemIboot;
|
||||
system->setMem = setMemIboot;
|
||||
system->moveMem = moveMemIboot;
|
||||
system->cmpMem = cmpMemIboot;
|
||||
system->readRandom = readRandomIboot;
|
||||
system->calcSHA1 = calcSHA1Iboot;
|
||||
system->crc32 = crc32Iboot;
|
||||
system->setProperty = setPropertyIboot;
|
||||
system->panicSys = panicIboot;
|
||||
system->vlogf = vlogfIboot;
|
||||
|
||||
return kEffaceableReturnSuccess;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int logf(void * ignored, const char * fmt, ...)
|
||||
{
|
||||
int err;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
err = vprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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_STORAGE_API_H_
|
||||
#define _EFFACEABLE_STORAGE_API_H_
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
// =============================================================================
|
||||
//
|
||||
// iBoot-specific presentation of the effaceable storage API for client use.
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
bool effaceable_get_locker(uint32_t type_id, void * data, uint32_t * data_size);
|
||||
bool effaceable_set_locker(uint32_t type_id, const void * data, uint32_t data_size);
|
||||
bool effaceable_space_for_locker(uint32_t type_id, uint32_t * data_space);
|
||||
bool effaceable_efface_locker(uint32_t type_id);
|
||||
bool effaceable_consume_nonce(uint64_t * nonce);
|
||||
|
||||
// =============================================================================
|
||||
//
|
||||
// iBoot-specific presentation of the effaceable storage API for util/devel.
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
bool effaceable_get_bytes(void * client_buf, uint32_t offset, uint32_t count);
|
||||
uint32_t effaceable_get_capacity(void);
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#endif /* _EFFACEABLE_STORAGE_API_H_ */
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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_IMPL_H_
|
||||
#define _EFFACEABLE_IMPL_H_
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#include <effaceable_contract.h>
|
||||
|
||||
// =============================================================================
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
extern EffaceableReturn setupEffaceableSystemContract(effaceable_system_t * system);
|
||||
extern void registerEffaceableStorage(effaceable_storage_t * storage);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
__END_DECLS
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#endif /* _EFFACEABLE_IMPL_H_ */
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#include <lib/effaceable.h>
|
||||
#include <lib/effaceable_impl.h>
|
||||
|
||||
#include <effaceable_contract.h>
|
||||
#include <effaceable_debug.h>
|
||||
|
||||
#include <nand_part.h>
|
||||
#include <nand_part_interface.h>
|
||||
|
||||
#include <lib/heap.h>
|
||||
|
||||
// =============================================================================
|
||||
|
||||
struct _context
|
||||
{
|
||||
NandPartInterface * npi;
|
||||
uint32_t idx;
|
||||
};
|
||||
|
||||
typedef struct _context context_t;
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static inline context_t * context(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return (context_t*)nand->opaque;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define delegateNPI(_me, _fn, ...) context(_me)->npi->_fn(context(_me)->npi->core, context(_me)->idx, ## __VA_ARGS__)
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static void initContext(effaceable_nand_hal_t * nand, NandPartInterface * npi, uint32_t idx)
|
||||
{
|
||||
context_t * ctx = (context_t*)malloc(sizeof(context_t));
|
||||
|
||||
ctx->npi = npi;
|
||||
ctx->idx = idx;
|
||||
|
||||
nand->opaque = ctx;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
static uint32_t getPageSizeHook(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return delegateNPI(nand, get_bytes_per_page);
|
||||
}
|
||||
|
||||
static uint32_t getPageCountHook(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return delegateNPI(nand, get_pages_per_block);
|
||||
}
|
||||
|
||||
static uint32_t getBlockCountHook(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return delegateNPI(nand, get_block_depth);
|
||||
}
|
||||
|
||||
static uint32_t getBankCountHook(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return delegateNPI(nand, get_bank_width);
|
||||
}
|
||||
|
||||
static bool isBlockBadHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block)
|
||||
{
|
||||
return delegateNPI(nand, is_block_bad, bank, block);
|
||||
}
|
||||
|
||||
static EffaceableReturn eraseBlockHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block)
|
||||
{
|
||||
return (delegateNPI(nand, erase_block, bank, block) ? kEffaceableReturnSuccess : kEffaceableReturnError);
|
||||
}
|
||||
|
||||
static EffaceableReturn writePageHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block, uint32_t page, const void * buf, uint32_t length)
|
||||
{
|
||||
return (delegateNPI(nand, write_page, bank, block, page, (void*)buf) ? kEffaceableReturnSuccess : kEffaceableReturnError);
|
||||
}
|
||||
|
||||
static EffaceableReturn readPageHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block, uint32_t page, void * buf, uint32_t length)
|
||||
{
|
||||
return (delegateNPI(nand, read_page, bank, block, page, buf, (bool*)0) ? kEffaceableReturnSuccess : kEffaceableReturnError);
|
||||
}
|
||||
|
||||
static bool requestPartitionTableDiffHook(effaceable_nand_hal_t * nand, void * buf, uint32_t length)
|
||||
{
|
||||
return delegateNPI(nand, request_ptab_diff, buf, length);
|
||||
}
|
||||
|
||||
static bool providePartitionTableDiffHook(effaceable_nand_hal_t * nand, void * buf, uint32_t length)
|
||||
{
|
||||
return delegateNPI(nand, provide_ptab_diff, buf, length);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
static void initContract(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
nand->getPageSize = getPageSizeHook;
|
||||
nand->getPageCount = getPageCountHook;
|
||||
nand->getBlockCount = getBlockCountHook;
|
||||
nand->getBankCount = getBankCountHook;
|
||||
nand->isBlockBad = isBlockBadHook;
|
||||
nand->eraseBlock = eraseBlockHook;
|
||||
nand->writePage = writePageHook;
|
||||
nand->readPage = readPageHook;
|
||||
nand->requestPartitionTableDiff = requestPartitionTableDiffHook;
|
||||
nand->providePartitionTableDiff = providePartitionTableDiffHook;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
extern void
|
||||
effaceable_nand_init(NandPartInterface * npi, uint32_t idx)
|
||||
{
|
||||
effaceable_nand_hal_t * nand = (effaceable_nand_hal_t *)malloc(sizeof(effaceable_nand_hal_t));
|
||||
effaceable_device_t * device = (effaceable_device_t *)malloc(sizeof(effaceable_device_t));
|
||||
effaceable_system_t * system = (effaceable_system_t *)malloc(sizeof(effaceable_system_t));
|
||||
effaceable_storage_t * storage = (effaceable_storage_t *)malloc(sizeof(effaceable_storage_t));
|
||||
|
||||
initContext(nand, npi, idx);
|
||||
initContract(nand);
|
||||
|
||||
if (kEffaceableReturnSuccess == setupEffaceableSystemContract(system)) {
|
||||
|
||||
startEffaceableNAND(device, system, nand, storage);
|
||||
registerEffaceableStorage(storage);
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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_NAND_H_
|
||||
#define _EFFACEABLE_NAND_H_
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void effaceable_nand_init(NandPartInterface * npi, uint32_t idx);
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#endif /* _EFFACEABLE_NAND_H_ */
|
||||
@@ -0,0 +1,32 @@
|
||||
#
|
||||
# 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)
|
||||
|
||||
# XXX Clean this kludge up when iBoot two-stage boot changes are submitted.
|
||||
ifneq ($(PRODUCT),iBSS)
|
||||
|
||||
OPTIONS += \
|
||||
WITH_EFFACEABLE_NAND=1
|
||||
|
||||
MODULES += \
|
||||
lib/effaceable/iboot
|
||||
|
||||
# XXX Clean this up when iBoot two-stage boot changes are submitted.
|
||||
GLOBAL_INCLUDES += \
|
||||
drivers/flash_nand/boot \
|
||||
$(LOCAL_DIR)/include
|
||||
|
||||
ALL_OBJS += \
|
||||
$(LOCAL_DIR)/effaceable_nand.o
|
||||
|
||||
endif
|
||||
|
||||
# XXX Consider relocating this directory to 'iBoot/drivers/apple/effaceable/nand'.
|
||||
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#include <lib/effaceable.h>
|
||||
#include <lib/effaceable_impl.h>
|
||||
|
||||
#include <effaceable_contract.h>
|
||||
#include <effaceable_debug.h>
|
||||
|
||||
#include <sys.h>
|
||||
#include <AssertMacros.h>
|
||||
#include <lib/blockdev.h>
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define WITH_EFFACEABLE_NOR_DEBUG 0
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int logf(void * ignored, const char * fmt, ...);
|
||||
|
||||
#define debug(...) dlogf((void*)0, __VA_ARGS__ )
|
||||
|
||||
// =============================================================================
|
||||
//
|
||||
// Effaceable NOR region definition is currently only supplied by means
|
||||
// of publishing device tree up to iOS. Currently, in order to get this off
|
||||
// its feet, I'm about to hard code equivalent information here; however,
|
||||
// that's obviously not a very good solution going forward (for any of the
|
||||
// NOR memory map, honestly).
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
#define NOR_BDEV "nor0"
|
||||
#define REGION_SIZE ((uint32_t)0x1000)
|
||||
|
||||
static const uint32_t region_offsets[] = {0xFA000, 0xFB000};
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void validate(effaceable_nor_hal_t * nor, uint32_t index, uint32_t length)
|
||||
{
|
||||
require(nor != 0, fail_contract);
|
||||
require(nor->opaque != 0, fail_context);
|
||||
require(index < sizeof(region_offsets)/sizeof(region_offsets[0]), fail_index);
|
||||
require(length <= REGION_SIZE, fail_length);
|
||||
return;
|
||||
|
||||
fail_contract:
|
||||
panic("contract missing");
|
||||
|
||||
fail_context:
|
||||
panic("context missing");
|
||||
|
||||
fail_index:
|
||||
panic("bad index");
|
||||
|
||||
fail_length:
|
||||
panic("bad length");
|
||||
}
|
||||
|
||||
uint32_t getNorOffset(uint32_t index)
|
||||
{
|
||||
return region_offsets[index];
|
||||
}
|
||||
|
||||
static void
|
||||
doHexdump(const void * buf, uint32_t count)
|
||||
{
|
||||
uint32_t i, j;
|
||||
for (i = 0; i < count; i += 16) {
|
||||
printf("0x%08x:", ((uint32_t) buf) + i);
|
||||
for (j = 0; (j < 16) && ((i + j) < count); j++) {
|
||||
printf(" %2.2x", ((uint8_t*)buf)[i+j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define context(nor) ((struct blockdev *)nor->opaque)
|
||||
|
||||
// =============================================================================
|
||||
|
||||
uint32_t getRegionCountIboot(effaceable_nor_hal_t * nor)
|
||||
{
|
||||
validate(nor, 0, 0);
|
||||
return sizeof(region_offsets)/sizeof(region_offsets[0]);
|
||||
}
|
||||
|
||||
uint32_t getRegionSizeIboot(effaceable_nor_hal_t * nor, uint32_t index)
|
||||
{
|
||||
validate(nor, index, 0);
|
||||
return REGION_SIZE;
|
||||
}
|
||||
|
||||
EffaceableReturn eraseRegionIboot(effaceable_nor_hal_t * nor, uint32_t index, uint32_t length)
|
||||
{
|
||||
EffaceableReturn ok = kEffaceableReturnSuccess;
|
||||
int err;
|
||||
|
||||
validate(nor, index, length);
|
||||
|
||||
err = blockdev_erase(context(nor), getNorOffset(index), length);
|
||||
|
||||
if (err < 0) {
|
||||
ok = kEffaceableReturnIOError;
|
||||
}
|
||||
debug(INFO, "blockdev_erase on '%s' %s", NOR_BDEV, (kEffaceableReturnSuccess == ok) ? "succeeded" : "failed");
|
||||
return ok;
|
||||
}
|
||||
|
||||
uint32_t writeRegionIboot(effaceable_nor_hal_t * nor, uint32_t index, const void * buf, uint32_t length)
|
||||
{
|
||||
EffaceableReturn ok = kEffaceableReturnSuccess;
|
||||
int err;
|
||||
|
||||
validate(nor, index, length);
|
||||
|
||||
err = blockdev_write(context(nor), buf, getNorOffset(index), length);
|
||||
|
||||
if (err <= 0) {
|
||||
ok = kEffaceableReturnIOError;
|
||||
}
|
||||
debug(INFO, "blockdev_write to '%s' %s", NOR_BDEV, (kEffaceableReturnSuccess == ok) ? "succeeded" : "failed");
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool readRegionIboot(effaceable_nor_hal_t * nor, uint32_t index, void * buf, uint32_t length)
|
||||
{
|
||||
EffaceableReturn ok = kEffaceableReturnSuccess;
|
||||
int err;
|
||||
|
||||
validate(nor, index, length);
|
||||
|
||||
err = blockdev_read(context(nor), buf, getNorOffset(index), length);
|
||||
|
||||
if (err <= 0) {
|
||||
ok = kEffaceableReturnIOError;
|
||||
} else {
|
||||
#if WITH_EFFACEABLE_NOR_DEBUG
|
||||
doHexdump(buf, length);
|
||||
#endif
|
||||
}
|
||||
debug(INFO, "blockdev_read from '%s' %s", NOR_BDEV, (kEffaceableReturnSuccess == ok) ? "succeeded" : "failed");
|
||||
return kEffaceableReturnSuccess;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
EffaceableReturn
|
||||
setupEffaceableNorContract(effaceable_nor_hal_t * nor)
|
||||
{
|
||||
nor->opaque = lookup_blockdev(NOR_BDEV);
|
||||
|
||||
if (0 == nor->opaque) {
|
||||
debug(ERR, "unable to find '%s' blockdev", NOR_BDEV);
|
||||
return kEffaceableReturnBadMedia;
|
||||
}
|
||||
|
||||
nor->getRegionCount = getRegionCountIboot;
|
||||
nor->getRegionSize = getRegionSizeIboot;
|
||||
nor->eraseRegion = eraseRegionIboot;
|
||||
nor->writeRegion = writeRegionIboot;
|
||||
nor->readRegion = readRegionIboot;
|
||||
|
||||
debug(INFO, "nor contract setup complete");
|
||||
|
||||
return kEffaceableReturnSuccess;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
extern void
|
||||
effaceable_nor_init(void)
|
||||
{
|
||||
effaceable_nor_hal_t * nor = (effaceable_nor_hal_t *)malloc(sizeof(effaceable_nor_hal_t));
|
||||
effaceable_device_t * device = (effaceable_device_t *)malloc(sizeof(effaceable_device_t));
|
||||
effaceable_system_t * system = (effaceable_system_t *)malloc(sizeof(effaceable_system_t));
|
||||
effaceable_storage_t * storage = (effaceable_storage_t *)malloc(sizeof(effaceable_storage_t));
|
||||
|
||||
if ((kEffaceableReturnSuccess == setupEffaceableNorContract(nor)) &&
|
||||
(kEffaceableReturnSuccess == setupEffaceableSystemContract(system))) {
|
||||
|
||||
startEffaceableNOR(device, system, nor, storage);
|
||||
registerEffaceableStorage(storage);
|
||||
debug(INFO, "registered nor-backed effaceable storage");
|
||||
|
||||
} else {
|
||||
|
||||
debug(ERR, "failed to init nor-backed effaceable storage");
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static int logf(void * ignored, const char * fmt, ...)
|
||||
{
|
||||
int err;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
err = vprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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_NOR_H_
|
||||
#define _EFFACEABLE_NOR_H_
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void effaceable_nor_init(void);
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#endif /* _EFFACEABLE_NOR_H_ */
|
||||
@@ -0,0 +1,30 @@
|
||||
#
|
||||
# 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)
|
||||
|
||||
# XXX Clean this kludge up when iBoot two-stage boot changes are submitted.
|
||||
ifneq ($(PRODUCT),iBSS)
|
||||
|
||||
OPTIONS += \
|
||||
WITH_EFFACEABLE_NOR=1
|
||||
|
||||
MODULES += \
|
||||
lib/effaceable/iboot
|
||||
|
||||
GLOBAL_INCLUDES += \
|
||||
$(LOCAL_DIR)/include
|
||||
|
||||
ALL_OBJS += \
|
||||
$(LOCAL_DIR)/effaceable_nor.o
|
||||
|
||||
endif
|
||||
|
||||
# XXX Consider relocating this directory to 'iBoot/drivers/apple/effaceable/nand'.
|
||||
@@ -0,0 +1,25 @@
|
||||
#
|
||||
# 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)
|
||||
|
||||
OPTIONS += \
|
||||
WITH_EFFACEABLE=1
|
||||
|
||||
MODULES += \
|
||||
lib/effaceable/common
|
||||
|
||||
GLOBAL_INCLUDES += \
|
||||
$(LOCAL_DIR)/include
|
||||
|
||||
ALL_OBJS += \
|
||||
$(LOCAL_DIR)/effaceable_system.o \
|
||||
$(LOCAL_DIR)/effaceable_storage.o \
|
||||
$(LOCAL_DIR)/effaceable_cmd.o
|
||||
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
* 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 <stdint.h>
|
||||
#include <IOKit/IOLib.h>
|
||||
|
||||
#include "AppleEffaceableNAND.h"
|
||||
|
||||
#include "effaceable_contract.h"
|
||||
#include "effaceable_debug.h"
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define debug(...) dlogf((void*)0, __VA_ARGS__ )
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#ifdef super
|
||||
#undef super
|
||||
#endif
|
||||
#define super AppleEffaceableStorage
|
||||
|
||||
OSDefineMetaClassAndStructors(AppleEffaceableNAND, AppleEffaceableStorage);
|
||||
|
||||
// =============================================================================
|
||||
|
||||
bool
|
||||
AppleEffaceableNAND::start(IOService *provider)
|
||||
{
|
||||
bool ok = false;
|
||||
|
||||
debug(INFO, "starting");
|
||||
|
||||
setupNandContract();
|
||||
|
||||
if (0 == (_partition = OSDynamicCast(IOFlashStoragePartition, provider))) {
|
||||
debug(ERR, "provider is not an IOFlashStoragePartition");
|
||||
} else if (!super::start(provider)) {
|
||||
debug(ERR, "superclass unable to start");
|
||||
} else {
|
||||
debug(INIT, "started");
|
||||
ok = true;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
effaceable_nand_hal_t *
|
||||
AppleEffaceableNAND::nand(void)
|
||||
{
|
||||
return &_nand_hal;
|
||||
}
|
||||
|
||||
void
|
||||
AppleEffaceableNAND::setupDeviceContract(void)
|
||||
{
|
||||
startEffaceableNAND(device(), system(), nand(), storage());
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
AppleEffaceableNAND * context(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
AppleEffaceableNAND * context;
|
||||
|
||||
// XXX assert instead?
|
||||
if (0 == (context = (AppleEffaceableNAND *)nand->opaque))
|
||||
panic("null context");
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
uint32_t getPageSizeHook(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return context(nand)->getPageSizeNand();
|
||||
}
|
||||
|
||||
uint32_t getPageCountHook(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return context(nand)->getPageCountNand();
|
||||
}
|
||||
|
||||
uint32_t getBlockCountHook(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return context(nand)->getBlockCountNand();
|
||||
}
|
||||
|
||||
uint32_t getBankCountHook(effaceable_nand_hal_t * nand)
|
||||
{
|
||||
return context(nand)->getBankCountNand();
|
||||
}
|
||||
|
||||
bool isBlockBadHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block)
|
||||
{
|
||||
return context(nand)->isBlockBadNand(bank, block);
|
||||
}
|
||||
|
||||
EffaceableReturn eraseBlockHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block)
|
||||
{
|
||||
return context(nand)->eraseBlockNand(bank, block);
|
||||
}
|
||||
|
||||
EffaceableReturn writePageHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block, uint32_t page, const void * buf, uint32_t length)
|
||||
{
|
||||
return context(nand)->writePageNand(bank, block, page, buf, length);
|
||||
}
|
||||
|
||||
EffaceableReturn readPageHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block, uint32_t page, void * buf, uint32_t length)
|
||||
{
|
||||
return context(nand)->readPageNand(bank, block, page, buf, length);
|
||||
}
|
||||
|
||||
bool requestPartitionTableDiffHook(effaceable_nand_hal_t * nand, void * buf, uint32_t length)
|
||||
{
|
||||
return context(nand)->requestPartitionTableDiffNand(buf, length);
|
||||
}
|
||||
|
||||
bool providePartitionTableDiffHook(effaceable_nand_hal_t * nand, void * buf, uint32_t length)
|
||||
{
|
||||
return context(nand)->providePartitionTableDiffNand(buf, length);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void
|
||||
AppleEffaceableNAND::setupNandContract(void)
|
||||
{
|
||||
nand()->opaque = this;
|
||||
|
||||
nand()->getPageSize = getPageSizeHook;
|
||||
nand()->getPageCount = getPageCountHook;
|
||||
nand()->getBlockCount = getBlockCountHook;
|
||||
nand()->getBankCount = getBankCountHook;
|
||||
nand()->isBlockBad = isBlockBadHook;
|
||||
nand()->eraseBlock = eraseBlockHook;
|
||||
nand()->writePage = writePageHook;
|
||||
nand()->readPage = readPageHook;
|
||||
nand()->requestPartitionTableDiff = requestPartitionTableDiffHook;
|
||||
nand()->providePartitionTableDiff = providePartitionTableDiffHook;
|
||||
}
|
||||
|
||||
uint32_t AppleEffaceableNAND::getPageSizeNand(void)
|
||||
{
|
||||
return (IOByteCount)_partition->getPageSize();
|
||||
}
|
||||
|
||||
uint32_t AppleEffaceableNAND::getPageCountNand(void)
|
||||
{
|
||||
return _partition->getPagesPerBlock();
|
||||
}
|
||||
|
||||
uint32_t AppleEffaceableNAND::getBlockCountNand(void)
|
||||
{
|
||||
return _partition->getBlockCount();
|
||||
}
|
||||
|
||||
uint32_t AppleEffaceableNAND::getBankCountNand(void)
|
||||
{
|
||||
return _partition->getBankCount();
|
||||
}
|
||||
|
||||
bool AppleEffaceableNAND::isBlockBadNand(uint32_t bank, uint32_t block)
|
||||
{
|
||||
return _partition->isBlockBad(bank, block);
|
||||
}
|
||||
|
||||
EffaceableReturn AppleEffaceableNAND::eraseBlockNand(uint32_t bank, uint32_t block)
|
||||
{
|
||||
return efReturn(_partition->eraseBlock(bank, block));
|
||||
}
|
||||
|
||||
EffaceableReturn AppleEffaceableNAND::writePageNand(uint32_t bank, uint32_t block, uint32_t page, const void * buf, uint32_t length)
|
||||
{
|
||||
IOReturn ok;
|
||||
IOBufferMemoryDescriptor * bmd;
|
||||
|
||||
// XXX It's unfortunate that the IOBMD is necessary; think about how/where to clean it up.
|
||||
|
||||
bmd = IOBufferMemoryDescriptor::withBytes(buf, length, kIODirectionInOut, true);
|
||||
if (0 == bmd)
|
||||
panic("no memory");
|
||||
|
||||
ok = _partition->writePage(bank, block, page, bmd);
|
||||
|
||||
bmd->release();
|
||||
|
||||
return efReturn(ok);
|
||||
}
|
||||
|
||||
EffaceableReturn AppleEffaceableNAND::readPageNand(uint32_t bank, uint32_t block, uint32_t page, void * buf, uint32_t length)
|
||||
{
|
||||
IOReturn ok;
|
||||
IOBufferMemoryDescriptor * bmd;
|
||||
|
||||
// XXX It's unfortunate that the IOBMD is necessary; think about how/where to clean it up.
|
||||
|
||||
bmd = IOBufferMemoryDescriptor::withCapacity(length, kIODirectionInOut, true);
|
||||
if (0 == bmd)
|
||||
panic("no memory");
|
||||
|
||||
ok = _partition->readPage(bank, block, page, bmd);
|
||||
|
||||
if (kIOReturnSuccess == ok)
|
||||
bmd->readBytes(0, buf, length);
|
||||
bmd->release();
|
||||
|
||||
return efReturn(ok);
|
||||
}
|
||||
|
||||
bool AppleEffaceableNAND::requestPartitionTableDiffNand(void * buf, uint32_t length)
|
||||
{
|
||||
return _partition->requestPartitionTableDiff(buf, length);
|
||||
}
|
||||
|
||||
bool AppleEffaceableNAND::providePartitionTableDiffNand(void * buf, uint32_t length)
|
||||
{
|
||||
return _partition->providePartitionTableDiff(buf, length);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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_NAND_H
|
||||
#define _APPLE_EFFACEABLE_NAND_H
|
||||
|
||||
#include <IOKit/IOService.h>
|
||||
#include <IOKit/IOBufferMemoryDescriptor.h>
|
||||
#include <IOKit/storage/flash/IOFlashStoragePartition.h>
|
||||
#include "AppleEffaceableStorage.h"
|
||||
|
||||
#include "effaceable_contract.h"
|
||||
|
||||
class AppleEffaceableNAND : public AppleEffaceableStorage
|
||||
{
|
||||
OSDeclareDefaultStructors(AppleEffaceableNAND);
|
||||
|
||||
public:
|
||||
virtual bool start(IOService * provider);
|
||||
|
||||
friend uint32_t getPageSizeHook(effaceable_nand_hal_t * nand);
|
||||
friend uint32_t getPageCountHook(effaceable_nand_hal_t * nand);
|
||||
friend uint32_t getBlockCountHook(effaceable_nand_hal_t * nand);
|
||||
friend uint32_t getBankCountHook(effaceable_nand_hal_t * nand);
|
||||
friend bool isBlockBadHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block);
|
||||
friend EffaceableReturn eraseBlockHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block);
|
||||
friend EffaceableReturn writePageHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block, uint32_t page, const void * buf, uint32_t length);
|
||||
friend EffaceableReturn readPageHook(effaceable_nand_hal_t * nand, uint32_t bank, uint32_t block, uint32_t page, void * buf, uint32_t length);
|
||||
friend bool requestPartitionTableDiffHook(effaceable_nand_hal_t * nand, void * buf, uint32_t length);
|
||||
friend bool providePartitionTableDiffHook(effaceable_nand_hal_t * nand, void * buf, uint32_t length);
|
||||
|
||||
protected:
|
||||
effaceable_nand_hal_t * nand(void);
|
||||
|
||||
virtual void setupDeviceContract(void);
|
||||
|
||||
private:
|
||||
void setupNandContract(void);
|
||||
|
||||
uint32_t getPageSizeNand(void);
|
||||
uint32_t getPageCountNand(void);
|
||||
uint32_t getBlockCountNand(void);
|
||||
uint32_t getBankCountNand(void);
|
||||
bool isBlockBadNand(uint32_t bank, uint32_t block);
|
||||
EffaceableReturn eraseBlockNand(uint32_t bank, uint32_t block);
|
||||
EffaceableReturn writePageNand(uint32_t bank, uint32_t block, uint32_t page, const void * buf, uint32_t length);
|
||||
EffaceableReturn readPageNand(uint32_t bank, uint32_t block, uint32_t page, void * buf, uint32_t length);
|
||||
bool requestPartitionTableDiffNand(void * buf, uint32_t length);
|
||||
bool providePartitionTableDiffNand(void * buf, uint32_t length);
|
||||
|
||||
IOFlashStoragePartition * _partition;
|
||||
effaceable_nand_hal_t _nand_hal;
|
||||
};
|
||||
|
||||
#endif /* _APPLE_NAND_EFFACEABLE_STORAGE_H */
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* 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 <IOKit/IOLib.h>
|
||||
|
||||
#include "AppleEffaceableNOR.h"
|
||||
|
||||
#include "effaceable_contract.h"
|
||||
#include "effaceable_debug.h"
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define debug(...) dlogf((void*)0, __VA_ARGS__ )
|
||||
|
||||
// =============================================================================
|
||||
|
||||
|
||||
#ifdef super
|
||||
#undef super
|
||||
#endif
|
||||
#define super AppleEffaceableStorage
|
||||
|
||||
OSDefineMetaClassAndStructors(AppleEffaceableNOR, AppleEffaceableStorage);
|
||||
|
||||
// =============================================================================
|
||||
|
||||
bool
|
||||
AppleEffaceableNOR::start(IOService *provider)
|
||||
{
|
||||
bool ok = false;
|
||||
|
||||
debug(INFO, "starting");
|
||||
|
||||
setupNorContract();
|
||||
|
||||
if (_is_started) {
|
||||
debug(INFO, "already started");
|
||||
ok = true;
|
||||
} else if (0 == (_nor_flash = OSDynamicCast(AppleARMNORFlashDevice, provider))) {
|
||||
debug(ERR, "bug in kext matching");
|
||||
} else if (!super::start(provider)) {
|
||||
debug(ERR, "super failed to start");
|
||||
} else {
|
||||
debug(INIT, "started");
|
||||
_is_started = true;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
effaceable_nor_hal_t *
|
||||
AppleEffaceableNOR::nor(void)
|
||||
{
|
||||
return &_nor_hal;
|
||||
}
|
||||
|
||||
void
|
||||
AppleEffaceableNOR::setupDeviceContract(void)
|
||||
{
|
||||
startEffaceableNOR(device(), system(), nor(), storage());
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
AppleEffaceableNOR * context(effaceable_nor_hal_t * nor)
|
||||
{
|
||||
AppleEffaceableNOR * context;
|
||||
|
||||
// XXX assert instead?
|
||||
if (0 == (context = (AppleEffaceableNOR *)nor->opaque))
|
||||
panic("null context");
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
uint32_t getRegionCountHook(effaceable_nor_hal_t * nor)
|
||||
{
|
||||
return context(nor)->getRegionCountNor();
|
||||
}
|
||||
|
||||
uint32_t getRegionSizeHook(effaceable_nor_hal_t * nor, uint32_t index)
|
||||
{
|
||||
return context(nor)->getRegionSizeNor(index);
|
||||
}
|
||||
|
||||
EffaceableReturn eraseRegionHook(effaceable_nor_hal_t * nor, uint32_t index, uint32_t length)
|
||||
{
|
||||
return context(nor)->eraseRegionNor(index, length);
|
||||
}
|
||||
|
||||
uint32_t writeRegionHook(effaceable_nor_hal_t * nor, uint32_t index, const void * buf, uint32_t length)
|
||||
{
|
||||
return context(nor)->writeRegionNor(index, buf, length);
|
||||
}
|
||||
|
||||
bool readRegionHook(effaceable_nor_hal_t * nor, uint32_t index, void * buf, uint32_t length)
|
||||
{
|
||||
return context(nor)->readRegionNor(index, buf, length);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void
|
||||
AppleEffaceableNOR::setupNorContract(void)
|
||||
{
|
||||
nor()->opaque = this;
|
||||
|
||||
nor()->getRegionCount = getRegionCountHook;
|
||||
nor()->getRegionSize = getRegionSizeHook;
|
||||
nor()->eraseRegion = eraseRegionHook;
|
||||
nor()->writeRegion = writeRegionHook;
|
||||
nor()->readRegion = readRegionHook;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
AppleEffaceableNOR::getRegionCountNor(void)
|
||||
{
|
||||
return _nor_flash->getRegionCount();
|
||||
}
|
||||
|
||||
uint32_t
|
||||
AppleEffaceableNOR::getRegionSizeNor(uint32_t index)
|
||||
{
|
||||
return _nor_flash->getRegionSize(index);
|
||||
}
|
||||
|
||||
EffaceableReturn
|
||||
AppleEffaceableNOR::eraseRegionNor(uint32_t index, uint32_t length)
|
||||
{
|
||||
return efReturn(_nor_flash->eraseRegion(index, 0, length));
|
||||
}
|
||||
|
||||
uint32_t
|
||||
AppleEffaceableNOR::writeRegionNor(uint32_t index, const void * buf, uint32_t length)
|
||||
{
|
||||
return _nor_flash->writeRegion(index, (const UInt8*)buf, length);
|
||||
}
|
||||
|
||||
bool
|
||||
AppleEffaceableNOR::readRegionNor(uint32_t index, void * buf, uint32_t length)
|
||||
{
|
||||
return _nor_flash->readRegion(index, (UInt8*)buf, length);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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_NOR_H
|
||||
#define _APPLE_EFFACEABLE_NOR_H
|
||||
|
||||
#include <IOKit/IOLib.h>
|
||||
#include <IOKit/IOService.h>
|
||||
#include <IOKit/platform/AppleARMNORFlashDevice.h>
|
||||
#include "AppleEffaceableStorage.h"
|
||||
|
||||
#include "effaceable_contract.h"
|
||||
|
||||
class AppleEffaceableNOR : public AppleEffaceableStorage
|
||||
{
|
||||
OSDeclareAbstractStructors(AppleEffaceableNOR);
|
||||
|
||||
public:
|
||||
virtual bool start(IOService * provider);
|
||||
|
||||
friend uint32_t getRegionCountHook(effaceable_nor_hal_t * nor);
|
||||
friend uint32_t getRegionSizeHook(effaceable_nor_hal_t * nor, uint32_t index);
|
||||
friend EffaceableReturn eraseRegionHook(effaceable_nor_hal_t * nor, uint32_t index, uint32_t length);
|
||||
friend uint32_t writeRegionHook(effaceable_nor_hal_t * nor, uint32_t index, const void * buf, uint32_t length);
|
||||
friend bool readRegionHook(effaceable_nor_hal_t * nor, uint32_t index, void * buf, uint32_t length);
|
||||
|
||||
protected:
|
||||
effaceable_nor_hal_t * nor(void);
|
||||
|
||||
virtual void setupDeviceContract(void);
|
||||
|
||||
private:
|
||||
void setupNorContract(void);
|
||||
|
||||
uint32_t getRegionCountNor(void);
|
||||
uint32_t getRegionSizeNor(uint32_t index);
|
||||
EffaceableReturn eraseRegionNor(uint32_t index, uint32_t length);
|
||||
uint32_t writeRegionNor(uint32_t index, const void * buf, uint32_t length);
|
||||
bool readRegionNor(uint32_t index, void * buf, uint32_t length);
|
||||
|
||||
AppleARMNORFlashDevice * _nor_flash;
|
||||
bool _is_started;
|
||||
effaceable_nor_hal_t _nor_hal;
|
||||
};
|
||||
|
||||
#endif /* _APPLE_EFFACEABLE_NOR_H */
|
||||
@@ -0,0 +1,545 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 <sys/types.h>
|
||||
#include <sys/random.h>
|
||||
|
||||
#include <libkern/zlib.h>
|
||||
#include <libkern/crypto/sha1.h>
|
||||
|
||||
#include <IOKit/IOTypes.h>
|
||||
#include <IOKit/IOLib.h>
|
||||
#include <IOKit/IOUserClient.h>
|
||||
|
||||
#include "AppleEffaceableStorageKeys.h"
|
||||
#include "AppleEffaceableStorageFormat.h"
|
||||
#include "AppleEffaceableStorage.h"
|
||||
|
||||
#include "effaceable_contract.h"
|
||||
#include "effaceable_debug.h"
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define debug(...) dlogf((void*)0, __VA_ARGS__ )
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#ifdef super
|
||||
#undef super
|
||||
#endif
|
||||
#define super IOService
|
||||
|
||||
OSDefineMetaClassAndAbstractStructors(AppleEffaceableStorage, IOService);
|
||||
|
||||
// =============================================================================
|
||||
|
||||
bool AppleEffaceableStorage::start(IOService *provider)
|
||||
{
|
||||
bool ok = false;
|
||||
|
||||
debug(INFO, "starting");
|
||||
|
||||
if (!super::start(provider)) {
|
||||
debug(ERR, "super did not start\n");
|
||||
} else if (NULL == (_work_loop = IOWorkLoop::workLoop())) {
|
||||
debug(ERR, "couldn't init work loop");
|
||||
} else if (NULL == (_command_gate = IOCommandGate::commandGate(this))) {
|
||||
debug(ERR, "couldn't acquire command gate");
|
||||
} else if ((NULL == (_function_get = OSSymbol::withCString(kAppleEffaceableStorageFunctionGet))) ||
|
||||
(NULL == (_function_set = OSSymbol::withCString(kAppleEffaceableStorageFunctionSet))) ||
|
||||
(NULL == (_function_space = OSSymbol::withCString(kAppleEffaceableStorageFunctionSpace))) ||
|
||||
(NULL == (_function_efface = OSSymbol::withCString(kAppleEffaceableStorageFunctionEfface))) ||
|
||||
(NULL == (_function_gen_nonce = OSSymbol::withCString(kAppleEffaceableStorageFunctionGenerateNonce)))) {
|
||||
debug(ERR, "couldn't allocate function symbols");
|
||||
} else {
|
||||
|
||||
// add command gate to work loop
|
||||
getWorkLoop()->addEventSource(_command_gate);
|
||||
|
||||
// commandgate actions
|
||||
#define _ACTION_CAST(_x) OSMemberFunctionCast(IOCommandGate::Action, this, _x)
|
||||
_get_action = _ACTION_CAST(&AppleEffaceableStorage::getLockerGated);
|
||||
_set_action = _ACTION_CAST(&AppleEffaceableStorage::setLockerGated);
|
||||
_space_action = _ACTION_CAST(&AppleEffaceableStorage::spaceForLockerGated);
|
||||
_efface_action = _ACTION_CAST(&AppleEffaceableStorage::effaceLockerGated);
|
||||
_gen_nonce_action = _ACTION_CAST(&AppleEffaceableStorage::generateNonceGated);
|
||||
_format_action = _ACTION_CAST(&AppleEffaceableStorage::formatStorageGated);
|
||||
_get_bytes_action = _ACTION_CAST(&AppleEffaceableStorage::getBytesGated);
|
||||
_wipe_action = _ACTION_CAST(&AppleEffaceableStorage::wipeStorageGated);
|
||||
#undef _ACTION_CAST
|
||||
|
||||
// initalize portable core
|
||||
setupSystemContract();
|
||||
setupDeviceContract();
|
||||
|
||||
registerService();
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
if (NULL != _command_gate) {
|
||||
_command_gate->release();
|
||||
_command_gate = NULL;
|
||||
}
|
||||
if (NULL != _work_loop) {
|
||||
_work_loop->release();
|
||||
_work_loop = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
void
|
||||
AppleEffaceableStorage::registerService(IOOptionBits options)
|
||||
{
|
||||
const OSSymbol * user_client;
|
||||
|
||||
// hook up the user client
|
||||
user_client = OSSymbol::withCStringNoCopy("AppleEffaceableStorageUserClient");
|
||||
setProperty(gIOUserClientClassKey, (OSObject *)user_client);
|
||||
user_client->release();
|
||||
|
||||
super::registerService(options);
|
||||
}
|
||||
|
||||
IOWorkLoop *
|
||||
AppleEffaceableStorage::getWorkLoop() const
|
||||
{
|
||||
return _work_loop;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
IOByteCount
|
||||
AppleEffaceableStorage::getCapacity(void)
|
||||
{
|
||||
return (IOByteCount)storage()->getCapacity(storage());
|
||||
}
|
||||
|
||||
bool
|
||||
AppleEffaceableStorage::isFormatted(void)
|
||||
{
|
||||
return storage()->isFormatted(storage());
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::formatStorage(void)
|
||||
{
|
||||
return _command_gate->runAction(_format_action);
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::getBytes(void * client_buf, IOByteCount offset, IOByteCount count)
|
||||
{
|
||||
if (PE_i_can_has_debugger(NULL)) {
|
||||
return _command_gate->runAction(_get_bytes_action, (void *) client_buf, (void *) offset, (void *) count);
|
||||
} else {
|
||||
return kIOReturnUnsupported;
|
||||
}
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::wipeStorage(void)
|
||||
{
|
||||
if (PE_i_can_has_debugger(NULL)) {
|
||||
return _command_gate->runAction(_wipe_action);
|
||||
} else {
|
||||
return kIOReturnUnsupported;
|
||||
}
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::getLocker(UInt32 type_id, void *data, IOByteCount *data_size, bool untrusted)
|
||||
{
|
||||
return _command_gate->runAction(_get_action, (void *)type_id, data, (void *)data_size, (void *)untrusted);
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::setLocker(UInt32 type_id, const void *data, IOByteCount data_size, bool untrusted)
|
||||
{
|
||||
return _command_gate->runAction(_set_action, (void *)type_id, (void *)data, (void *)data_size, (void *)untrusted);
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::spaceForLocker(UInt32 type_id, IOByteCount *data_space)
|
||||
{
|
||||
return _command_gate->runAction(_space_action, (void *)type_id, (void *)data_space);
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::effaceLocker(UInt32 type_id, bool untrusted)
|
||||
{
|
||||
return _command_gate->runAction(_efface_action, (void *)type_id, (void *)untrusted);
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::generateNonce(void * hash)
|
||||
{
|
||||
return _command_gate->runAction(_gen_nonce_action, hash);
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::callPlatformFunction(
|
||||
const OSSymbol *functionSymbol,
|
||||
bool waitForFunction,
|
||||
void *param1,
|
||||
void *param2,
|
||||
void *param3,
|
||||
void *param4)
|
||||
{
|
||||
if (functionSymbol == _function_get)
|
||||
return getLocker((UInt32)(uintptr_t)param1, param2, (IOByteCount *)param3, (bool)param4);
|
||||
|
||||
if (functionSymbol == _function_set)
|
||||
return setLocker((UInt32)(uintptr_t)param1, param2, (IOByteCount)param3, (bool)param4);
|
||||
|
||||
if (functionSymbol == _function_space)
|
||||
return effaceLocker((UInt32)(uintptr_t)param1, (IOByteCount *)param2);
|
||||
|
||||
if (functionSymbol == _function_efface)
|
||||
return effaceLocker((UInt32)(uintptr_t)param1, (bool)param2);
|
||||
|
||||
if (functionSymbol == _function_gen_nonce)
|
||||
return generateNonce(param1);
|
||||
|
||||
return(IOService::callPlatformFunction(functionSymbol, waitForFunction, param1, param2, param3, param4));
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::formatStorageGated(void)
|
||||
{
|
||||
return ioReturn(storage()->formatStorage(storage()));
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::getBytesGated(void * client_buf, IOByteCount offset, IOByteCount count)
|
||||
{
|
||||
return ioReturn(storage()->getBytes(storage(), client_buf, offset, count));
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::wipeStorageGated(void * tmp_buf)
|
||||
{
|
||||
return ioReturn(storage()->wipeStorage(storage(), tmp_buf));
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::getLockerGated(UInt32 type_id, void *data, IOByteCount *data_size, bool untrusted)
|
||||
{
|
||||
return ioReturn(storage()->getLocker(storage(), type_id, data, (uint32_t *)data_size, untrusted));
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::setLockerGated(UInt32 type_id, const void *data, IOByteCount data_size, bool untrusted)
|
||||
{
|
||||
return ioReturn(storage()->setLocker(storage(), type_id, data, (uint32_t)data_size, untrusted));
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::spaceForLockerGated(UInt32 type_id, IOByteCount *data_space)
|
||||
{
|
||||
return ioReturn(storage()->spaceForLocker(storage(), type_id, (uint32_t *)data_space));
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::effaceLockerGated(UInt32 type_id, bool untrusted)
|
||||
{
|
||||
return ioReturn(storage()->effaceLocker(storage(), type_id, untrusted));
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::generateNonceGated(void * hash)
|
||||
{
|
||||
return ioReturn(storage()->generateNonce(storage(), hash));
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
effaceable_storage_t *
|
||||
AppleEffaceableStorage::storage(void)
|
||||
{
|
||||
return &_storage;
|
||||
}
|
||||
|
||||
effaceable_device_t *
|
||||
AppleEffaceableStorage::device(void)
|
||||
{
|
||||
return &_device;
|
||||
}
|
||||
|
||||
effaceable_system_t *
|
||||
AppleEffaceableStorage::system(void)
|
||||
{
|
||||
return &_system;
|
||||
}
|
||||
|
||||
EffaceableReturn
|
||||
AppleEffaceableStorage::efReturn(IOReturn ret)
|
||||
{
|
||||
EffaceableReturn cast = kEffaceableReturnInvalid;
|
||||
switch (ret) {
|
||||
case kIOReturnSuccess : cast = kEffaceableReturnSuccess ; break;
|
||||
case kIOReturnError : cast = kEffaceableReturnError ; break;
|
||||
case kIOReturnNotFound : cast = kEffaceableReturnNotFound ; break;
|
||||
case kIOReturnUnsupported : cast = kEffaceableReturnUnsupported ; break;
|
||||
case kIOReturnNotPrivileged : cast = kEffaceableReturnNotPrivileged ; break;
|
||||
case kIOReturnNotPermitted : cast = kEffaceableReturnNotPermitted ; break;
|
||||
case kIOReturnBadArgument : cast = kEffaceableReturnBadArgument ; break;
|
||||
case kIOReturnBadMedia : cast = kEffaceableReturnBadMedia ; break;
|
||||
case kIOReturnIOError : cast = kEffaceableReturnIOError ; break;
|
||||
case kIOReturnNoMemory : cast = kEffaceableReturnNoMemory ; break;
|
||||
case kIOReturnNoSpace : cast = kEffaceableReturnNoSpace ; break;
|
||||
case kIOReturnInternalError : cast = kEffaceableReturnInternalError ; break;
|
||||
case kIOReturnUnformattedMedia : cast = kEffaceableReturnUnformattedMedia; break;
|
||||
}
|
||||
return cast;
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorage::ioReturn(EffaceableReturn ret)
|
||||
{
|
||||
IOReturn cast = kIOReturnInvalid;
|
||||
switch (ret) {
|
||||
case kEffaceableReturnSuccess : cast = kIOReturnSuccess ; break;
|
||||
case kEffaceableReturnError : cast = kIOReturnError ; break;
|
||||
case kEffaceableReturnNotFound : cast = kIOReturnNotFound ; break;
|
||||
case kEffaceableReturnUnsupported : cast = kIOReturnUnsupported ; break;
|
||||
case kEffaceableReturnNotPrivileged : cast = kIOReturnNotPrivileged ; break;
|
||||
case kEffaceableReturnNotPermitted : cast = kIOReturnNotPermitted ; break;
|
||||
case kEffaceableReturnBadArgument : cast = kIOReturnBadArgument ; break;
|
||||
case kEffaceableReturnBadMedia : cast = kIOReturnBadMedia ; break;
|
||||
case kEffaceableReturnIOError : cast = kIOReturnIOError ; break;
|
||||
case kEffaceableReturnNoMemory : cast = kIOReturnNoMemory ; break;
|
||||
case kEffaceableReturnNoSpace : cast = kIOReturnNoSpace ; break;
|
||||
case kEffaceableReturnInternalError : cast = kIOReturnInternalError ; break;
|
||||
case kEffaceableReturnUnformattedMedia : cast = kIOReturnUnformattedMedia; break;
|
||||
}
|
||||
return cast;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
int
|
||||
AppleEffaceableStorage::logf(void * ignored, const char * fmt, ...)
|
||||
{
|
||||
int err;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
err = vlogfSys(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void
|
||||
AppleEffaceableStorage::handleBootArgs(void)
|
||||
{
|
||||
uint32_t arg_flag = 0;
|
||||
bool enable_wipe = false;
|
||||
bool enable_full_scan = false;
|
||||
|
||||
// boot-arg support to enable/disable of media wipe
|
||||
if (PE_parse_boot_argn("effaceable-enable-wipe", &arg_flag, sizeof (arg_flag)) && arg_flag) {
|
||||
enable_wipe = (0 != arg_flag);
|
||||
// XXX plumb down through to core implementation
|
||||
debug(INIT, "wipe %sabled via boot-arg", enable_wipe ? "en" : "dis");
|
||||
}
|
||||
|
||||
// boot-arg support to enable/disable full scan
|
||||
if (PE_parse_boot_argn("effaceable-enable-full-scan", &arg_flag, sizeof (arg_flag)) && arg_flag) {
|
||||
enable_full_scan = (0 != arg_flag);
|
||||
// XXX plumb down through to core implementation
|
||||
debug(INIT, "full scan %sabled via boot-arg", enable_full_scan ? "en" : "dis");
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
AppleEffaceableStorage * context(effaceable_system_t * system)
|
||||
{
|
||||
AppleEffaceableStorage * context;
|
||||
|
||||
// XXX assert instead?
|
||||
if (NULL == (context = (AppleEffaceableStorage *)system->opaque))
|
||||
panic("null context");
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
void * allocMemHook(effaceable_system_t * system, uint32_t size)
|
||||
{
|
||||
return context(system)->allocMemSys(size);
|
||||
}
|
||||
|
||||
void freeMemHook(effaceable_system_t * system, void * buf, uint32_t size)
|
||||
{
|
||||
context(system)->freeMemSys(buf, size);
|
||||
}
|
||||
|
||||
void * setMemHook(effaceable_system_t * system, void * buf, uint8_t val, uint32_t size)
|
||||
{
|
||||
return context(system)->setMemSys(buf, val, size);
|
||||
}
|
||||
|
||||
void * moveMemHook(effaceable_system_t * system, void * dst, const void * src, uint32_t size)
|
||||
{
|
||||
return context(system)->moveMemSys(dst, src, size);
|
||||
}
|
||||
|
||||
int cmpMemHook(effaceable_system_t * system, const void * lhs, const void * rhs, uint32_t size)
|
||||
{
|
||||
return context(system)->cmpMemSys(lhs, rhs, size);
|
||||
}
|
||||
|
||||
void readRandomHook(effaceable_system_t * system, void * buf, uint32_t size)
|
||||
{
|
||||
context(system)->readRandomSys(buf, size);
|
||||
}
|
||||
|
||||
void calcSHA1Hook(effaceable_system_t * system, const void * buf, uint32_t size, void * hash)
|
||||
{
|
||||
return context(system)->calcSHA1Sys(buf, size, hash);
|
||||
}
|
||||
|
||||
uint32_t crc32Hook(effaceable_system_t * system, uint32_t crc, const void * buf, uint32_t size, bool ignored)
|
||||
{
|
||||
return context(system)->crc32Sys(crc, buf, size);
|
||||
}
|
||||
|
||||
bool setPropertyHook(effaceable_system_t * system, const char * key, uint32_t value)
|
||||
{
|
||||
return context(system)->setPropertySys(key, value);
|
||||
}
|
||||
|
||||
void panicHook(effaceable_system_t * system, const char * msg)
|
||||
{
|
||||
context(system)->panicSys(msg);
|
||||
}
|
||||
|
||||
int vlogfHook(effaceable_system_t * system, const char * fmt, va_list ap)
|
||||
{
|
||||
return context(system)->vlogfSys(fmt, ap);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void
|
||||
AppleEffaceableStorage::setupSystemContract(void)
|
||||
{
|
||||
// XXX convert to appropriate type of AssertMacros
|
||||
if (sizeof(uint32_t) != sizeof(IOByteCount))
|
||||
panic("(sizeof(uint32_t) != sizeof(IOByteCount))");
|
||||
|
||||
system()->opaque = this;
|
||||
|
||||
system()->allocMem = allocMemHook;
|
||||
system()->freeMem = freeMemHook;
|
||||
system()->setMem = setMemHook;
|
||||
system()->moveMem = moveMemHook;
|
||||
system()->cmpMem = cmpMemHook;
|
||||
system()->readRandom = readRandomHook;
|
||||
system()->calcSHA1 = calcSHA1Hook;
|
||||
system()->crc32 = crc32Hook;
|
||||
system()->setProperty = setPropertyHook;
|
||||
system()->panicSys = panicHook;
|
||||
system()->vlogf = vlogfHook;
|
||||
}
|
||||
|
||||
void *
|
||||
AppleEffaceableStorage::allocMemSys(uint32_t size)
|
||||
{
|
||||
void * buf = IOMalloc(size);
|
||||
|
||||
if (0 == buf) {
|
||||
panic("no memory");
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void
|
||||
AppleEffaceableStorage::freeMemSys(void * buf, uint32_t size)
|
||||
{
|
||||
IOFree(buf, size);
|
||||
}
|
||||
|
||||
void *
|
||||
AppleEffaceableStorage::setMemSys(void * buf, uint8_t val, uint32_t size)
|
||||
{
|
||||
return memset(buf, val, size);
|
||||
}
|
||||
|
||||
void *
|
||||
AppleEffaceableStorage::moveMemSys(void * dst, const void * src, uint32_t size)
|
||||
{
|
||||
return memmove(dst, src, size);
|
||||
}
|
||||
|
||||
int
|
||||
AppleEffaceableStorage::cmpMemSys(const void * lhs, const void * rhs, uint32_t size)
|
||||
{
|
||||
return memcmp(lhs, rhs, size);
|
||||
}
|
||||
|
||||
void
|
||||
AppleEffaceableStorage::readRandomSys(void * buf, uint32_t size)
|
||||
{
|
||||
read_random(buf, size);
|
||||
}
|
||||
|
||||
void
|
||||
AppleEffaceableStorage::calcSHA1Sys(const void * buf, uint32_t size, void * hash)
|
||||
{
|
||||
SHA1_CTX ctx;
|
||||
|
||||
SHA1Init(&ctx);
|
||||
SHA1Update(&ctx, buf, size);
|
||||
SHA1Final(hash, &ctx);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
AppleEffaceableStorage::crc32Sys(uint32_t crc, const void * buf, uint32_t size)
|
||||
{
|
||||
return crc32(crc, buf, size);
|
||||
}
|
||||
|
||||
bool
|
||||
AppleEffaceableStorage::setPropertySys(const char * key, uint32_t value)
|
||||
{
|
||||
return setProperty(key, value, 32);
|
||||
}
|
||||
|
||||
void
|
||||
AppleEffaceableStorage::panicSys(const char * msg)
|
||||
{
|
||||
panic(msg);
|
||||
}
|
||||
|
||||
int
|
||||
AppleEffaceableStorage::vlogfSys(const char * fmt, va_list ap)
|
||||
{
|
||||
IOLogv(fmt, ap);
|
||||
|
||||
// XXX Given that IOLogv doesn't return character printed count,
|
||||
// as least common denominator, it's probably best to reduce vlogf
|
||||
// contract to void return value.
|
||||
return 0;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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_H
|
||||
#define _APPLE_EFFACEABLE_STORAGE_H
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#include <IOKit/IOLib.h>
|
||||
#include <IOKit/IOService.h>
|
||||
#include <IOKit/IOCommandGate.h>
|
||||
|
||||
#include "AppleEffaceableStorageKeys.h"
|
||||
#include "effaceable_contract.h"
|
||||
|
||||
// =============================================================================
|
||||
|
||||
class AppleEffaceableStorage : public IOService
|
||||
{
|
||||
OSDeclareAbstractStructors(AppleEffaceableStorage);
|
||||
|
||||
public:
|
||||
/* client API */
|
||||
IOReturn getLocker(UInt32 type_id, void *data, IOByteCount *data_size, bool untrusted = false);
|
||||
IOReturn setLocker(UInt32 type_id, const void *data, IOByteCount data_size, bool untrusted = false);
|
||||
IOReturn spaceForLocker(UInt32 type_id, IOByteCount *data_space);
|
||||
IOReturn effaceLocker(UInt32 type_id, bool untrusted = false);
|
||||
IOReturn generateNonce(void * hash);
|
||||
|
||||
/* additional public functions intended for manufacturing and/or diagnostics only */
|
||||
IOReturn getBytes(void * client_buf, IOByteCount offset, IOByteCount count);
|
||||
IOReturn wipeStorage(void);
|
||||
IOByteCount getCapacity(void);
|
||||
bool isFormatted(void);
|
||||
IOReturn formatStorage(void);
|
||||
|
||||
protected:
|
||||
/* subclass SPI */
|
||||
effaceable_storage_t * storage(void);
|
||||
effaceable_device_t * device(void);
|
||||
effaceable_system_t * system(void);
|
||||
|
||||
EffaceableReturn efReturn(IOReturn ret);
|
||||
IOReturn ioReturn(EffaceableReturn ret);
|
||||
|
||||
int logf(void * ignored, const char * fmt, ...);
|
||||
|
||||
virtual void setupDeviceContract(void) = 0;
|
||||
|
||||
public:
|
||||
/* IOKit hooks */
|
||||
virtual bool start(IOService * provider);
|
||||
virtual void registerService(IOOptionBits options = 0);
|
||||
virtual IOWorkLoop * getWorkLoop() const;
|
||||
virtual IOReturn callPlatformFunction(const OSSymbol *functionSymbol, bool waitForFunction,
|
||||
void *param1, void *param2, void *param3, void *param4);
|
||||
|
||||
private:
|
||||
/* gated versions of public methods */
|
||||
IOReturn wipeStorageGated(void * tmp_buf);
|
||||
IOReturn formatStorageGated(void);
|
||||
IOReturn getBytesGated(void * client_buf, IOByteCount offset, IOByteCount count);
|
||||
IOReturn getLockerGated(UInt32 type_id, void *data, IOByteCount *data_size, bool untrusted);
|
||||
IOReturn setLockerGated(UInt32 type_id, const void *data, IOByteCount data_size, bool untrusted);
|
||||
IOReturn spaceForLockerGated(UInt32 type_id, IOByteCount *data_spage);
|
||||
IOReturn effaceLockerGated(UInt32 type_id, bool untrusted);
|
||||
IOReturn generateNonceGated(void * hash);
|
||||
|
||||
/* methods to help get portable core setup and configured */
|
||||
void setupSystemContract(void);
|
||||
void handleBootArgs(void);
|
||||
|
||||
/* implementation of portable core system services contract */
|
||||
void * allocMemSys(uint32_t size);
|
||||
void freeMemSys(void * buf, uint32_t size);
|
||||
void * setMemSys(void * buf, uint8_t val, uint32_t size);
|
||||
void * moveMemSys(void * dst, const void * src, uint32_t size);
|
||||
int cmpMemSys(const void * lhs, const void * rhs, uint32_t size);
|
||||
void readRandomSys(void * buf, uint32_t size);
|
||||
void calcSHA1Sys(const void * buf, uint32_t size, void * hash);
|
||||
uint32_t crc32Sys(uint32_t crc, const void * buf, uint32_t size);
|
||||
bool setPropertySys(const char * key, uint32_t value);
|
||||
void panicSys(const char * msg);
|
||||
int vlogfSys(const char * fmt, va_list ap);
|
||||
|
||||
private:
|
||||
/* friend C function hooks implementing trampoline back to C++ for portable core system contract */
|
||||
friend void * allocMemHook(effaceable_system_t * system, uint32_t size);
|
||||
friend void freeMemHook(effaceable_system_t * system, void * buf, uint32_t size);
|
||||
friend void * setMemHook(effaceable_system_t * system, void * buf, uint8_t val, uint32_t size);
|
||||
friend void * moveMemHook(effaceable_system_t * system, void * dst, const void * src, uint32_t size);
|
||||
friend int cmpMemHook(effaceable_system_t * system, const void * lhs, const void * rhs, uint32_t size);
|
||||
friend void readRandomHook(effaceable_system_t * system, void * buf, uint32_t size);
|
||||
friend void calcSHA1Hook(effaceable_system_t * system, const void * buf, uint32_t size, void * hash);
|
||||
friend uint32_t crc32Hook(effaceable_system_t * system, uint32_t crc, const void * buf, uint32_t size, bool ignored);
|
||||
friend bool setPropertyHook(effaceable_system_t * system, const char * key, uint32_t value);
|
||||
friend void panicHook(effaceable_system_t * system, const char * msg);
|
||||
friend int vlogfHook(effaceable_system_t * system, const char * fmt, va_list ap);
|
||||
|
||||
private:
|
||||
/* implementation data */
|
||||
IOCommandGate * _command_gate;
|
||||
IOWorkLoop * _work_loop;
|
||||
const OSSymbol * _function_get;
|
||||
const OSSymbol * _function_set;
|
||||
const OSSymbol * _function_space;
|
||||
const OSSymbol * _function_efface;
|
||||
const OSSymbol * _function_gen_nonce;
|
||||
IOCommandGate::Action _get_action;
|
||||
IOCommandGate::Action _set_action;
|
||||
IOCommandGate::Action _space_action;
|
||||
IOCommandGate::Action _efface_action;
|
||||
IOCommandGate::Action _gen_nonce_action;
|
||||
IOCommandGate::Action _format_action;
|
||||
IOCommandGate::Action _wipe_action;
|
||||
IOCommandGate::Action _get_bytes_action;
|
||||
effaceable_storage_t _storage;
|
||||
effaceable_system_t _system;
|
||||
effaceable_device_t _device;
|
||||
};
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#endif /* _APPLE_EFFACEABLE_STORAGE_H */
|
||||
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 <IOKit/IOTypes.h>
|
||||
#include <IOKit/IOLib.h>
|
||||
#include <IOKit/IOUserClient.h>
|
||||
|
||||
#include "AppleEffaceableStorage.h"
|
||||
#include "AppleEffaceableStorageKeys.h"
|
||||
#include "AppleEffaceableStorageUserClient.h"
|
||||
|
||||
// =============================================================================
|
||||
|
||||
// XXX correct hard-coding of SHA1 buffer size
|
||||
#define SHA1_HASH_SIZE 20
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_INIT (1<<0)
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_ERR (1<<1)
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_WARN (1<<2)
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_INFO (1<<3)
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_SPEW (1<<4)
|
||||
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGTAG_INIT "INIT"
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGTAG_ERR "ERROR"
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGTAG_WARN "WARNING"
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGTAG_INFO "INFO"
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DBGTAG_SPEW "SPEW"
|
||||
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DEBUG_ALL (APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_INIT | \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_ERR | \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_WARN | \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_INFO | \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_SPEW)
|
||||
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DEBUG_MOST (APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_INIT | \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_ERR | \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_WARN | \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_INFO)
|
||||
|
||||
#define APPLE_EFFACEABLE_USER_CLIENT_DEBUG_DEFAULT (APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_INIT | \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_ERR)
|
||||
|
||||
#ifdef DEBUG_BUILD
|
||||
# define APPLE_EFFACEABLE_USER_CLIENT_DEBUG APPLE_EFFACEABLE_USER_CLIENT_DEBUG_MOST
|
||||
#else
|
||||
# define APPLE_EFFACEABLE_USER_CLIENT_DEBUG APPLE_EFFACEABLE_USER_CLIENT_DEBUG_DEFAULT
|
||||
#endif
|
||||
|
||||
#if defined(APPLE_EFFACEABLE_USER_CLIENT_DEBUG) && APPLE_EFFACEABLE_USER_CLIENT_DEBUG
|
||||
# define debug(fac, fmt, args...) \
|
||||
do { \
|
||||
if (APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_##fac & \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DEBUG) { \
|
||||
kprintf("AppleEffaceableStorageUserClient::%s():[%s] " fmt "\n", \
|
||||
__FUNCTION__, \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGTAG_##fac , \
|
||||
##args ); \
|
||||
} \
|
||||
} while(0)
|
||||
# define hexdump(fac, bytes, count) \
|
||||
do { \
|
||||
if (APPLE_EFFACEABLE_USER_CLIENT_DBGMSK_##fac & \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DEBUG) { \
|
||||
uint32_t i, j; \
|
||||
for (i = 0; i < count; i += 16) { \
|
||||
kprintf("AppleEffaceableStorageUserClient::%s():[%s] 0x%08x:", \
|
||||
__FUNCTION__, \
|
||||
APPLE_EFFACEABLE_USER_CLIENT_DBGTAG_##fac , \
|
||||
((uint32_t) bytes) + i); \
|
||||
for (j = 0; (j < 16) && ((i + j) < count); j++) \
|
||||
kprintf(" %2.2x", ((uint8_t*)bytes)[i+j]); \
|
||||
kprintf("\n"); \
|
||||
} \
|
||||
} \
|
||||
} while(0)
|
||||
#else
|
||||
# define debug(fac, fmt, args...) do { /* nothing */ } while(0)
|
||||
# define hexdump(fac, bytes, count) do { /* nothing */ } while(0)
|
||||
#endif
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#ifdef super
|
||||
#undef super
|
||||
#endif
|
||||
#define super IOUserClient
|
||||
|
||||
OSDefineMetaClassAndStructors(AppleEffaceableStorageUserClient, IOUserClient);
|
||||
|
||||
// =============================================================================
|
||||
|
||||
bool
|
||||
AppleEffaceableStorageUserClient::start(IOService *provider)
|
||||
{
|
||||
bool ok = false;
|
||||
|
||||
_provider = OSDynamicCast(AppleEffaceableStorage, provider);
|
||||
|
||||
if (NULL == _provider) {
|
||||
debug(ERR, "bug in kext matching");
|
||||
} else if (!super::start(provider)) {
|
||||
debug(ERR, "super failed to start");
|
||||
} else {
|
||||
debug(INFO, "started");
|
||||
ok = true;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorageUserClient::clientClose(void)
|
||||
{
|
||||
terminate();
|
||||
return kIOReturnSuccess;
|
||||
}
|
||||
|
||||
IOReturn
|
||||
AppleEffaceableStorageUserClient::externalMethod(uint32_t selector,
|
||||
IOExternalMethodArguments * arguments,
|
||||
IOExternalMethodDispatch *dispatch,
|
||||
OSObject *target,
|
||||
void *reference)
|
||||
{
|
||||
IOReturn ok = kIOReturnSuccess;
|
||||
bool secure_root;
|
||||
|
||||
// require administrator privilege
|
||||
if (kIOReturnSuccess != clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator)) {
|
||||
debug(ERR, "not authorized");
|
||||
return kIOReturnNotPrivileged;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_BUILD
|
||||
secure_root = true;
|
||||
#else
|
||||
// some operations require a secure root
|
||||
if (kIOReturnSuccess != (ok = _provider->getProvider()->callPlatformFunction("SecureRoot",
|
||||
false,
|
||||
_provider->getProvider(),
|
||||
&secure_root,
|
||||
0, 0))) {
|
||||
debug(ERR, "failed to determine root trust state");
|
||||
secure_root = false;
|
||||
}
|
||||
#endif // DEBUG_BUILD
|
||||
|
||||
switch (selector) {
|
||||
case kAppleEffaceableStorageMethodGetCapacity:
|
||||
|
||||
if (arguments->scalarOutputCount != 1) {
|
||||
debug(ERR, "getCapacity: missing output argument");
|
||||
ok = kIOReturnBadArgument;
|
||||
} else {
|
||||
arguments->scalarOutput[0] = _provider->getCapacity();
|
||||
}
|
||||
break;
|
||||
|
||||
case kAppleEffaceableStorageMethodIsFormatted:
|
||||
|
||||
if (arguments->scalarOutputCount != 1) {
|
||||
debug(ERR, "isFormatted: missing output argument");
|
||||
ok = kIOReturnBadArgument;
|
||||
} else {
|
||||
arguments->scalarOutput[0] = _provider->isFormatted();
|
||||
}
|
||||
break;
|
||||
|
||||
case kAppleEffaceableStorageMethodFormatStorage:
|
||||
|
||||
if (!secure_root) {
|
||||
debug(ERR, "format attempt from untrusted root");
|
||||
} else {
|
||||
ok = _provider->formatStorage();
|
||||
}
|
||||
break;
|
||||
|
||||
case kAppleEffaceableStorageMethodWipeStorage:
|
||||
|
||||
if (!secure_root) {
|
||||
debug(ERR, "wipe attempt from untrusted root");
|
||||
} else {
|
||||
ok = _provider->wipeStorage();
|
||||
}
|
||||
break;
|
||||
|
||||
case kAppleEffaceableStorageMethodGetLocker:
|
||||
|
||||
if ((arguments->scalarInputCount != 1) ||
|
||||
(arguments->scalarOutputCount != 1)) {
|
||||
debug(ERR, "getLocker: missing input/output arguments");
|
||||
ok = kIOReturnBadArgument;
|
||||
} else {
|
||||
IOByteCount data_size = arguments->structureOutputSize;
|
||||
ok = _provider->getLocker(arguments->scalarInput[0],
|
||||
arguments->structureOutput,
|
||||
&data_size,
|
||||
!secure_root);
|
||||
arguments->scalarOutput[0] = data_size;
|
||||
// if we didn't clip the data, report the actual output size
|
||||
if (data_size < arguments->structureOutputSize)
|
||||
arguments->structureOutputSize = data_size;
|
||||
}
|
||||
break;
|
||||
|
||||
case kAppleEffaceableStorageMethodSetLocker:
|
||||
|
||||
if ((arguments->scalarInputCount != 1) ||
|
||||
(arguments->structureInputSize < 1)) {
|
||||
debug(ERR, "setLocker: missing input arguments");
|
||||
ok = kIOReturnBadArgument;
|
||||
} else {
|
||||
ok = _provider->setLocker(arguments->scalarInput[0],
|
||||
arguments->structureInput,
|
||||
arguments->structureInputSize,
|
||||
!secure_root);
|
||||
}
|
||||
break;
|
||||
|
||||
case kAppleEffaceableStorageMethodSpace:
|
||||
|
||||
if ((arguments->scalarInputCount != 1) ||
|
||||
(arguments->scalarOutputCount != 1)) {
|
||||
debug(ERR, "lockerSpace: missing input/output arguments");
|
||||
ok = kIOReturnBadArgument;
|
||||
} else {
|
||||
IOByteCount data_space;
|
||||
ok = _provider->spaceForLocker(arguments->scalarInput[0],
|
||||
&data_space);
|
||||
arguments->scalarOutput[0] = data_space;
|
||||
}
|
||||
break;
|
||||
|
||||
case kAppleEffaceableStorageMethodEffaceLocker:
|
||||
|
||||
if (arguments->scalarInputCount != 1) {
|
||||
debug(ERR, "effaceLocker: missing input argument");
|
||||
ok = kIOReturnBadArgument;
|
||||
} else if (!secure_root && arguments->scalarInput[0] == kAppleEffaceableStorageLockerWildcard) {
|
||||
debug(ERR, "effaceLocker: untrusted attempt to efface by wildcard");
|
||||
ok = kIOReturnNotPermitted;
|
||||
} else {
|
||||
ok = _provider->effaceLocker(arguments->scalarInput[0], !secure_root);
|
||||
}
|
||||
break;
|
||||
|
||||
case kAppleEffaceableStorageMethodGenerateNonce:
|
||||
|
||||
if (arguments->structureOutputSize != SHA1_HASH_SIZE) {
|
||||
debug(ERR, "generateNonce: missing output argument");
|
||||
ok = kIOReturnBadArgument;
|
||||
} else {
|
||||
ok = _provider->generateNonce(arguments->structureOutput);
|
||||
if (kIOReturnSuccess != ok) {
|
||||
arguments->structureOutputSize = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
//
|
||||
// Methods below are not for production use and may only be used when kernel debug enabled via boot-args.
|
||||
//
|
||||
|
||||
case kAppleEffaceableStorageMethodGetBytes:
|
||||
|
||||
if ((arguments->scalarInputCount != 1) ||
|
||||
(arguments->structureOutputSize < 1)) {
|
||||
ok = kIOReturnBadArgument;
|
||||
} else if (!PE_i_can_has_debugger(NULL)) {
|
||||
debug(ERR, "getBytes is only allowed when kernel debug is enabled");
|
||||
ok = kIOReturnUnsupported;
|
||||
} else {
|
||||
ok = _provider->getBytes(arguments->structureOutput, arguments->scalarInput[0], arguments->structureOutputSize);
|
||||
if (kIOReturnSuccess != ok) {
|
||||
debug(ERR, "getBytes from user client to service failed");
|
||||
} else {
|
||||
debug(SPEW, "read %u bytes from offset %llu", arguments->structureOutputSize, arguments->scalarInput[0]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
debug(ERR, "bad selector %u", selector);
|
||||
ok = kIOReturnUnsupported;
|
||||
break;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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_USER_CLIENT_H
|
||||
#define _APPLE_EFFACEABLE_USER_CLIENT_H
|
||||
|
||||
#include <IOKit/IOUserClient.h>
|
||||
#include "AppleEffaceableStorage.h"
|
||||
|
||||
class AppleEffaceableStorageUserClient : public IOUserClient
|
||||
{
|
||||
OSDeclareAbstractStructors(AppleEffaceableStorageUserClient);
|
||||
|
||||
public:
|
||||
virtual bool start(IOService * provider);
|
||||
virtual IOReturn clientClose(void);
|
||||
virtual IOReturn externalMethod(uint32_t selector, IOExternalMethodArguments * arguments,
|
||||
IOExternalMethodDispatch * dispatch, OSObject * target,
|
||||
void * reference);
|
||||
|
||||
private:
|
||||
AppleEffaceableStorage * _provider;
|
||||
};
|
||||
|
||||
#endif /* _APPLE_EFFACEABLE_USER_CLIENT_H */
|
||||
@@ -0,0 +1,77 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>${EXECUTABLE_NAME}</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>${PRODUCT_NAME}</string>
|
||||
<key>CFBundleIconFile</key>
|
||||
<string></string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.driver.AppleEffaceableStorage</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>KEXT</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>OSBundleCompatibleVersion</key>
|
||||
<string>1.0</string>
|
||||
<key>IOKitPersonalities</key>
|
||||
<dict>
|
||||
<key>AppleEffaceableNAND</key>
|
||||
<dict>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.driver.AppleEffaceableStorage</string>
|
||||
<key>IOClass</key>
|
||||
<string>AppleEffaceableNAND</string>
|
||||
<key>IOPropertyMatch</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>Content Hint</key>
|
||||
<string>Effaceable</string>
|
||||
</dict>
|
||||
</array>
|
||||
<key>IOProviderClass</key>
|
||||
<string>IOFlashMedia</string>
|
||||
</dict>
|
||||
<key>AppleEffaceableNOR</key>
|
||||
<dict>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.driver.AppleEffaceableStorage</string>
|
||||
<key>IOClass</key>
|
||||
<string>AppleEffaceableNOR</string>
|
||||
<key>IONameMatch</key>
|
||||
<string>effaceable,nor</string>
|
||||
<key>IOProviderClass</key>
|
||||
<string>AppleARMNORFlashDevice</string>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>OSBundleCompatibleVersion</key>
|
||||
<string>1.0.0</string>
|
||||
<key>OSBundleLibraries</key>
|
||||
<dict>
|
||||
<key>com.apple.driver.AppleARMPlatform</key>
|
||||
<string>1.0.0</string>
|
||||
<key>com.apple.iokit.IOFlashStorage</key>
|
||||
<string>1.0.0</string>
|
||||
<key>com.apple.kpi.bsd</key>
|
||||
<string>8.0</string>
|
||||
<key>com.apple.kpi.iokit</key>
|
||||
<string>8.0</string>
|
||||
<key>com.apple.kpi.libkern</key>
|
||||
<string>8.0</string>
|
||||
<key>com.apple.kpi.mach</key>
|
||||
<string>8.0</string>
|
||||
<key>com.apple.kpi.unsupported</key>
|
||||
<string>8.0</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,537 @@
|
||||
/*
|
||||
* Copyright (c) 2010 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 <stdlib.h>
|
||||
#include <err.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/random.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <IOKit/IOKitLib.h>
|
||||
|
||||
#include "AppleEffaceableStorageKeys.h"
|
||||
#include "AppleEffaceableStorageFormat.h"
|
||||
#include "effaceable.h"
|
||||
|
||||
// =============================================================================
|
||||
|
||||
// XXX correct hard-coding of SHA1 buffer size
|
||||
#define SHA1_HASH_SIZE 20
|
||||
|
||||
// =============================================================================
|
||||
|
||||
bool
|
||||
connectClient(const char * name, io_service_t * service, io_connect_t * connection)
|
||||
{
|
||||
bool ok = false;
|
||||
mach_port_t master;
|
||||
CFDictionaryRef match;
|
||||
io_service_t found_service;
|
||||
io_connect_t found_connection;
|
||||
|
||||
if (KERN_SUCCESS != IOMasterPort(MACH_PORT_NULL, &master)) {
|
||||
warnx("IOMasterPort failed");
|
||||
} else if (IO_OBJECT_NULL == (match = IOServiceMatching(name))) {
|
||||
warnx("IOServiceMatching failed for %s", name);
|
||||
} else if (IO_OBJECT_NULL == (found_service = IOServiceGetMatchingService(master, match))) {
|
||||
warnx("IOServiceGetMatchingService failed");
|
||||
} else if (KERN_SUCCESS != IOServiceOpen(found_service, mach_task_self(), 0, &found_connection)) {
|
||||
warnx("IOServiceOpen failed for class '%s'", name);
|
||||
} else {
|
||||
*service = found_service;
|
||||
*connection = found_connection;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
// XXX clean up
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool
|
||||
wipeStorage(io_connect_t connection)
|
||||
{
|
||||
bool ok = false;
|
||||
|
||||
if (KERN_SUCCESS != IOConnectCallScalarMethod(connection,
|
||||
kAppleEffaceableStorageMethodWipeStorage,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
NULL)) {
|
||||
errx(1, "wipe failed");
|
||||
} else {
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool
|
||||
getCapacity(io_connect_t connection, IOByteCount * capacity)
|
||||
{
|
||||
bool ok = false;
|
||||
const uint32_t expected_count = 1;
|
||||
uint32_t output_count = expected_count;
|
||||
uint64_t output[output_count];
|
||||
|
||||
if (KERN_SUCCESS != IOConnectCallScalarMethod(connection,
|
||||
kAppleEffaceableStorageMethodGetCapacity,
|
||||
NULL,
|
||||
0,
|
||||
output,
|
||||
&output_count)) {
|
||||
warnx("IOConnectCallScalarMethod failed");
|
||||
} else if (expected_count != output_count) {
|
||||
warnx("unexpected scalar output count");
|
||||
} else {
|
||||
*capacity = (IOByteCount) output[0];
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool
|
||||
getBytes(io_connect_t connection, void * buf, IOByteCount offset, IOByteCount count)
|
||||
{
|
||||
bool ok = false;
|
||||
uint64_t input;
|
||||
size_t output_count = count;
|
||||
|
||||
input = (uint64_t)offset;
|
||||
|
||||
if (KERN_SUCCESS != IOConnectCallMethod(connection,
|
||||
kAppleEffaceableStorageMethodGetBytes,
|
||||
&input,
|
||||
1,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
buf,
|
||||
&output_count)) {
|
||||
warnx("IOConnectCallMethod failed");
|
||||
} else if (count != output_count) {
|
||||
warnx("unexpected output struct count");
|
||||
} else {
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool
|
||||
setBytes(io_connect_t connection, const void * buf, IOByteCount offset, IOByteCount count)
|
||||
{
|
||||
bool ok = false;
|
||||
uint64_t input;
|
||||
|
||||
input = (uint64_t)offset;
|
||||
|
||||
if (KERN_SUCCESS != IOConnectCallMethod(connection,
|
||||
kAppleEffaceableStorageMethodSetBytes,
|
||||
&input,
|
||||
1,
|
||||
buf,
|
||||
count,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL)) {
|
||||
warnx("IOConnectCallMethod failed");
|
||||
} else {
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool
|
||||
isFormatted(io_connect_t connection, bool * is_formatted)
|
||||
{
|
||||
bool ok = false;
|
||||
const uint32_t expected_count = 1;
|
||||
uint32_t output_count = expected_count;
|
||||
uint64_t output[output_count];
|
||||
|
||||
if (KERN_SUCCESS != IOConnectCallScalarMethod(connection,
|
||||
kAppleEffaceableStorageMethodIsFormatted,
|
||||
NULL,
|
||||
0,
|
||||
output,
|
||||
&output_count)) {
|
||||
warnx("IOConnectCallScalarMethod failed");
|
||||
} else if (expected_count != output_count) {
|
||||
warnx("unexpected scalar output count");
|
||||
} else {
|
||||
*is_formatted = (bool) output[0];
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool
|
||||
formatStorage(io_connect_t connection)
|
||||
{
|
||||
bool ok = false;
|
||||
|
||||
if (KERN_SUCCESS != IOConnectCallScalarMethod(connection,
|
||||
kAppleEffaceableStorageMethodFormatStorage,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
NULL)) {
|
||||
warnx("IOConnectCallScalarMethod failed");
|
||||
} else {
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
io_service_t service;
|
||||
io_connect_t connection;
|
||||
|
||||
if (0 != getuid()) {
|
||||
errx(1, "must be run as root (uid = %d)", getuid());
|
||||
}
|
||||
|
||||
if (!connectClient(kAppleEffaceableStorageClassName, &service, &connection)) {
|
||||
errx(1, "unable to connect to service");
|
||||
}
|
||||
|
||||
// crude command parser...really only intended as test vehicle
|
||||
if (1 >= argc) {
|
||||
printUsage(argc, argv);
|
||||
} else {
|
||||
if (0 == strcmp("format", argv[1])) {
|
||||
if (formatStorage(connection)) {
|
||||
printf("format succeeded\n");
|
||||
} else {
|
||||
printf("format failed\n");
|
||||
}
|
||||
} else if (0 == strcmp("wipe", argv[1])) {
|
||||
wipeStorage(connection);
|
||||
} else if (0 == strcmp("test", argv[1])) {
|
||||
quickTest(connection);
|
||||
} else if (0 == strcmp("lockers", argv[1])) {
|
||||
lockerList(connection);
|
||||
} else if (0 == strcmp("nonce", argv[1])) {
|
||||
generateNonce(connection);
|
||||
} else if (2 >= argc) {
|
||||
printUsage(argc, argv);
|
||||
} else if (0 == strcmp("efface", argv[1])) {
|
||||
lockerEfface(connection, argv[2]);
|
||||
} else if (3 >= argc) {
|
||||
printUsage(argc, argv);
|
||||
} else if (0 == strcmp("set", argv[1])) {
|
||||
lockerSet(connection, argv[2], argv[3]);
|
||||
} else if (0 == strcmp("get", argv[1])) {
|
||||
lockerGet(connection, argv[2], argv[3]);
|
||||
} else {
|
||||
printUsage(argc, argv);
|
||||
errx(1, "unrecognized subcommand");
|
||||
}
|
||||
}
|
||||
|
||||
// XXX clean up service and connection
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
printUsage(int argc, char *argv[])
|
||||
{
|
||||
printf("USAGE: %s <subcmd>\n\n", ((0 < argc) ? argv[0] : "?"));
|
||||
printf(" Where <subcmd> is one of following\n\n");
|
||||
printf(" format - format storage\n");
|
||||
printf(" wipe - wipe storage\n");
|
||||
printf(" test - perform quick test\n");
|
||||
printf(" lockers - list lockers and contents\n");
|
||||
printf(" nonce - generate a nonce and report its SHA1 hash\n");
|
||||
printf(" set <tag> <file> - write <file> into locker <tag>\n");
|
||||
printf(" get <tag> <file> - write <file> from locker <tag>\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void
|
||||
hexdump(const void * buf, IOByteCount count)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < count; i += 16) {
|
||||
printf("0x%08x:", ((unsigned) buf) + i);
|
||||
for (j = 0; (j < 16) && ((i + j) < count); j++) {
|
||||
printf(" %2.2x", ((uint8_t*)buf)[i+j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
quickTest(io_connect_t connection)
|
||||
{
|
||||
const IOByteCount count = 64;
|
||||
IOByteCount offset;
|
||||
IOByteCount capacity;
|
||||
void * sbuf = NULL;
|
||||
void * gbuf = NULL;
|
||||
unsigned i;
|
||||
|
||||
if (!getCapacity(connection, &capacity)) {
|
||||
errx(1, "unable to get capacity");
|
||||
}
|
||||
printf("capacity reported as %u; expected 960\n", (unsigned) capacity);
|
||||
|
||||
offset = capacity - count - 1;
|
||||
sbuf = malloc(count);
|
||||
gbuf = malloc(count);
|
||||
|
||||
if ((NULL == sbuf) || (NULL == gbuf)) {
|
||||
errx(1, "failed to allocate buffers");
|
||||
}
|
||||
|
||||
if (!getBytes(connection, gbuf, offset, count)) {
|
||||
errx(1, "unable to get bytes");
|
||||
}
|
||||
printf("read %u bytes from %u offset\n", (unsigned) count, (unsigned) offset);
|
||||
hexdump(gbuf, count);
|
||||
|
||||
srandom(((unsigned *)gbuf)[0]);
|
||||
for (i = 0; i < (count / sizeof(long)); i++) {
|
||||
((long *)sbuf)[i] = random();
|
||||
}
|
||||
|
||||
if (!setBytes(connection, sbuf, offset, count)) {
|
||||
errx(1, "unable to set bytes");
|
||||
}
|
||||
printf("wrote %u bytes to %u offset\n", (unsigned) count, (unsigned) offset);
|
||||
hexdump(sbuf, count);
|
||||
|
||||
if (!getBytes(connection, gbuf, offset, count)) {
|
||||
errx(1, "unable to get bytes");
|
||||
}
|
||||
printf("read %u bytes from %u offset\n", (unsigned) count, (unsigned) offset);
|
||||
hexdump(gbuf, count);
|
||||
|
||||
if (0 != bcmp(sbuf, gbuf, count)) {
|
||||
errx(1, "bytes read back differ from those written");
|
||||
}
|
||||
printf("test passed\n");
|
||||
|
||||
if (NULL != sbuf) {
|
||||
free(sbuf);
|
||||
}
|
||||
if (NULL != gbuf) {
|
||||
free(gbuf);
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
#define UNTAG(x) (int)(((x) >> 24) & 0xff),(int)(((x) >> 16) & 0xff),(int)(((x) >> 8) & 0xff),(int)((x) & 0xff)
|
||||
|
||||
void
|
||||
lockerList(io_connect_t connection)
|
||||
{
|
||||
uint8_t *buf, *cursor;
|
||||
IOByteCount size;
|
||||
AppleEffaceableStorageLockerHeader *header;
|
||||
|
||||
if (!getCapacity(connection, &size))
|
||||
errx(1, "unable to get capacity");
|
||||
if (size != kAppleEffaceableStorageLockerSize)
|
||||
warnx("size %lu not %lu as expected", size, kAppleEffaceableStorageLockerSize);
|
||||
buf = malloc(size);
|
||||
|
||||
if (!getBytes(connection, buf, 0, size))
|
||||
errx(1, "unable to read");
|
||||
|
||||
cursor = buf;
|
||||
for (;;) {
|
||||
header = (AppleEffaceableStorageLockerHeader *)cursor;
|
||||
if (header->magic != kAppleEffaceableStorageLockerMagic) {
|
||||
warnx("unexpected magic 0x%04x at %u, expected 0x%04x", header->magic, cursor - buf, kAppleEffaceableStorageLockerMagic);
|
||||
break;
|
||||
}
|
||||
if (header->type_id == kAppleEffaceableStorageLockerSentinel) {
|
||||
printf("<END>\n");
|
||||
break;
|
||||
}
|
||||
printf("0x%08x/0x%04x %c%c%c%c %s\n",
|
||||
(unsigned)header->type_id, header->data_size, UNTAG(header->type_id),
|
||||
(header->type_id & kAppleEffaceableStorageLockerProtected) ? "(protected)" : "");
|
||||
cursor += sizeof(*header);
|
||||
if (header->data_size > 0) {
|
||||
if ((cursor + header->data_size) > (buf + size)) {
|
||||
warnx("data overflows buffer");
|
||||
break;
|
||||
}
|
||||
hexdump(cursor, header->data_size);
|
||||
cursor += header->data_size;
|
||||
}
|
||||
if ((cursor + sizeof(*header)) > (buf + size)) {
|
||||
warnx("header overflows buffer");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
lockerGet(io_connect_t connection, char *tag, char *file)
|
||||
{
|
||||
UInt32 tagVal;
|
||||
UInt8 tagStr[4];
|
||||
UInt8 buf[1024];
|
||||
UInt64 input, output;
|
||||
uint32_t output_count;
|
||||
UInt32 output_size;
|
||||
int fd;
|
||||
|
||||
// get the tag
|
||||
memset(tagStr, 0, 4);
|
||||
strncpy((char *)tagStr, tag, 4);
|
||||
tagVal = ((UInt32)tagStr[0] << 24) + ((UInt32)tagStr[1] << 16) + ((UInt32)tagStr[2] << 8) + (UInt32)tagStr[3];
|
||||
|
||||
// get the locker
|
||||
input = tagVal;
|
||||
output_count = 1;
|
||||
output_size = sizeof(buf);
|
||||
if (KERN_SUCCESS != IOConnectCallMethod(connection, // connection
|
||||
kAppleEffaceableStorageMethodGetLocker, // selector
|
||||
&input, // input
|
||||
1, // input count
|
||||
NULL, // input struct
|
||||
0, // input struct size
|
||||
&output, // output
|
||||
&output_count, // output count
|
||||
&buf, // output struct
|
||||
&output_size)) { // output struct size
|
||||
errx(1, "getLocker call failed");
|
||||
}
|
||||
|
||||
// write the file
|
||||
if ((fd = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
|
||||
err(1, "failed to create %s", file);
|
||||
warnx("using descriptor %d", fd);
|
||||
if (write(fd, buf, output_size) != output_size)
|
||||
err(1, "failed writing %lu bytes to %s (fd %d)", output_size, file, fd);
|
||||
close(fd);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void
|
||||
lockerSet(io_connect_t connection, char *tag, char *file)
|
||||
{
|
||||
UInt32 tagVal;
|
||||
UInt8 tagStr[4];
|
||||
UInt8 buf[1024];
|
||||
UInt64 input;
|
||||
int fd, size;
|
||||
|
||||
// get the tag
|
||||
memset(tagStr, 0, 4);
|
||||
strncpy((char *)tagStr, tag, 4);
|
||||
tagVal = ((UInt32)tagStr[0] << 24) + ((UInt32)tagStr[1] << 16) + ((UInt32)tagStr[2] << 8) + (UInt32)tagStr[3];
|
||||
|
||||
// read the file
|
||||
if ((fd = open(file, O_RDONLY)) < 0)
|
||||
err(1, "failed to open %s", file);
|
||||
if ((size = read(fd, buf, sizeof(buf))) < 0)
|
||||
err(1, "failed to read from %s", file);
|
||||
close(fd);
|
||||
|
||||
|
||||
// make the call
|
||||
input = tagVal;
|
||||
if (KERN_SUCCESS != IOConnectCallMethod(connection,
|
||||
kAppleEffaceableStorageMethodSetLocker,
|
||||
&input,
|
||||
1,
|
||||
&buf,
|
||||
size,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
NULL)) {
|
||||
errx(1, "setLocker call failed");
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void
|
||||
lockerEfface(io_connect_t connection, char *tag)
|
||||
{
|
||||
UInt32 tagVal;
|
||||
UInt8 tagStr[4];
|
||||
UInt64 input;
|
||||
|
||||
// get the tag
|
||||
memset(tagStr, 0, 4);
|
||||
strncpy((char *)tagStr, tag, 4);
|
||||
tagVal = ((UInt32)tagStr[0] << 24) + ((UInt32)tagStr[1] << 16) + ((UInt32)tagStr[2] << 8) + (UInt32)tagStr[3];
|
||||
|
||||
// make the call
|
||||
input = tagVal;
|
||||
if (KERN_SUCCESS != IOConnectCallMethod(connection,
|
||||
kAppleEffaceableStorageMethodEffaceLocker,
|
||||
&input,
|
||||
1,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
NULL)) {
|
||||
errx(1, "effaceLocker call failed");
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void
|
||||
generateNonce(io_connect_t connection)
|
||||
{
|
||||
|
||||
UInt8 buf[SHA1_HASH_SIZE];
|
||||
UInt32 output_size = sizeof(buf);
|
||||
UInt32 idx;
|
||||
|
||||
memset(buf, 0, output_size);
|
||||
if (KERN_SUCCESS != IOConnectCallMethod(connection, // connection
|
||||
kAppleEffaceableStorageMethodGenerateNonce, // selector
|
||||
NULL, // input
|
||||
0, // input count
|
||||
NULL, // input struct
|
||||
0, // input struct size
|
||||
NULL, // output
|
||||
NULL, // output count
|
||||
&buf, // output struct
|
||||
&output_size)) { // output struct size
|
||||
errx(1, "generateNonce call failed");
|
||||
}
|
||||
|
||||
// report the nonce SHA1 hash
|
||||
for (idx = 0; idx < output_size; idx++) {
|
||||
printf("%s%02X", (idx ? " " : ""), buf[idx]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2010 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_H
|
||||
#define _EFFACEABLE_H
|
||||
|
||||
#include <IOKit/IOTypes.h>
|
||||
|
||||
bool connectClient(const char * name, io_service_t * service, io_connect_t * connection);
|
||||
|
||||
bool wipeStorage(io_connect_t connection);
|
||||
bool getCapacity(io_connect_t connection, IOByteCount * capacity);
|
||||
bool getBytes(io_connect_t connection, void * buf, IOByteCount offset, IOByteCount count);
|
||||
bool setBytes(io_connect_t connection, const void * buf, IOByteCount offset, IOByteCount count);
|
||||
bool isFormatted(io_connect_t connection, bool * is_formatted);
|
||||
bool formatStorage(io_connect_t connection);
|
||||
|
||||
int main(int argc, char * argv[]);
|
||||
void printUsage(int argc, char *argv[]);
|
||||
|
||||
void hexdump(const void * buf, IOByteCount count);
|
||||
void quickTest(io_connect_t connection);
|
||||
void lockerList(io_connect_t connection);
|
||||
void lockerSet(io_connect_t connection, char *tag, char *file);
|
||||
void lockerGet(io_connect_t connection, char *tag, char *file);
|
||||
void lockerEfface(io_connect_t connection, char *tag);
|
||||
void generateNonce(io_connect_t connection);
|
||||
|
||||
|
||||
#endif /* _EFFACEABLE_H */
|
||||
Reference in new issue
Block a user