1711d51d8SMatt Spinler /**
2711d51d8SMatt Spinler  * Copyright © 2019 IBM Corporation
3711d51d8SMatt Spinler  *
4711d51d8SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5711d51d8SMatt Spinler  * you may not use this file except in compliance with the License.
6711d51d8SMatt Spinler  * You may obtain a copy of the License at
7711d51d8SMatt Spinler  *
8711d51d8SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9711d51d8SMatt Spinler  *
10711d51d8SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11711d51d8SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12711d51d8SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13711d51d8SMatt Spinler  * See the License for the specific language governing permissions and
14711d51d8SMatt Spinler  * limitations under the License.
15711d51d8SMatt Spinler  */
1614d671faSMatt Spinler #include "generic.hpp"
1714d671faSMatt Spinler 
18*5bc26533SArya K Padman #include <phosphor-logging/lg2.hpp>
1914d671faSMatt Spinler 
201aa90d49SJayanth Othayoth #include <format>
211aa90d49SJayanth Othayoth 
2214d671faSMatt Spinler namespace openpower
2314d671faSMatt Spinler {
2414d671faSMatt Spinler namespace pels
2514d671faSMatt Spinler {
2614d671faSMatt Spinler 
unflatten(Stream & stream)2714d671faSMatt Spinler void Generic::unflatten(Stream& stream)
2814d671faSMatt Spinler {
2914d671faSMatt Spinler     stream >> _header;
3014d671faSMatt Spinler 
3114d671faSMatt Spinler     if (_header.size <= SectionHeader::flattenedSize())
3214d671faSMatt Spinler     {
3314d671faSMatt Spinler         throw std::out_of_range(
3414d671faSMatt Spinler             "Generic::unflatten: SectionHeader::size field too small");
3514d671faSMatt Spinler     }
3614d671faSMatt Spinler 
3714d671faSMatt Spinler     size_t dataLength = _header.size - SectionHeader::flattenedSize();
3814d671faSMatt Spinler     _data.resize(dataLength);
3914d671faSMatt Spinler 
4014d671faSMatt Spinler     stream >> _data;
4114d671faSMatt Spinler }
4214d671faSMatt Spinler 
flatten(Stream & stream) const430688545bSMatt Spinler void Generic::flatten(Stream& stream) const
4414d671faSMatt Spinler {
4514d671faSMatt Spinler     stream << _header << _data;
4614d671faSMatt Spinler }
4714d671faSMatt Spinler 
Generic(Stream & pel)4814d671faSMatt Spinler Generic::Generic(Stream& pel)
4914d671faSMatt Spinler {
5014d671faSMatt Spinler     try
5114d671faSMatt Spinler     {
5214d671faSMatt Spinler         unflatten(pel);
5314d671faSMatt Spinler         validate();
5414d671faSMatt Spinler     }
5514d671faSMatt Spinler     catch (const std::exception& e)
5614d671faSMatt Spinler     {
57*5bc26533SArya K Padman         lg2::error("Cannot unflatten generic section: {EXCEPTION}", "EXCEPTION",
58*5bc26533SArya K Padman                    e);
5914d671faSMatt Spinler         _valid = false;
6014d671faSMatt Spinler     }
6114d671faSMatt Spinler }
6214d671faSMatt Spinler 
validate()6314d671faSMatt Spinler void Generic::validate()
6414d671faSMatt Spinler {
6514d671faSMatt Spinler     // Nothing to validate
6614d671faSMatt Spinler     _valid = true;
6714d671faSMatt Spinler }
6814d671faSMatt Spinler 
6914d671faSMatt Spinler } // namespace pels
7014d671faSMatt Spinler } // namespace openpower
71