- IORegistryEntry *parent;
- OSData *prop;
- // cells in addresses at regEntry
- UInt32 sizeCells, addressCells;
- // cells in addresses below regEntry
- UInt32 childSizeCells, childAddressCells;
- UInt32 childCells;
- UInt32 cell[ 5 ], offset = 0, length;
- UInt32 *range;
- UInt32 *endRanges;
- bool ok = true;
- SInt32 diff;
-
- IODTPersistent *persist;
- IODTCompareAddressCellFunc compare;
-
- IODTGetCellCounts( regEntry, &childSizeCells, &childAddressCells );
- childCells = childAddressCells + childSizeCells;
-
- bcopy( cellsIn, cell, 4 * childCells );
- if( childSizeCells > 1)
- *len = IOPhysical32( cellsIn[ childAddressCells ],
- cellsIn[ childAddressCells + 1 ] );
- else
- *len = IOPhysical32( 0, cellsIn[ childAddressCells ] );
-
- do {
- prop = OSDynamicCast( OSData, regEntry->getProperty( gIODTRangeKey ));
- if( 0 == prop) {
- /* end of the road */
- *phys = IOPhysical32( 0, cell[ childAddressCells - 1 ] + offset);
- break;
- }
-
- parent = regEntry->getParentEntry( gIODTPlane );
- IODTGetCellCounts( parent, &sizeCells, &addressCells );
-
- if( (length = prop->getLength())) {
- // search
- range = (UInt32 *) prop->getBytesNoCopy();
- endRanges = range + (length / 4);
-
- prop = (OSData *) regEntry->getProperty( gIODTPersistKey );
- if( prop) {
- persist = (IODTPersistent *) prop->getBytesNoCopy();
- compare = persist->compareFunc;
- } else
- compare = DefaultCompare;
-
- for( ok = false;
- range < endRanges;
- range += (childCells + addressCells) ) {
-
- // is cell >= range start?
- diff = (*compare)( childAddressCells, cell, range );
- if( diff < 0)
- continue;
-
- // is cell + size <= range end?
- if( (diff + cell[ childCells - 1 ])
- > range[ childCells + addressCells - 1 ])
- continue;
-
- offset += diff;
- ok = true;
- break;
- }
-
- // Get the physical start of the range from our parent
- bcopy( range + childAddressCells, cell, 4 * addressCells );
- bzero( cell + addressCells, 4 * sizeCells );
-
- } /* else zero length range => pass thru to parent */
-
- regEntry = parent;
- childSizeCells = sizeCells;
- childAddressCells = addressCells;
- childCells = childAddressCells + childSizeCells;
-
- } while( ok && regEntry);
-
- return( ok);
+ IORegistryEntry * parent;
+ IORegistryEntry * regEntry;
+ OSData * prop;
+ OSNumber * num;
+ unsigned int index, count;
+ // cells in addresses at regEntry
+ UInt32 sizeCells, addressCells;
+ // cells in addresses below regEntry
+ UInt32 childSizeCells, childAddressCells;
+ UInt32 childCells;
+ UInt32 cell[8], propLen;
+ UInt64 offset = 0;
+ UInt32 endCell[8];
+ UInt32 *range;
+ UInt32 *lookRange;
+ UInt32 *startRange;
+ UInt32 *endRanges;
+ bool ok = true;
+ SInt64 diff, diff2, endDiff;
+ UInt64 len, rangeLen;
+
+ IODTPersistent *persist;
+ IODTCompareAddressCellFunc compare;
+
+ regEntry = startEntry;
+ IODTGetCellCounts( regEntry, &childSizeCells, &childAddressCells );
+ childCells = childAddressCells + childSizeCells;
+
+ if (childCells > sizeof(cell) / sizeof(cell[0])) {
+ panic("IODTResolveAddressCell: Invalid device tree (%u,%u)", (uint32_t)childAddressCells, (uint32_t)childSizeCells);
+ }
+
+ bcopy( cellsIn, cell, sizeof(UInt32) * childCells );
+ *lenOut = CellsValue( childSizeCells, cellsIn + childAddressCells );
+
+ do{
+ prop = OSDynamicCast( OSData, regEntry->getProperty( gIODTRangeKey ));
+ if (NULL == prop) {
+ /* end of the road */
+ *phys = CellsValue( childAddressCells, cell );
+ *phys += offset;
+ if (regEntry != startEntry) {
+ regEntry->release();
+ }
+ break;
+ }
+
+ parent = regEntry->copyParentEntry( gIODTPlane );
+ IODTGetCellCounts( parent, &sizeCells, &addressCells );
+
+ if ((propLen = prop->getLength())) {
+ // search
+ startRange = (UInt32 *) prop->getBytesNoCopy();
+ range = startRange;
+ endRanges = range + (propLen / sizeof(UInt32));
+
+ compare = NULL;
+ num = OSDynamicCast(OSNumber, regEntry->getProperty(gIODTPersistKey));
+ if (num) {
+ IOLockLock(gIODTResolversLock);
+ index = num->unsigned32BitValue();
+ count = gIODTResolvers->getLength() / sizeof(IODTPersistent);
+ if (index < count) {
+ persist = ((IODTPersistent *) gIODTResolvers->getBytesNoCopy()) + index;
+ compare = persist->compareFunc;
+ }
+ IOLockUnlock(gIODTResolversLock);
+ }
+
+ if (!compare && (addressCells == childAddressCells)) {
+ compare = DefaultCompare;
+ }
+ if (!compare) {
+ panic("There is no mixed comparison function yet...");
+ }
+
+ for (ok = false;
+ range < endRanges;
+ range += (childCells + addressCells)) {
+ // is cell start within range?
+ diff = (*compare)( childAddressCells, cell, range );
+
+ if (childAddressCells > sizeof(endCell) / sizeof(endCell[0])) {
+ panic("IODTResolveAddressCell: Invalid device tree (%u)", (uint32_t)childAddressCells);
+ }
+
+ bcopy(range, endCell, childAddressCells * sizeof(UInt32));
+
+ rangeLen = CellsValue(childSizeCells, range + childAddressCells + addressCells);
+ AddLengthToCells(childAddressCells, endCell, rangeLen);
+
+ diff2 = (*compare)( childAddressCells, cell, endCell );
+
+ // if start of cell < start of range, or end of range >= start of cell, skip
+ if ((diff < 0) || (diff2 >= 0)) {
+ continue;
+ }
+
+ len = CellsValue(childSizeCells, cell + childAddressCells);
+ ok = (0 == len);
+
+ if (!ok) {
+ // search for cell end
+ bcopy(cell, endCell, childAddressCells * sizeof(UInt32));
+
+ AddLengthToCells(childAddressCells, endCell, len - 1);
+
+ for (lookRange = startRange;
+ lookRange < endRanges;
+ lookRange += (childCells + addressCells)) {
+ // make sure end of cell >= range start
+ endDiff = (*compare)( childAddressCells, endCell, lookRange );
+ if (endDiff < 0) {
+ continue;
+ }
+
+ UInt64 rangeStart = CellsValue(addressCells, range + childAddressCells);
+ UInt64 lookRangeStart = CellsValue(addressCells, lookRange + childAddressCells);
+ if ((endDiff - len + 1 + lookRangeStart) == (diff + rangeStart)) {
+ ok = true;
+ break;
+ }
+ }
+ if (!ok) {
+ continue;
+ }
+ }
+ offset += diff;
+ break;
+ }
+
+ if (addressCells + sizeCells > sizeof(cell) / sizeof(cell[0])) {
+ panic("IODTResolveAddressCell: Invalid device tree (%u, %u)", (uint32_t)addressCells, (uint32_t)sizeCells);
+ }
+
+ // Get the physical start of the range from our parent
+ bcopy( range + childAddressCells, cell, sizeof(UInt32) * addressCells );
+ bzero( cell + addressCells, sizeof(UInt32) * sizeCells );
+ } /* else zero length range => pass thru to parent */
+
+ if (regEntry != startEntry) {
+ regEntry->release();
+ }
+ regEntry = parent;
+ childSizeCells = sizeCells;
+ childAddressCells = addressCells;
+ childCells = childAddressCells + childSizeCells;
+ }while (ok && regEntry);
+
+ return ok;